]> git.ipfire.org Git - thirdparty/man-pages.git/commitdiff
man/: EXAMPLES: Use err(3) and errc(3bsd) instead of similar macros
authorAlejandro Colomar <alx@kernel.org>
Tue, 19 Aug 2025 15:48:50 +0000 (17:48 +0200)
committerAlejandro Colomar <alx@kernel.org>
Wed, 20 Aug 2025 16:14:03 +0000 (18:14 +0200)
These functions are quite portable.  And if one doesn't have them for
some reason (but libbsd has been ported to many systems), one can write
them easily as macros, anyway.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
24 files changed:
man/man2/bind.2
man/man2/mmap.2
man/man2/mprotect.2
man/man2/poll.2
man/man2/shmop.2
man/man2/timer_create.2
man/man3/duplocale.3
man/man3/inet_net_pton.3
man/man3/makecontext.3
man/man3/mq_getattr.3
man/man3/mq_notify.3
man/man3/newlocale.3
man/man3/posix_spawn.3
man/man3/pthread_cancel.3
man/man3/pthread_cleanup_push.3
man/man3/pthread_create.3
man/man3/pthread_getcpuclockid.3
man/man3/pthread_mutexattr_setrobust.3
man/man3/pthread_setschedparam.3
man/man3/pthread_sigmask.3
man/man3/sem_wait.3
man/man3/shm_open.3
man/man4/loop.4
man/man7/aio.7

index 5d44c152a63e0f7527367e38851c6802cd70b34d..309f6d9d7c19dad64ae65d54dc5f4f9ef7569272 100644 (file)
@@ -209,6 +209,7 @@ domain, and accept connections:
 .P
 .\" SRC BEGIN (bind.c)
 .EX
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -219,9 +220,6 @@ domain, and accept connections:
 #define MY_SOCK_PATH "/somepath"
 #define LISTEN_BACKLOG 50
 \&
-#define handle_error(msg) \[rs]
-    do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 int
 main(void)
 {
@@ -231,7 +229,7 @@ main(void)
 \&
     sfd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (sfd == \-1)
-        handle_error("socket");
+        err(EXIT_FAILURE, "socket");
 \&
     memset(&my_addr, 0, sizeof(my_addr));
     my_addr.sun_family = AF_UNIX;
@@ -240,10 +238,10 @@ main(void)
 \&
     if (bind(sfd, (struct sockaddr *) &my_addr,
              sizeof(my_addr)) == \-1)
-        handle_error("bind");
+        err(EXIT_FAILURE, "bind");
 \&
     if (listen(sfd, LISTEN_BACKLOG) == \-1)
-        handle_error("listen");
+        err(EXIT_FAILURE, "listen");
 \&
     /* Now we can accept incoming connections one
        at a time using accept(2). */
@@ -252,15 +250,15 @@ main(void)
     cfd = accept(sfd, (struct sockaddr *) &peer_addr,
                  &peer_addr_size);
     if (cfd == \-1)
-        handle_error("accept");
+        err(EXIT_FAILURE, "accept");
 \&
     /* Code to deal with incoming connection(s)... */
 \&
     if (close(sfd) == \-1)
-        handle_error("close");
+        err(EXIT_FAILURE, "close");
 \&
     if (unlink(MY_SOCK_PATH) == \-1)
-        handle_error("unlink");
+        err(EXIT_FAILURE, "unlink");
 }
 .EE
 .\" SRC END
index 28ad07198a3a9ee9a3df993da54551129d4b2d75..23ffcd758cc94c3c03f766e152c8d1b54015eff4 100644 (file)
@@ -948,6 +948,7 @@ to output the desired bytes.
 .SS Program source
 .\" SRC BEGIN (mmap.c)
 .EX
+#include <err.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -956,9 +957,6 @@ to output the desired bytes.
 #include <sys/types.h>
 #include <unistd.h>
 \&
