diff options
| author | Alejandro Colomar <alx@kernel.org> | 2025-08-19 17:48:50 +0200 |
|---|---|---|
| committer | Alejandro Colomar <alx@kernel.org> | 2025-08-20 18:14:03 +0200 |
| commit | 0e7a39804a3c017a209117fc2243c6cbb543dede (patch) | |
| tree | 91e0b287d8b826d668c3d8118347f07cc8b8964a /man/man3/shm_open.3 | |
| parent | e2d3f14fe40ad90a1fedf0fcd27e6cc896c49a7a (diff) | |
| download | man-pages-0e7a39804a3c.tar.gz | |
man/: EXAMPLES: Use err(3) and errc(3bsd) instead of similar macros
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>
Diffstat (limited to 'man/man3/shm_open.3')
| -rw-r--r-- | man/man3/shm_open.3 | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/man/man3/shm_open.3 b/man/man3/shm_open.3 index db703e1e4e..0ba63c870e 100644 --- a/man/man3/shm_open.3 +++ b/man/man3/shm_open.3 @@ -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); } |
