mirror of https://github.com/mkerrisk/man-pages
Handle errors using a custom handle_errror() macro.
This commit is contained in:
parent
6a578b8823
commit
4407d3d81e
27
man2/mmap.2
27
man2/mmap.2
|
@ -513,6 +513,9 @@ to output the desired bytes.
|
|||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define handle_error(msg) \\
|
||||
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -529,15 +532,11 @@ main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
if (fd == \-1) {
|
||||
perror("open");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (fd == \-1)
|
||||
handle_error("open");
|
||||
|
||||
if (fstat(fd, &sb) == \-1) { /* To obtain file size */
|
||||
perror("fstat");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (fstat(fd, &sb) == \-1) /* To obtain file size */
|
||||
handle_error("fstat");
|
||||
|
||||
offset = atoi(argv[2]);
|
||||
pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) \- 1);
|
||||
|
@ -560,17 +559,15 @@ main(int argc, char *argv[])
|
|||
|
||||
addr = mmap(NULL, length + offset \- pa_offset, PROT_READ,
|
||||
MAP_PRIVATE, fd, pa_offset);
|
||||
if (addr == MAP_FAILED) {
|
||||
perror("mmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (addr == MAP_FAILED)
|
||||
handle_error("mmap");
|
||||
|
||||
s = write(STDOUT_FILENO, addr + offset \- pa_offset, length);
|
||||
if (s != length) {
|
||||
if (s == \-1)
|
||||
perror("write");
|
||||
else
|
||||
fprintf(stderr, "partial write");
|
||||
handle_error("write");
|
||||
|
||||
fprintf(stderr, "partial write");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
|
|
@ -191,6 +191,9 @@ The source code of the program is as follows:
|
|||
|
||||
sem_t sem;
|
||||
|
||||
#define handle_error(msg) \\
|
||||
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
||||
|
||||
static void
|
||||
handler(int sig)
|
||||
{
|
||||
|
@ -214,30 +217,25 @@ main(int argc, char *argv[])
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (sem_init(&sem, 0, 0) == \-1) {
|
||||
perror("sem_init");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (sem_init(&sem, 0, 0) == \-1)
|
||||
handle_error("sem_init");
|
||||
|
||||
/* Establish SIGALRM handler; set alarm timer using argv[1] */
|
||||
|
||||
sa.sa_handler = handler;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sa.sa_flags = 0;
|
||||
if (sigaction(SIGALRM, &sa, NULL) == \-1) {
|
||||
perror("sigaction");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (sigaction(SIGALRM, &sa, NULL) == \-1)
|
||||
handle_error("sigaction");
|
||||
|
||||
alarm(atoi(argv[1]));
|
||||
|
||||
/* Calculate relative interval as current time plus
|
||||
number of seconds given argv[2] */
|
||||
|
||||
if (clock_gettime(CLOCK_REALTIME, &ts) == \-1) {
|
||||
perror("clock_gettime");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
|
||||
handle_error("clock_gettime");
|
||||
|
||||
ts.tv_sec += atoi(argv[2]);
|
||||
|
||||
printf("main() about to call sem_timedwait()\\n");
|
||||
|
|
Loading…
Reference in New Issue