-#define handle_error(msg) \[rs]
-    do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 int
 main(int argc, char *argv[])
 {
@@ -976,10 +974,10 @@ main(int argc, char *argv[])
 \&
     fd = open(argv[1], O_RDONLY);
     if (fd == \-1)
-        handle_error("open");
+        err(EXIT_FAILURE, "open");
 \&
     if (fstat(fd, &sb) == \-1)           /* To obtain file size */
-        handle_error("fstat");
+        err(EXIT_FAILURE, "fstat");
 \&
     offset = atoi(argv[2]);
     pa_offset = offset & \[ti](sysconf(_SC_PAGE_SIZE) \- 1);
@@ -1003,12 +1001,12 @@ main(int argc, char *argv[])
     addr = mmap(NULL, length + offset \- pa_offset, PROT_READ,
                 MAP_PRIVATE, fd, pa_offset);
     if (addr == MAP_FAILED)
-        handle_error("mmap");
+        err(EXIT_FAILURE, "mmap");
 \&
     s = write(STDOUT_FILENO, addr + offset \- pa_offset, length);
     if (s != length) {
         if (s == \-1)
-            handle_error("write");
+            err(EXIT_FAILURE, "write");
 \&
         fprintf(stderr, "partial write");
         exit(EXIT_FAILURE);
index 773d93dc9c895019036a1234ec6c20e3dc7f5abe..269ae204d61839445e859f69cd1dfc90d2d41fa1 100644 (file)
@@ -293,6 +293,7 @@ Got SIGSEGV at address: 0x804e000
 \&
 .\" SRC BEGIN (mprotect.c)
 .EX
+#include <err.h>
 #include <malloc.h>
 #include <signal.h>
 #include <stdio.h>
@@ -300,9 +301,6 @@ Got SIGSEGV at address: 0x804e000
 #include <sys/mman.h>
 #include <unistd.h>
 \&
-#define handle_error(msg) \[rs]
-    do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static char *buffer;
 \&
 static void
@@ -328,24 +326,24 @@ main(void)
     sigemptyset(&sa.sa_mask);
     sa.sa_sigaction = handler;
     if (sigaction(SIGSEGV, &sa, NULL) == \-1)
-        handle_error("sigaction");
+        err(EXIT_FAILURE, "sigaction");
 \&
     pagesize = sysconf(_SC_PAGE_SIZE);
     if (pagesize == \-1)
-        handle_error("sysconf");
+        err(EXIT_FAILURE, "sysconf");
 \&
     /* Allocate a buffer aligned on a page boundary;
        initial protection is PROT_READ | PROT_WRITE. */
 \&
     buffer = memalign(pagesize, 4 * pagesize);
     if (buffer == NULL)
-        handle_error("memalign");
+        err(EXIT_FAILURE, "memalign");
 \&
     printf("Start of region:        %p\[rs]n", buffer);
 \&
     if (mprotect(buffer + pagesize * 2, pagesize,
                  PROT_READ) == \-1)
-        handle_error("mprotect");
+        err(EXIT_FAILURE, "mprotect");
 \&
     for (char *p = buffer ; ; )
         *(p++) = \[aq]a\[aq];
index 85856bb34df8b8d79bc2b53fdedaa9d4ec572be0..e3e5119d2e7cf5ea6dae655f55011f27bafe41a1 100644 (file)
@@ -555,6 +555,7 @@ at which point the file descriptor was closed and the program terminated.
 \&
    Licensed under GNU General Public License v2 or later.
 */
+#include <err.h>
 #include <fcntl.h>
 #include <poll.h>
 #include <stdio.h>
@@ -562,9 +563,6 @@ at which point the file descriptor was closed and the program terminated.
 #include <sys/types.h>
 #include <unistd.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 int
 main(int argc, char *argv[])
 {
@@ -582,14 +580,14 @@ main(int argc, char *argv[])
     num_open_fds = nfds = argc \- 1;
     pfds = calloc(nfds, sizeof(struct pollfd));
     if (pfds == NULL)
-        errExit("malloc");
+        err(EXIT_FAILURE, "malloc");
 \&
     /* Open each file on command line, and add it to \[aq]pfds\[aq] array. */
 \&
     for (nfds_t j = 0; j < nfds; j++) {
         pfds[j].fd = open(argv[j + 1], O_RDONLY);
         if (pfds[j].fd == \-1)
-            errExit("open");
+            err(EXIT_FAILURE, "open");
 \&
         printf("Opened \[rs]"%s\[rs]" on fd %d\[rs]n", argv[j + 1], pfds[j].fd);
 \&
@@ -603,7 +601,7 @@ main(int argc, char *argv[])
         printf("About to poll()\[rs]n");
         ready = poll(pfds, nfds, \-1);
         if (ready == \-1)
-            errExit("poll");
+            err(EXIT_FAILURE, "poll");
 \&
         printf("Ready: %d\[rs]n", ready);
 \&
@@ -619,13 +617,13 @@ main(int argc, char *argv[])
                 if (pfds[j].revents & POLLIN) {
                     s = read(pfds[j].fd, buf, sizeof(buf));
                     if (s == \-1)
-                        errExit("read");
+                        err(EXIT_FAILURE, "read");
                     printf("    read %zd bytes: %.*s\[rs]n",
                            s, (int) s, buf);
                 } else {                /* POLLERR | POLLHUP */
                     printf("    closing fd %d\[rs]n", pfds[j].fd);
                     if (close(pfds[j].fd) == \-1)
-                        errExit("close");
+                        err(EXIT_FAILURE, "close");
                     num_open_fds\-\-;
                 }
             }
index 12e7bd763fb1661d26ab5b3d3ac71761014efb8b..660d9615ea255b11cac433cedc3e0019f0d33e62 100644 (file)
@@ -312,13 +312,11 @@ The following header file is included by the "reader" and "writer" programs:
 #ifndef SVSHM_STRING_H
 #define SVSHM_STRING_H
 \&
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/sem.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 union semun {                   /* Used in calls to semctl() */
     int                 val;
     struct semid_ds     *buf;
@@ -372,23 +370,23 @@ main(void)
 \&
     shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600);
     if (shmid == \-1)
-        errExit("shmget");
+        err(EXIT_FAILURE, "shmget");
 \&
     semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
     if (semid == \-1)
-        errExit("semget");
+        err(EXIT_FAILURE, "semget");
 \&
     /* Attach shared memory into our address space. */
 \&
     addr = shmat(shmid, NULL, SHM_RDONLY);
     if (addr == (void *) \-1)
-        errExit("shmat");
+        err(EXIT_FAILURE, "shmat");
 \&
     /* Initialize semaphore 0 in set with value 1. */
 \&
     arg.val = 1;
     if (semctl(semid, 0, SETVAL, arg) == \-1)
-        errExit("semctl");
+        err(EXIT_FAILURE, "semctl");
 \&
     printf("shmid = %d; semid = %d\[rs]n", shmid, semid);
 \&
@@ -399,7 +397,7 @@ main(void)
     sop.sem_flg = 0;
 \&
     if (semop(semid, &sop, 1) == \-1)
-        errExit("semop");
+        err(EXIT_FAILURE, "semop");
 \&
     /* Print the string from shared memory. */
 \&
@@ -408,9 +406,9 @@ main(void)
     /* Remove shared memory and semaphore set. */
 \&
     if (shmctl(shmid, IPC_RMID, NULL) == \-1)
-        errExit("shmctl");
+        err(EXIT_FAILURE, "shmctl");
     if (semctl(semid, 0, IPC_RMID, dummy) == \-1)
-        errExit("semctl");
+        err(EXIT_FAILURE, "semctl");
 \&
     exit(EXIT_SUCCESS);
 }
@@ -470,7 +468,7 @@ main(int argc, char *argv[])
 \&
     addr = shmat(shmid, NULL, 0);
     if (addr == (void *) \-1)
-        errExit("shmat");
+        err(EXIT_FAILURE, "shmat");
 \&
     memcpy(addr, argv[3], size);
 \&
@@ -481,7 +479,7 @@ main(int argc, char *argv[])
     sop.sem_flg = 0;
 \&
     if (semop(semid, &sop, 1) == \-1)
-        errExit("semop");
+        err(EXIT_FAILURE, "semop");
 \&
     exit(EXIT_SUCCESS);
 }
index 287d7bda4ade690ae8ae7aec4a6b838eef89f2c4..90ed35ef8529eeec4649749678a642116d4c47fb 100644 (file)
@@ -353,6 +353,7 @@ Caught signal 34
 \&
 .\" SRC BEGIN (timer_create.c)
 .EX
+#include <err.h>
 #include <signal.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -363,9 +364,6 @@ Caught signal 34
 #define CLOCKID CLOCK_REALTIME
 #define SIG SIGRTMIN
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 static void
 print_siginfo(siginfo_t *si)
 {
@@ -379,9 +377,9 @@ print_siginfo(siginfo_t *si)
 \&
     or = timer_getoverrun(*tidp);
     if (or == \-1)
-        errExit("timer_getoverrun");
-    else
-        printf("    overrun count = %d\[rs]n", or);
+        err(EXIT_FAILURE, "timer_getoverrun");
+\&
+    printf("    overrun count = %d\[rs]n", or);
 }
 \&
 static void
@@ -421,7 +419,7 @@ main(int argc, char *argv[])
     sa.sa_sigaction = handler;
     sigemptyset(&sa.sa_mask);
     if (sigaction(SIG, &sa, NULL) == \-1)
-        errExit("sigaction");
+        err(EXIT_FAILURE, "sigaction");
 \&
     /* Block timer signal temporarily. */
 \&
@@ -429,7 +427,7 @@ main(int argc, char *argv[])
     sigemptyset(&mask);
     sigaddset(&mask, SIG);
     if (sigprocmask(SIG_SETMASK, &mask, NULL) == \-1)
-        errExit("sigprocmask");
+        err(EXIT_FAILURE, "sigprocmask");
 \&
     /* Create the timer. */
 \&
@@ -437,7 +435,7 @@ main(int argc, char *argv[])
     sev.sigev_signo = SIG;
     sev.sigev_value.sival_ptr = &timerid;
     if (timer_create(CLOCKID, &sev, &timerid) == \-1)
-        errExit("timer_create");
+        err(EXIT_FAILURE, "timer_create");
 \&
     printf("timer ID is %#jx\[rs]n", (uintmax_t) timerid);
 \&
@@ -450,7 +448,7 @@ main(int argc, char *argv[])
     its.it_interval.tv_nsec = its.it_value.tv_nsec;
 \&
     if (timer_settime(timerid, 0, &its, NULL) == \-1)
-         errExit("timer_settime");
+         err(EXIT_FAILURE, "timer_settime");
 \&
     /* Sleep for a while; meanwhile, the timer may expire
        multiple times. */
@@ -463,7 +461,7 @@ main(int argc, char *argv[])
 \&
     printf("Unblocking signal %d\[rs]n", SIG);
     if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == \-1)
