1 /*
2 * $Id: error.c,v 1.5 2003/08/23 12:37:42 sakari Exp $
3 *
4 * Error and wrapper functions.
5 *
6 * Copyright (C) 2002 Sakari Lehtonen <sakari@ionstream.fi>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33
34 #include "error.h"
35
36 /* internal error handler */
error_handler(int strflag,const char * fmt,va_list ap)37 static void error_handler(int strflag, const char *fmt, va_list ap)
38 {
39 int n;
40 int errno_save;
41 char buf[256];
42
43 errno_save = errno;
44
45 fprintf(stderr, "%s: ", program_name);
46 vsnprintf(buf, sizeof(buf), fmt, ap);
47 n = strlen(buf);
48 /* only print errno if it is specified and not 0 */
49 if (strflag == 1 && errno_save != 0)
50 snprintf(buf+n, sizeof(buf) - n, ": %s", strerror(errno_save));
51 strcat(buf, "\n");
52 fflush(stdout);
53 fputs(buf, stderr);
54 fflush(stderr);
55 }
56
eioctl(int fd,unsigned long request,void * arg)57 int eioctl(int fd, unsigned long request, void *arg)
58 {
59 int n;
60
61 if ((n = ioctl(fd, request, arg)) == -1)
62 err_msg("ioctl request %d failed: %s", request,
63 strerror(errno));
64
65 return n;
66 }
67
emalloc(size_t size)68 void *emalloc(size_t size)
69 {
70 void *ptr;
71
72 if ((ptr = malloc(size)) == NULL)
73 err_quit("malloc of %d bytes failed", size);
74
75 return ptr;
76 }
77
erealloc(void * ptr,size_t size)78 void *erealloc(void *ptr, size_t size)
79 {
80 void *reptr;
81
82 if ((reptr = realloc(ptr, size)) == NULL)
83 err_quit("realloc of %d bytes failed", size);
84
85 return reptr;
86 }
87
estrdup(const char * str)88 char *estrdup(const char *str)
89 {
90 char *ptr;
91
92 if ((ptr = strdup(str)) == NULL)
93 err_quit("strdup \"%s\" failed");
94
95 return ptr;
96 }
97
eclose(int fd)98 void eclose(int fd)
99 {
100 if (close(fd) == -1)
101 err_quit("close of fd %d failed", fd);
102 }
103
eopen(const char * path,int flags,mode_t mode)104 int eopen(const char *path, int flags, mode_t mode)
105 {
106 int fd;
107
108 if ((fd = open(path, flags, mode)) == -1)
109 fd = -1;
110
111 return fd;
112 }
113
114 /* nonfatal, print message and return */
err_msg(const char * fmt,...)115 void err_msg(const char *fmt, ...)
116 {
117 va_list ap;
118
119 va_start(ap, fmt);
120 error_handler(0, fmt, ap);
121 va_end(ap);
122 }
123
124 /* nonfatal, print message with strerror(errno) and return */
err_str(const char * fmt,...)125 void err_str(const char *fmt, ...)
126 {
127 va_list ap;
128
129 va_start(ap, fmt);
130 error_handler(1, fmt, ap);
131 va_end(ap);
132 }
133
134 /* fatal, print message and exit */
err_quit(const char * fmt,...)135 void err_quit(const char *fmt, ...)
136 {
137 va_list ap;
138
139 va_start(ap, fmt);
140 error_handler(0, fmt, ap);
141 va_end(ap);
142
143 exit(EXIT_FAILURE);
144 }
145