xref: /plan9/sys/src/ape/cmd/pax/warn.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 /* $Source: /u/mark/src/pax/RCS/warn.c,v $
2  *
3  * $Revision: 1.2 $
4  *
5  * warn.c - miscellaneous user warning routines
6  *
7  * DESCRIPTION
8  *
9  *	These routines provide the user with various forms of warning
10  *	and informational messages.
11  *
12  * AUTHOR
13  *
14  *     Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
15  *
16  * Sponsored by The USENIX Association for public distribution.
17  *
18  * Copyright (c) 1989 Mark H. Colburn.
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms are permitted
22  * provided that the above copyright notice is duplicated in all such
23  * forms and that any documentation, advertising materials, and other
24  * materials related to such distribution and use acknowledge that the
25  * software was developed * by Mark H. Colburn and sponsored by The
26  * USENIX Association.
27  *
28  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31  *
32  * $Log:	warn.c,v $
33  * Revision 1.2  89/02/12  10:06:15  mark
34  * 1.2 release fixes
35  *
36  * Revision 1.1  88/12/23  18:02:40  mark
37  * Initial revision
38  *
39  */
40 
41 #ifndef lint
42 static char *ident = "$Id: warn.c,v 1.2 89/02/12 10:06:15 mark Exp $";
43 static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
44 #endif /* ! lint */
45 
46 
47 /* Headers */
48 
49 #include "pax.h"
50 
51 
52 /* Function Prototypes */
53 
54 #ifdef __STDC__
55 
56 static void prsize(FILE *, OFFSET);
57 
58 #else /* !__STDC__ */
59 
60 static void prsize();
61 
62 #endif /* __STDC__ */
63 
64 
65 /* warnarch - print an archive-related warning message and offset
66  *
67  * DESCRIPTION
68  *
69  *	Present the user with an error message and an archive offset at
70  *	which the error occured.   This can be useful for diagnosing or
71  *	fixing damaged archives.
72  *
73  * PARAMETERS
74  *
75  *	char 	*msg	- A message string to be printed for the user.
76  *	OFFSET 	adjust	- An adjustment which is added to the current
77  *			  archive position to tell the user exactly where
78  *			  the error occurred.
79  */
80 
81 #ifdef __STDC__
82 
warnarch(char * msg,OFFSET adjust)83 void warnarch(char *msg, OFFSET adjust)
84 
85 #else
86 
87 void warnarch(msg, adjust)
88 char           *msg;
89 OFFSET          adjust;
90 
91 #endif
92 {
93     fprintf(stderr, "%s: [offset ", myname);
94     prsize(stderr, total - adjust);
95     fprintf(stderr, "]: %s\n", msg);
96 }
97 
98 
99 /* strerror - return pointer to appropriate system error message
100  *
101  * DESCRIPTION
102  *
103  *	Get an error message string which is appropriate for the setting
104  *	of the errno variable.
105  *
106  * RETURNS
107  *
108  *	Returns a pointer to a string which has an appropriate error
109  *	message for the present value of errno.  The error message
110  *	strings are taken from sys_errlist[] where appropriate.  If an
111  *	appropriate message is not available in sys_errlist, then a
112  *	pointer to the string "Unknown error (errno <errvalue>)" is
113  *	returned instead.
114  */
115 
116 #ifdef __STDC__
117 
strerror(void)118 char *strerror(void)
119 
120 #else
121 
122 char *strerror()
123 
124 #endif
125 {
126 #ifdef _POSIX_SOURCE
127 #undef strerror
128     return (strerror(errno));
129 #else
130     static char     msg[40];		/* used for "Unknown error" messages */
131 
132     if (errno > 0 && errno < sys_nerr) {
133 	return (sys_errlist[errno]);
134     }
135     sprintf(msg, "Unknown error (errno %d)", errno);
136     return (msg);
137 #endif
138 }
139 
140 
141 /* prsize - print a file offset on a file stream
142  *
143  * DESCRIPTION
144  *
145  *	Prints a file offset to a specific file stream.  The file offset is
146  *	of the form "%dm+%dk+%d", where the number preceeding the "m" and
147  *	the "k" stand for the number of Megabytes and the number of
148  *	Kilobytes, respectivley, which have been processed so far.
149  *
150  * PARAMETERS
151  *
152  *	FILE  *stream	- Stream which is to be used for output
153  *	OFFSET size	- Current archive position to be printed on the output
154  *			  stream in the form: "%dm+%dk+%d".
155  *
156  */
157 
158 #ifdef __STDC__
159 
prsize(FILE * stream,OFFSET size)160 static void prsize(FILE *stream, OFFSET size)
161 
162 #else
163 
164 static void prsize(stream, size)
165 FILE           *stream;		/* stream which is used for output */
166 OFFSET          size;		/* current archive position to be printed */
167 
168 #endif
169 
170 {
171     OFFSET          n;
172 
173     if (n = (size / (1024L * 1024L))) {
174 	fprintf(stream, "%ldm+", n);
175 	size -= n * 1024L * 1024L;
176     }
177     if (n = (size / 1024L)) {
178 	fprintf(stream, "%ldk+", n);
179 	size -= n * 1024L;
180     }
181     fprintf(stream, "%ld", size);
182 }
183 
184 
185 /* fatal - print fatal message and exit
186  *
187  * DESCRIPTION
188  *
189  *	Fatal prints the program's name along with an error message, then
190  *	exits the program with a non-zero return code.
191  *
192  * PARAMETERS
193  *
194  *	char 	*why	- description of reason for termination
195  *
196  * RETURNS
197  *
198  *	Returns an exit code of 1 to the parent process.
199  */
200 
201 #ifdef __STDC__
202 
fatal(char * why)203 void fatal(char *why)
204 
205 #else
206 
207 void fatal(why)
208 char           *why;		/* description of reason for termination */
209 
210 #endif
211 {
212     fprintf(stderr, "%s: %s\n", myname, why);
213     exit(1);
214 }
215 
216 
217 
218 /* warn - print a warning message
219  *
220  * DESCRIPTION
221  *
222  *	Print an error message listing the program name, the actual error
223  *	which occurred and an informational message as to why the error
224  *	occurred on the standard error device.  The standard error is
225  *	flushed after the error is printed to assure that the user gets
226  *	the message in a timely fasion.
227  *
228  * PARAMETERS
229  *
230  *	char *what	- Pointer to string describing what failed.
231  *	char *why	- Pointer to string describing why did it failed.
232  */
233 
234 #ifdef __STDC__
235 
warn(char * what,char * why)236 void warn(char *what, char *why)
237 
238 #else
239 
240 void warn(what, why)
241 char           *what;		/* message as to what the error was */
242 char           *why;		/* explanation why the error occurred */
243 
244 #endif
245 {
246     fprintf(stderr, "%s: %s : %s\n", myname, what, why);
247     fflush(stderr);
248 }
249