-        errExit("sigprocmask");
+        err(EXIT_FAILURE, "sigprocmask");
 \&
     exit(EXIT_SUCCESS);
 }
index dfaaddd55706ca22e7287c2033c433b498419349..7f66c743bb52128a339da11ddf085e59c367399f 100644 (file)
@@ -119,13 +119,11 @@ ABC
 .EX
 #define _XOPEN_SOURCE 700
 #include <ctype.h>
+#include <err.h>
 #include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 int
 main(int argc, char *argv[])
 {
@@ -142,11 +140,11 @@ main(int argc, char *argv[])
 \&
     loc = uselocale((locale_t) 0);
     if (loc == (locale_t) 0)
-        errExit("uselocale");
+        err(EXIT_FAILURE, "uselocale");
 \&
     nloc = duplocale(loc);
     if (nloc == (locale_t) 0)
-        errExit("duplocale");
+        err(EXIT_FAILURE, "duplocale");
 \&
     for (char *p = argv[1]; *p; p++)
         putchar(toupper_l(*p, nloc));
index 78c43f153ae9937a02fdfa012aaf664414abe9e8..2e8a33e069d7aa7316be283571d19a83e2970e39 100644 (file)
@@ -311,12 +311,10 @@ Raw address:              c1a80180
 /* Link with "\-lresolv" */
 \&
 #include <arpa/inet.h>
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 int
 main(int argc, char *argv[])
 {
@@ -343,7 +341,7 @@ main(int argc, char *argv[])
 \&
     bits = inet_net_pton(AF_INET, argv[1], &addr, sizeof(addr));
     if (bits == \-1)
-        errExit("inet_net_ntop");
+        err(EXIT_FAILURE, "inet_net_ntop");
 \&
     printf("inet_net_pton() returned: %d\[rs]n", bits);
 \&
@@ -351,7 +349,7 @@ main(int argc, char *argv[])
        returned by inet_net_pton(). */
 \&
     if (inet_net_ntop(AF_INET, &addr, bits, buf, sizeof(buf)) == NULL)
-        errExit("inet_net_ntop");
+        err(EXIT_FAILURE, "inet_net_ntop");
 \&
     printf("inet_net_ntop() yielded:  %s\[rs]n", buf);
 \&
index 1af4828ad52dcb7c7aa8955b5e2e2771a0cd713e..f213cc3158170f5601a328a05ab3460491579761 100644 (file)
@@ -179,22 +179,20 @@ main: exiting
 \&
 .\" SRC BEGIN (makecontext.c)
 .EX
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <ucontext.h>
 \&
 static ucontext_t uctx_main, uctx_func1, uctx_func2;
 \&
-#define handle_error(msg) \[rs]
-    do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static void
 func1(void)
 {
     printf("%s: started\[rs]n", __func__);
     printf("%s: swapcontext(&uctx_func1, &uctx_func2)\[rs]n", __func__);
     if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
-        handle_error("swapcontext");
+        err(EXIT_FAILURE, "swapcontext");
     printf("%s: returning\[rs]n", __func__);
 }
 \&
@@ -204,7 +202,7 @@ func2(void)
     printf("%s: started\[rs]n", __func__);
     printf("%s: swapcontext(&uctx_func2, &uctx_func1)\[rs]n", __func__);
     if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
-        handle_error("swapcontext");
+        err(EXIT_FAILURE, "swapcontext");
     printf("%s: returning\[rs]n", __func__);
 }
 \&
@@ -215,14 +213,14 @@ main(int argc, char *argv[])
     char func2_stack[16384];
 \&
     if (getcontext(&uctx_func1) == \-1)
-        handle_error("getcontext");
+        err(EXIT_FAILURE, "getcontext");
     uctx_func1.uc_stack.ss_sp = func1_stack;
     uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
     uctx_func1.uc_link = &uctx_main;
     makecontext(&uctx_func1, func1, 0);
 \&
     if (getcontext(&uctx_func2) == \-1)
-        handle_error("getcontext");
+        err(EXIT_FAILURE, "getcontext");
     uctx_func2.uc_stack.ss_sp = func2_stack;
     uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
     /* Successor context is f1(), unless argc > 1 */
@@ -231,7 +229,7 @@ main(int argc, char *argv[])
 \&
     printf("%s: swapcontext(&uctx_main, &uctx_func2)\[rs]n", __func__);
     if (swapcontext(&uctx_main, &uctx_func2) == \-1)
-        handle_error("swapcontext");
+        err(EXIT_FAILURE, "swapcontext");
 \&
     printf("%s: exiting\[rs]n", __func__);
     exit(EXIT_SUCCESS);
index 644c83a3a9eaace3e078d805f3268247fc24ae02..9c327477e9ccbd78b161a51b992b1b4ae82dcc6a 100644 (file)
@@ -181,6 +181,7 @@ Linux 3.8.0
 \&
 .\" SRC BEGIN (mq_getattr.c)
 .EX
+#include <err.h>
 #include <fcntl.h>
 #include <mqueue.h>
 #include <stdio.h>
@@ -188,9 +189,6 @@ Linux 3.8.0
 #include <sys/stat.h>
 #include <unistd.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 int
 main(int argc, char *argv[])
 {
@@ -204,16 +202,16 @@ main(int argc, char *argv[])
 \&
     mqd = mq_open(argv[1], O_CREAT | O_EXCL, 0600, NULL);
     if (mqd == (mqd_t) \-1)
-        errExit("mq_open");
+        err(EXIT_FAILURE, "mq_open");
 \&
     if (mq_getattr(mqd, &attr) == \-1)
-        errExit("mq_getattr");
+        err(EXIT_FAILURE, "mq_getattr");
 \&
     printf("Maximum # of messages on queue:   %ld\[rs]n", attr.mq_maxmsg);
     printf("Maximum message size:             %ld\[rs]n", attr.mq_msgsize);
 \&
     if (mq_unlink(argv[1]) == \-1)
-        errExit("mq_unlink");
+        err(EXIT_FAILURE, "mq_unlink");
 \&
     exit(EXIT_SUCCESS);
 }
index 87e5cfe341eff9a6e5c8239f0afe31e5809e6751..cb375a0db099ada1cc7f849268968cfbdc489026 100644 (file)
@@ -201,6 +201,7 @@ queue and then terminates the process.
 .SS Program source
 .\" SRC BEGIN (mq_notify.c)
 .EX
+#include <err.h>
 #include <mqueue.h>
 #include <pthread.h>
 #include <signal.h>
@@ -208,9 +209,6 @@ queue and then terminates the process.
 #include <stdlib.h>
 #include <unistd.h>
 \&
-#define handle_error(msg) \[rs]
-    do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static void                     /* Thread start function */
 tfunc(union sigval sv)
 {
@@ -222,14 +220,14 @@ tfunc(union sigval sv)
     /* Determine max. msg size; allocate buffer to receive msg */
 \&
     if (mq_getattr(mqdes, &attr) == \-1)
-        handle_error("mq_getattr");
+        err(EXIT_FAILURE, "mq_getattr");
     buf = malloc(attr.mq_msgsize);
     if (buf == NULL)
-        handle_error("malloc");
+        err(EXIT_FAILURE, "malloc");
 \&
     nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
     if (nr == \-1)
-        handle_error("mq_receive");
+        err(EXIT_FAILURE, "mq_receive");
 \&
     printf("Read %zd bytes from MQ\[rs]n", nr);
     free(buf);
@@ -249,14 +247,14 @@ main(int argc, char *argv[])
 \&
     mqdes = mq_open(argv[1], O_RDONLY);
     if (mqdes == (mqd_t) \-1)
-        handle_error("mq_open");
+        err(EXIT_FAILURE, "mq_open");
 \&
     sev.sigev_notify = SIGEV_THREAD;
     sev.sigev_notify_function = tfunc;
     sev.sigev_notify_attributes = NULL;
     sev.sigev_value.sival_ptr = &mqdes;   /* Arg. to thread func. */
     if (mq_notify(mqdes, &sev) == \-1)
-        handle_error("mq_notify");
+        err(EXIT_FAILURE, "mq_notify");
 \&
     pause();    /* Process will be terminated by thread function */
 }
index de23c96a58bdcf3f03527d14dc9849ae2f819157..fbf8a1d4d971a6c213b62b8b2f55ca1566c9a282 100644 (file)
@@ -275,14 +275,12 @@ Te Paraire, te 07 o PoutÅ«\-te\-rangi, 2014 00:38:44 CET
 .\" SRC BEGIN (newlocale.c)
 .EX
 #define _XOPEN_SOURCE 700
+#include <err.h>
 #include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 int
 main(int argc, char *argv[])
 {
@@ -302,7 +300,7 @@ main(int argc, char *argv[])
 \&
     loc = newlocale(LC_NUMERIC_MASK, argv[1], (locale_t) 0);
     if (loc == (locale_t) 0)
-        errExit("newlocale");
+        err(EXIT_FAILURE, "newlocale");
 \&
     /* If a second command\-line argument was specified, modify the
        locale object to take the LC_TIME settings from the locale
@@ -313,7 +311,7 @@ main(int argc, char *argv[])
     if (argc > 2) {
         nloc = newlocale(LC_TIME_MASK, argv[2], loc);
         if (nloc == (locale_t) 0)
-            errExit("newlocale");
+            err(EXIT_FAILURE, "newlocale");
         loc = nloc;
     }
 \&
@@ -330,11 +328,11 @@ main(int argc, char *argv[])
     t = time(NULL);
     tm = localtime(&t);
     if (tm == NULL)
-        errExit("time");
+        err(EXIT_FAILURE, "time");
 \&
     s = strftime(buf, sizeof(buf), "%c", tm);
     if (s == 0)
-        errExit("strftime");
+        err(EXIT_FAILURE, "strftime");
 \&
     printf("%s\[rs]n", buf);
 \&
index c4c5b3c96ad987318801e0751d0ff4b8c9ac8fc3..852cb58616cc4230fece93d5a2a9beb11873aa01 100644 (file)
@@ -655,6 +655,7 @@ Child status: exited, status=127
 \&
 .\" SRC BEGIN (posix_spawn.c)
 .EX
+#include <err.h>
 #include <errno.h>
 #include <spawn.h>
 #include <stdint.h>
@@ -664,13 +665,6 @@ Child status: exited, status=127
 #include <unistd.h>
 #include <wait.h>
 \&
-#define errExit(msg)    do { perror(msg); \[rs]
-                             exit(EXIT_FAILURE); } while (0)
-\&
-#define errExitEN(en, msg) \[rs]
-                        do { errno = en; perror(msg); \[rs]
-                             exit(EXIT_FAILURE); } while (0)
-\&
 char **environ;
 \&
 int
@@ -699,12 +693,12 @@ main(int argc, char *argv[])
 \&
             s = posix_spawn_file_actions_init(&file_actions);
             if (s != 0)
-                errExitEN(s, "posix_spawn_file_actions_init");
+                errc(EXIT_FAILURE, s, "posix_spawn_file_actions_init");
 \&
             s = posix_spawn_file_actions_addclose(&file_actions,
                                                   STDOUT_FILENO);
             if (s != 0)
-                errExitEN(s, "posix_spawn_file_actions_addclose");
+                errc(EXIT_FAILURE, s, "posix_spawn_file_actions_addclose");
 \&
             file_actionsp = &file_actions;
             break;
@@ -716,15 +710,15 @@ main(int argc, char *argv[])
 \&
             s = posix_spawnattr_init(&attr);
             if (s != 0)
-                errExitEN(s, "posix_spawnattr_init");
+                errc(EXIT_FAILURE, s, "posix_spawnattr_init");
             s = posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGMASK);
             if (s != 0)
-                errExitEN(s, "posix_spawnattr_setflags");
+                errc(EXIT_FAILURE, s, "posix_spawnattr_setflags");
 \&
             sigfillset(&mask);
             s = posix_spawnattr_setsigmask(&attr, &mask);
             if (s != 0)
-                errExitEN(s, "posix_spawnattr_setsigmask");
+                errc(EXIT_FAILURE, s, "posix_spawnattr_setsigmask");
 \&
             attrp = &attr;
             break;
@@ -744,20 +738,20 @@ main(int argc, char *argv[])
     s = posix_spawnp(&child_pid, argv[optind], file_actionsp, attrp,
                      &argv[optind], environ);
     if (s != 0)
-        errExitEN(s, "posix_spawn");
+        errc(EXIT_FAILURE, s, "posix_spawn");
 \&
     /* Destroy any objects that we created earlier. */
 \&
     if (attrp != NULL) {
         s = posix_spawnattr_destroy(attrp);
         if (s != 0)
-            errExitEN(s, "posix_spawnattr_destroy");
+            errc(EXIT_FAILURE, s, "posix_spawnattr_destroy");
     }
 \&
     if (file_actionsp != NULL) {
         s = posix_spawn_file_actions_destroy(file_actionsp);
         if (s != 0)
-            errExitEN(s, "posix_spawn_file_actions_destroy");
+            errc(EXIT_FAILURE, s, "posix_spawn_file_actions_destroy");
     }
 \&
     printf("PID of child: %jd\[rs]n", (intmax_t) child_pid);
@@ -767,7 +761,7 @@ main(int argc, char *argv[])
     do {
         s = waitpid(child_pid, &status, WUNTRACED | WCONTINUED);
         if (s == \-1)
-            errExit("waitpid");
+            err(EXIT_FAILURE, "waitpid");
 \&
         printf("Child status: ");
         if (WIFEXITED(status)) {
index d14a3059548cda46c56ef9ed58c7225ee077b9bd..c89c8ca483c0628dfdcdf09715c9531e65f0e43a 100644 (file)
@@ -148,15 +148,13 @@ main(): thread was canceled
 \&
 .\" SRC BEGIN (pthread_cancel.c)
 .EX
+#include <err.h>
 #include <errno.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 \&
-#define handle_error_en(en, msg) \[rs]
-        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static void *
 thread_func(void *ignored_argument)
 {
@@ -167,7 +165,7 @@ thread_func(void *ignored_argument)
 \&
     s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_setcancelstate");
+        errc(EXIT_FAILURE, s, "pthread_setcancelstate");
 \&
     printf("%s(): started; cancelation disabled\[rs]n", __func__);
     sleep(5);
@@ -175,7 +173,7 @@ thread_func(void *ignored_argument)
 \&
     s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_setcancelstate");
+        errc(EXIT_FAILURE, s, "pthread_setcancelstate");
 \&
     /* sleep() is a cancelation point. */
 \&
@@ -198,20 +196,20 @@ main(void)
 \&
     s = pthread_create(&thr, NULL, &thread_func, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_create");
+        errc(EXIT_FAILURE, s, "pthread_create");
 \&
     sleep(2);           /* Give thread a chance to get started */
 \&
     printf("%s(): sending cancelation request\[rs]n", __func__);
     s = pthread_cancel(thr);
     if (s != 0)
-        handle_error_en(s, "pthread_cancel");
+        errc(EXIT_FAILURE, s, "pthread_cancel");
 \&
     /* Join with thread to see what its exit status was. */
 \&
     s = pthread_join(thr, &res);
     if (s != 0)
-        handle_error_en(s, "pthread_join");
+        errc(EXIT_FAILURE, s, "pthread_join");
 \&
     if (res == PTHREAD_CANCELED)
         printf("%s(): thread was canceled\[rs]n", __func__);
index 8553ec7bc48fa2338abb446a5332c0f314a49d1d..0f5137986b71d809e578aa6cacd619ee911a1ff9 100644 (file)
@@ -236,6 +236,7 @@ was nonzero.
 \&
 .\" SRC BEGIN (pthread_cleanup_push.c)
 .EX
+#include <err.h>
 #include <errno.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -243,9 +244,6 @@ was nonzero.
 #include <sys/types.h>
 #include <unistd.h>
 \&
-#define handle_error_en(en, msg) \[rs]
-        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static int done = 0;
 static int cleanup_pop_arg = 0;
 static int cnt = 0;
@@ -290,7 +288,7 @@ main(int argc, char *argv[])
 \&
     s = pthread_create(&thr, NULL, thread_start, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_create");
+        errc(EXIT_FAILURE, s, "pthread_create");
 \&
     sleep(2);           /* Allow new thread to run a while */
 \&
@@ -303,12 +301,12 @@ main(int argc, char *argv[])
         printf("Canceling thread\[rs]n");
         s = pthread_cancel(thr);
         if (s != 0)
-            handle_error_en(s, "pthread_cancel");
+            errc(EXIT_FAILURE, s, "pthread_cancel");
     }
 \&
     s = pthread_join(thr, &res);
     if (s != 0)
-        handle_error_en(s, "pthread_join");
+        errc(EXIT_FAILURE, s, "pthread_join");
 \&
     if (res == PTHREAD_CANCELED)
         printf("Thread was canceled; cnt = %d\[rs]n", cnt);
index 1aee9c06fac26d72020cb0bea1e4d54f5160fba9..8c9f58edd138b9f2e3232e27ebd7ddddb6e03825 100644 (file)
@@ -251,6 +251,7 @@ Joined with thread 3; returned value was SERVUS
 .\" SRC BEGIN (pthread_create.c)
 .EX
 #include <ctype.h>
+#include <err.h>
 #include <errno.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -259,12 +260,6 @@ Joined with thread 3; returned value was SERVUS
 #include <sys/types.h>
 #include <unistd.h>
 \&
-#define handle_error_en(en, msg) \[rs]
-        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
-#define handle_error(msg) \[rs]
-        do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 struct thread_info {    /* Used as argument to thread_start() */
     pthread_t thread_id;        /* ID returned by pthread_create() */
     int       thread_num;       /* Application\-defined thread # */
@@ -285,7 +280,7 @@ thread_start(void *arg)
 \&
     uargv = strdup(tinfo\->argv_string);
     if (uargv == NULL)
-        handle_error("strdup");
+        err(EXIT_FAILURE, "strdup");
 \&
     for (char *p = uargv; *p != \[aq]\[rs]0\[aq]; p++)
         *p = toupper(*p);
@@ -325,19 +320,19 @@ main(int argc, char *argv[])
 \&
     s = pthread_attr_init(&attr);
     if (s != 0)
-        handle_error_en(s, "pthread_attr_init");
+        errc(EXIT_FAILURE, s, "pthread_attr_init");
 \&
     if (stack_size > 0) {
         s = pthread_attr_setstacksize(&attr, stack_size);
         if (s != 0)
-            handle_error_en(s, "pthread_attr_setstacksize");
+            errc(EXIT_FAILURE, s, "pthread_attr_setstacksize");
     }
 \&
     /* Allocate memory for pthread_create() arguments. */
 \&
     tinfo = calloc(num_threads, sizeof(*tinfo));
     if (tinfo == NULL)
-        handle_error("calloc");
+        err(EXIT_FAILURE, "calloc");
 \&
     /* Create one thread for each command\-line argument. */
 \&
@@ -351,7 +346,7 @@ main(int argc, char *argv[])
         s = pthread_create(&tinfo[tnum].thread_id, &attr,
                            &thread_start, &tinfo[tnum]);
         if (s != 0)
-            handle_error_en(s, "pthread_create");
+            errc(EXIT_FAILURE, s, "pthread_create");
     }
 \&
     /* Destroy the thread attributes object, since it is no
@@ -359,14 +354,14 @@ main(int argc, char *argv[])
 \&
     s = pthread_attr_destroy(&attr);
     if (s != 0)
-        handle_error_en(s, "pthread_attr_destroy");
+        errc(EXIT_FAILURE, s, "pthread_attr_destroy");
 \&
     /* Now join with each thread, and display its returned value. */
 \&
     for (size_t tnum = 0; tnum < num_threads; tnum++) {
         s = pthread_join(tinfo[tnum].thread_id, &res);
         if (s != 0)
-            handle_error_en(s, "pthread_join");
+            errc(EXIT_FAILURE, s, "pthread_join");
 \&
         printf("Joined with thread %d; returned value was %s\[rs]n",
                tinfo[tnum].thread_num, (char *) res);
index 9de7dfb41397b0fa5845ce73b2dbfc926f9cb2c0..5a4785ef46caf6a385076531eb48bebcd36b7dc7 100644 (file)
@@ -98,6 +98,7 @@ Subthread CPU time:        0.992
 .EX
 /* Link with "\-lrt" */
 \&
+#include <err.h>
 #include <errno.h>
 #include <pthread.h>
 #include <stdint.h>
@@ -108,12 +109,6 @@ Subthread CPU time:        0.992
 #include <time.h>
 #include <unistd.h>
 \&
-#define handle_error(msg) \[rs]
-        do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
-#define handle_error_en(en, msg) \[rs]
-        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static void *
 thread_start(void *arg)
 {
@@ -129,7 +124,7 @@ pclock(char *msg, clockid_t cid)
 \&
     printf("%s", msg);
     if (clock_gettime(cid, &ts) == \-1)
-        handle_error("clock_gettime");
+        err(EXIT_FAILURE, "clock_gettime");
     printf("%4jd.%03ld\[rs]n", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
 }
 \&
@@ -142,7 +137,7 @@ main(void)
 \&
     s = pthread_create(&thread, NULL, thread_start, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_create");
+        errc(EXIT_FAILURE, s, "pthread_create");
 \&
     printf("Main thread sleeping\[rs]n");
     sleep(1);
@@ -155,7 +150,7 @@ main(void)
 \&
     s = pthread_getcpuclockid(pthread_self(), &cid);
     if (s != 0)
-        handle_error_en(s, "pthread_getcpuclockid");
+        errc(EXIT_FAILURE, s, "pthread_getcpuclockid");
     pclock("Main thread CPU time:   ", cid);
 \&
     /* The preceding 4 lines of code could have been replaced by:
@@ -163,7 +158,7 @@ main(void)
 \&
     s = pthread_getcpuclockid(thread, &cid);
     if (s != 0)
-        handle_error_en(s, "pthread_getcpuclockid");
+        errc(EXIT_FAILURE, s, "pthread_getcpuclockid");
     pclock("Subthread CPU time: 1    ", cid);
 \&
     exit(EXIT_SUCCESS);         /* Terminates both threads */
index 6110b54c75644468ef8a4148da423e646bf1cabc..3dfbb357b1380987ee465f4a51406f1ded1bcbb9 100644 (file)
@@ -189,15 +189,13 @@ The following shell session shows what we see when running this program:
 .SS Program source
 .\" SRC BEGIN (pthread_mutexattr_setrobust.c)
 .EX
+#include <err.h>
 #include <errno.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 \&
-#define handle_error_en(en, msg) \[rs]
-        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static pthread_mutex_t mtx;
 \&
 static void *
@@ -235,11 +233,11 @@ main(void)
         printf("[main] Now make the mutex consistent\[rs]n");
         s = pthread_mutex_consistent(&mtx);
         if (s != 0)
-            handle_error_en(s, "pthread_mutex_consistent");
+            errc(EXIT_FAILURE, s, "pthread_mutex_consistent");
         printf("[main] Mutex is now consistent; unlocking\[rs]n");
         s = pthread_mutex_unlock(&mtx);
         if (s != 0)
-            handle_error_en(s, "pthread_mutex_unlock");
+            errc(EXIT_FAILURE, s, "pthread_mutex_unlock");
 \&
         exit(EXIT_SUCCESS);
     } else if (s == 0) {
@@ -247,7 +245,7 @@ main(void)
         exit(EXIT_FAILURE);
     } else {
         printf("[main] pthread_mutex_lock() unexpectedly failed\[rs]n");
-        handle_error_en(s, "pthread_mutex_lock");
+        errc(EXIT_FAILURE, s, "pthread_mutex_lock");
     }
 }
 .EE
index 37c8cb081cd7e414bc332e1142773be2c86b4db6..c16560e7ba67e8f6c1244c3f0ffa61af58abd91b 100644 (file)
@@ -224,15 +224,13 @@ is the default for the inherit scheduler attribute.
 .EX
 /* pthreads_sched_test.c */
 \&
+#include <err.h>
 #include <errno.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 \&
-#define handle_error_en(en, msg) \[rs]
-        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 [[noreturn]]
 static void
 usage(char *prog_name, char *msg)
@@ -287,7 +285,7 @@ display_thread_sched_attr(char *msg)
 \&
     s = pthread_getschedparam(pthread_self(), &policy, &param);
     if (s != 0)
-        handle_error_en(s, "pthread_getschedparam");
+        errc(EXIT_FAILURE, s, "pthread_getschedparam");
 \&
     printf("%s\[rs]n", msg);
     display_sched_attr(policy, &param);
@@ -344,7 +342,7 @@ main(int argc, char *argv[])
 \&
         s = pthread_setschedparam(pthread_self(), policy, &param);
         if (s != 0)
-            handle_error_en(s, "pthread_setschedparam");
+            errc(EXIT_FAILURE, s, "pthread_setschedparam");
     }
 \&
     display_thread_sched_attr("Scheduler settings of main thread");
@@ -357,7 +355,7 @@ main(int argc, char *argv[])
     if (!use_null_attrib) {
         s = pthread_attr_init(&attr);
         if (s != 0)
-            handle_error_en(s, "pthread_attr_init");
+            errc(EXIT_FAILURE, s, "pthread_attr_init");
         attrp = &attr;
     }
 \&
@@ -371,7 +369,7 @@ main(int argc, char *argv[])
 \&
         s = pthread_attr_setinheritsched(&attr, inheritsched);
         if (s != 0)
-            handle_error_en(s, "pthread_attr_setinheritsched");
+            errc(EXIT_FAILURE, s, "pthread_attr_setinheritsched");
     }
 \&
     if (attr_sched_str != NULL) {
@@ -381,10 +379,10 @@ main(int argc, char *argv[])
 \&
         s = pthread_attr_setschedpolicy(&attr, policy);
         if (s != 0)
-            handle_error_en(s, "pthread_attr_setschedpolicy");
+            errc(EXIT_FAILURE, s, "pthread_attr_setschedpolicy");
         s = pthread_attr_setschedparam(&attr, &param);
         if (s != 0)
-            handle_error_en(s, "pthread_attr_setschedparam");
+            errc(EXIT_FAILURE, s, "pthread_attr_setschedparam");
     }
 \&
     /* If we initialized a thread attributes object, display
@@ -393,10 +391,10 @@ main(int argc, char *argv[])
     if (attrp != NULL) {
         s = pthread_attr_getschedparam(&attr, &param);
         if (s != 0)
-            handle_error_en(s, "pthread_attr_getschedparam");
+            errc(EXIT_FAILURE, s, "pthread_attr_getschedparam");
         s = pthread_attr_getschedpolicy(&attr, &policy);
         if (s != 0)
-            handle_error_en(s, "pthread_attr_getschedpolicy");
+            errc(EXIT_FAILURE, s, "pthread_attr_getschedpolicy");
 \&
         printf("Scheduler settings in \[aq]attr\[aq]\[rs]n");
         display_sched_attr(policy, &param);
@@ -413,19 +411,19 @@ main(int argc, char *argv[])
 \&
     s = pthread_create(&thread, attrp, &thread_start, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_create");
+        errc(EXIT_FAILURE, s, "pthread_create");
 \&
     /* Destroy unneeded thread attributes object. */
 \&
     if (!use_null_attrib) {
       s = pthread_attr_destroy(&attr);
       if (s != 0)
-          handle_error_en(s, "pthread_attr_destroy");
+          errc(EXIT_FAILURE, s, "pthread_attr_destroy");
     }
 \&
     s = pthread_join(thread, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_join");
+        errc(EXIT_FAILURE, s, "pthread_join");
 \&
     exit(EXIT_SUCCESS);
 }
index 2175e3a97d287a69a99be26546afd6ef1f581499..bf9ad38008839be31b93ef7faf79cda865488963 100644 (file)
@@ -95,6 +95,7 @@ Signal handling thread got signal 10
 \&
 .\" SRC BEGIN (pthread_sigmask.c)
 .EX
+#include <err.h>
 #include <errno.h>
 #include <pthread.h>
 #include <signal.h>
@@ -104,9 +105,6 @@ Signal handling thread got signal 10
 \&
 /* Simple error handling functions */
 \&
-#define handle_error_en(en, msg) \[rs]
-        do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static void *
 sig_thread(void *arg)
 {
@@ -116,7 +114,7 @@ sig_thread(void *arg)
     for (;;) {
         s = sigwait(set, &sig);
         if (s != 0)
-            handle_error_en(s, "sigwait");
+            errc(EXIT_FAILURE, s, "sigwait");
         printf("Signal handling thread got signal %d\[rs]n", sig);
     }
 }
@@ -136,11 +134,11 @@ main(void)
     sigaddset(&set, SIGUSR1);
     s = pthread_sigmask(SIG_BLOCK, &set, NULL);
     if (s != 0)
-        handle_error_en(s, "pthread_sigmask");
+        errc(EXIT_FAILURE, s, "pthread_sigmask");
 \&
     s = pthread_create(&thread, NULL, &sig_thread, &set);
     if (s != 0)
-        handle_error_en(s, "pthread_create");
+        errc(EXIT_FAILURE, s, "pthread_create");
 \&
     /* Main thread carries on to create other threads and/or do
        other work. */
index 2aa68eb3453e9a8fcea3549506656e51c193540a..8b75ea59a69867466e34440f6a474ad452bf96c5 100644 (file)
@@ -165,6 +165,7 @@ sem_timedwait() timed out
 \&
 .\" SRC BEGIN (sem_wait.c)
 .EX
+#include <err.h>
 #include <errno.h>
 #include <semaphore.h>
 #include <signal.h>
@@ -177,9 +178,6 @@ sem_timedwait() timed out
 \&
 sem_t sem;
 \&
-#define handle_error(msg) \[rs]
-    do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 static void
 handler(int sig)
 {
@@ -204,7 +202,7 @@ main(int argc, char *argv[])
     }
 \&
     if (sem_init(&sem, 0, 0) == \-1)
-        handle_error("sem_init");
+        err(EXIT_FAILURE, "sem_init");
 \&
     /* Establish SIGALRM handler; set alarm timer using argv[1]. */
 \&
@@ -212,7 +210,7 @@ main(int argc, char *argv[])
     sigemptyset(&sa.sa_mask);
     sa.sa_flags = 0;
     if (sigaction(SIGALRM, &sa, NULL) == \-1)
-        handle_error("sigaction");
+        err(EXIT_FAILURE, "sigaction");
 \&
     alarm(atoi(argv[1]));
 \&
@@ -220,7 +218,7 @@ main(int argc, char *argv[])
        number of seconds given argv[2]. */
 \&
     if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
-        handle_error("clock_gettime");
+        err(EXIT_FAILURE, "clock_gettime");
 \&
     ts.tv_sec += atoi(argv[2]);
 \&
index db703e1e4ed98908ea6c9c07a38c00a859e7c5d4..0ba63c870e23673da15ad52d62960b7090acba00 100644 (file)
@@ -293,14 +293,12 @@ on the memory object that is shared between the two programs.
 #ifndef PSHM_UCASE_H
 #define PSHM_UCASE_H
 \&
+#include <err.h>
 #include <semaphore.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 #define BUF_SIZE 1024   /* Maximum size for exchanged string */
 \&
 /* Define a structure that will be imposed on the shared
@@ -367,30 +365,30 @@ main(int argc, char *argv[])
 \&
     fd = shm_open(shmpath, O_CREAT | O_EXCL | O_RDWR, 0600);
     if (fd == \-1)
-        errExit("shm_open");
+        err(EXIT_FAILURE, "shm_open");
 \&
     if (ftruncate(fd, sizeof(struct shmbuf)) == \-1)
-        errExit("ftruncate");
+        err(EXIT_FAILURE, "ftruncate");
 \&
     /* Map the object into the caller\[aq]s address space. */
 \&
     shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
                 MAP_SHARED, fd, 0);
     if (shmp == MAP_FAILED)
-        errExit("mmap");
+        err(EXIT_FAILURE, "mmap");
 \&
     /* Initialize semaphores as process\-shared, with value 0. */
 \&
     if (sem_init(&shmp\->sem1, 1, 0) == \-1)
-        errExit("sem_init\-sem1");
+        err(EXIT_FAILURE, "sem_init\-sem1");
     if (sem_init(&shmp\->sem2, 1, 0) == \-1)
-        errExit("sem_init\-sem2");
+        err(EXIT_FAILURE, "sem_init\-sem2");
 \&
     /* Wait for \[aq]sem1\[aq] to be posted by peer before touching
        shared memory. */
 \&
     if (sem_wait(&shmp\->sem1) == \-1)
-        errExit("sem_wait");
+        err(EXIT_FAILURE, "sem_wait");
 \&
     /* Convert data in shared memory into upper case. */
 \&
@@ -401,7 +399,7 @@ main(int argc, char *argv[])
        access the modified data in shared memory. */
 \&
     if (sem_post(&shmp\->sem2) == \-1)
-        errExit("sem_post");
+        err(EXIT_FAILURE, "sem_post");
 \&
     /* Unlink the shared memory object. Even if the peer process
        is still using the object, this is okay. The object will
@@ -474,12 +472,12 @@ main(int argc, char *argv[])
 \&
     fd = shm_open(shmpath, O_RDWR, 0);
     if (fd == \-1)
-        errExit("shm_open");
+        err(EXIT_FAILURE, "shm_open");
 \&
     shmp = mmap(NULL, sizeof(*shmp), PROT_READ | PROT_WRITE,
                 MAP_SHARED, fd, 0);
     if (shmp == MAP_FAILED)
-        errExit("mmap");
+        err(EXIT_FAILURE, "mmap");
 \&
     /* Copy data into the shared memory object. */
 \&
@@ -489,20 +487,20 @@ main(int argc, char *argv[])
     /* Tell peer that it can now access shared memory. */
 \&
     if (sem_post(&shmp\->sem1) == \-1)
-        errExit("sem_post");
+        err(EXIT_FAILURE, "sem_post");
 \&
     /* Wait until peer says that it has finished accessing
        the shared memory. */
 \&
     if (sem_wait(&shmp\->sem2) == \-1)
-        errExit("sem_wait");
+        err(EXIT_FAILURE, "sem_wait");
 \&
     /* Write modified data in shared memory to standard output. */
 \&
     if (write(STDOUT_FILENO, &shmp\->buf, len) == \-1)
-        errExit("write");
+        err(EXIT_FAILURE, "write");
     if (write(STDOUT_FILENO, "\[rs]n", 1) == \-1)
-        errExit("write");
+        err(EXIT_FAILURE, "write");
 \&
     exit(EXIT_SUCCESS);
 }
index 8997a9aca013603a7f8556f746e473a1e91991d0..a497ab039956f6d5bf59fb869a0bcb2c97670418 100644 (file)
@@ -308,6 +308,7 @@ loopname = /dev/loop5
 .SS Program source
 \&
 .EX
+#include <err.h>
 #include <fcntl.h>
 #include <linux/loop.h>
 #include <sys/ioctl.h>
@@ -315,9 +316,6 @@ loopname = /dev/loop5
 #include <stdlib.h>
 #include <unistd.h>
 \&
-#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \[rs]
-                        } while (0)
-\&
 int
 main(int argc, char *argv[])
 {
@@ -332,25 +330,25 @@ main(int argc, char *argv[])
 \&
     loopctlfd = open("/dev/loop\-control", O_RDWR);
     if (loopctlfd == \-1)
-        errExit("open: /dev/loop\-control");
+        err(EXIT_FAILURE, "open: /dev/loop\-control");
 \&
     devnr = ioctl(loopctlfd, LOOP_CTL_GET_FREE);
     if (devnr == \-1)
-        errExit("ioctl\-LOOP_CTL_GET_FREE");
+        err(EXIT_FAILURE, "ioctl\-LOOP_CTL_GET_FREE");
 \&
     sprintf(loopname, "/dev/loop%ld", devnr);
     printf("loopname = %s\[rs]n", loopname);
 \&
     loopfd = open(loopname, O_RDWR);
     if (loopfd == \-1)
-        errExit("open: loopname");
+        err(EXIT_FAILURE, "open: loopname");
 \&
     backingfile = open(argv[1], O_RDWR);
     if (backingfile == \-1)
-        errExit("open: backing\-file");
+        err(EXIT_FAILURE, "open: backing\-file");
 \&
     if (ioctl(loopfd, LOOP_SET_FD, backingfile) == \-1)
-        errExit("ioctl\-LOOP_SET_FD");
+        err(EXIT_FAILURE, "ioctl\-LOOP_SET_FD");
 \&
     exit(EXIT_SUCCESS);
 }
index 931247a580b9d12f2254f8b1ac90d24ca616776c..a3937206bf5803448679d32d2ba140b5b14bd62e 100644 (file)
@@ -226,6 +226,7 @@ aio_return():
 .SS Program source
 \&
 .EX
+#include <err.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -236,8 +237,6 @@ aio_return():
 \&
 #define BUF_SIZE 20     /* Size of buffers for read operations */
 \&
-#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
-\&
 struct ioRequest {      /* Application\-defined structure for tracking
                            I/O requests */
     int           reqNum;
@@ -290,11 +289,11 @@ main(int argc, char *argv[])
 \&
     struct ioRequest *ioList = calloc(numReqs, sizeof(*ioList));
     if (ioList == NULL)
-        errExit("calloc");
+        err(EXIT_FAILURE, "calloc");
 \&
     struct aiocb *aiocbList = calloc(numReqs, sizeof(*aiocbList));
     if (aiocbList == NULL)
-        errExit("calloc");
+        err(EXIT_FAILURE, "calloc");
 \&
     /* Establish handlers for SIGQUIT and the I/O completion signal. */
 \&
@@ -303,12 +302,12 @@ main(int argc, char *argv[])
 \&
     sa.sa_handler = quitHandler;
     if (sigaction(SIGQUIT, &sa, NULL) == \-1)
-        errExit("sigaction");
+        err(EXIT_FAILURE, "sigaction");
 \&
     sa.sa_flags = SA_RESTART | SA_SIGINFO;
     sa.sa_sigaction = aioSigHandler;
     if (sigaction(IO_SIGNAL, &sa, NULL) == \-1)
-        errExit("sigaction");
+        err(EXIT_FAILURE, "sigaction");
 \&
     /* Open each file specified on the command line, and queue
        a read request on the resulting file descriptor. */
@@ -320,13 +319,13 @@ main(int argc, char *argv[])
 \&
         ioList[j].aiocbp\->aio_fildes = open(argv[j + 1], O_RDONLY);
         if (ioList[j].aiocbp\->aio_fildes == \-1)
-            errExit("open");
+            err(EXIT_FAILURE, "open");
         printf("opened %s on descriptor %d\[rs]n", argv[j + 1],
                 ioList[j].aiocbp\->aio_fildes);
 \&
         ioList[j].aiocbp\->aio_buf = malloc(BUF_SIZE);
         if (ioList[j].aiocbp\->aio_buf == NULL)
-            errExit("malloc");
+            err(EXIT_FAILURE, "malloc");
 \&
         ioList[j].aiocbp\->aio_nbytes = BUF_SIZE;
         ioList[j].aiocbp\->aio_reqprio = 0;
@@ -338,7 +337,7 @@ main(int argc, char *argv[])
 \&
         s = aio_read(ioList[j].aiocbp);
         if (s == \-1)
-            errExit("aio_read");
+            err(EXIT_FAILURE, "aio_read");
     }
 \&
     openReqs = numReqs;