1 | /* |
---|
2 | * eolstrip - strip white space from end of lines |
---|
3 | * |
---|
4 | * This program strips the white space from the end of every line in the |
---|
5 | * specified program. |
---|
6 | * |
---|
7 | * usage: eolstrip [ -v ] [ arg ... ] files... |
---|
8 | * -v -- verbose |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #define GETOPTARGS "vt" |
---|
14 | |
---|
15 | char *USAGE = "\ |
---|
16 | usage: cklength [ -v ] [ arg ... ] files... \n\ |
---|
17 | -v -- verbose\n\ |
---|
18 | -t -- test only .. DO NOT OVERWRITE FILE!!!\n\ |
---|
19 | \n\ |
---|
20 | Strip the white space from the end of every line on the list of files.\n\ |
---|
21 | "; |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | #include <stdlib.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <ctype.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <unistd.h> |
---|
29 | #include <string.h> |
---|
30 | #include <memory.h> |
---|
31 | #include <stdarg.h> |
---|
32 | |
---|
33 | #define BUFFER_SIZE 2048 |
---|
34 | #define MAX_PATH 2048 |
---|
35 | |
---|
36 | #define SUCCESS 0 |
---|
37 | #define FAILURE -1 |
---|
38 | #define Failed(x) (((int) (x)) == FAILURE) |
---|
39 | #define TRUE 1 |
---|
40 | #define FALSE 0 |
---|
41 | #define STREQ(a,b) (strcmp(a,b) == 0) |
---|
42 | #define NUMELEMS(arr) (sizeof(arr) / sizeof(arr[0])) |
---|
43 | |
---|
44 | /* |
---|
45 | * Definitions for unsigned "ints"; especially for use in data structures |
---|
46 | * that will be shared among (potentially) different cpu's (we punt on |
---|
47 | * byte ordering problems tho) |
---|
48 | */ |
---|
49 | |
---|
50 | typedef unsigned char u8; |
---|
51 | typedef unsigned short u16; |
---|
52 | typedef unsigned long u32; |
---|
53 | |
---|
54 | /* |
---|
55 | * vars controlled by command line options |
---|
56 | */ |
---|
57 | |
---|
58 | int verbose = FALSE; /* be verbose */ |
---|
59 | int test_only = FALSE; /* test only */ |
---|
60 | |
---|
61 | extern char *optarg; /* getopt(3) control vars */ |
---|
62 | extern int optind, opterr; |
---|
63 | extern int errno; |
---|
64 | |
---|
65 | char *progname; /* for error() */ |
---|
66 | |
---|
67 | int process(char *arg); |
---|
68 | void error(int errn, ...); |
---|
69 | long getparm(char *s, long min, long max, char *msg); |
---|
70 | |
---|
71 | #define ERR_ERRNO (1<<((sizeof(int) * 8) - 2)) /* hi bit; use 'errno' */ |
---|
72 | #define ERR_FATAL (ERR_ERRNO / 2) /* fatal error ; no return */ |
---|
73 | #define ERR_ABORT (ERR_ERRNO / 4) /* fatal error ; abort */ |
---|
74 | #define ERR_MASK (ERR_ERRNO | ERR_FATAL | ERR_ABORT) /* all */ |
---|
75 | |
---|
76 | #define stol(p) strtol(p, (char **) NULL, 0) |
---|
77 | int Open(), Read(), Write(); |
---|
78 | |
---|
79 | int |
---|
80 | main(int argc, char **argv, char **env) |
---|
81 | { |
---|
82 | register int c; |
---|
83 | int showusage = FALSE; /* usage error? */ |
---|
84 | int rc = 0; |
---|
85 | |
---|
86 | /* |
---|
87 | * figure out invocation leaf-name |
---|
88 | */ |
---|
89 | |
---|
90 | if ((progname = strrchr(argv[0], '/')) == (char *) NULL) |
---|
91 | progname = argv[0]; |
---|
92 | else |
---|
93 | progname++; |
---|
94 | |
---|
95 | argv[0] = progname; /* for getopt err reporting */ |
---|
96 | |
---|
97 | /* |
---|
98 | * Check options and arguments. |
---|
99 | */ |
---|
100 | |
---|
101 | opterr = 0; /* we'll report all errors */ |
---|
102 | while ((c = getopt(argc, argv, GETOPTARGS)) != EOF) |
---|
103 | switch (c) |
---|
104 | { |
---|
105 | case 't': /* toggle test only mode */ |
---|
106 | test_only = ! test_only; |
---|
107 | break; |
---|
108 | |
---|
109 | case 'v': /* toggle verbose */ |
---|
110 | verbose = ! verbose; |
---|
111 | break; |
---|
112 | |
---|
113 | case '?': |
---|
114 | showusage = TRUE; |
---|
115 | } |
---|
116 | |
---|
117 | if (showusage) |
---|
118 | { |
---|
119 | (void) fprintf(stderr, "%s", USAGE); |
---|
120 | exit(1); |
---|
121 | } |
---|
122 | |
---|
123 | /* |
---|
124 | * traverse and process the arguments |
---|
125 | */ |
---|
126 | |
---|
127 | for ( ; argv[optind]; optind++) |
---|
128 | if (Failed(process(argv[optind]))) |
---|
129 | rc = FAILURE; |
---|
130 | |
---|
131 | return rc; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /* |
---|
136 | * process(arg) |
---|
137 | */ |
---|
138 | |
---|
139 | int |
---|
140 | process(char *arg) |
---|
141 | { |
---|
142 | FILE *in; |
---|
143 | FILE *out = (FILE *) 0; |
---|
144 | char outname[ MAX_PATH ]; |
---|
145 | char *bptr; |
---|
146 | char buffer[ BUFFER_SIZE ]; |
---|
147 | int length; |
---|
148 | int line_number; |
---|
149 | int rc = SUCCESS; /* succeed by default */ |
---|
150 | |
---|
151 | in = fopen( arg, "r" ); |
---|
152 | if (!in) |
---|
153 | error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg ); |
---|
154 | |
---|
155 | if ( !test_only ) { |
---|
156 | sprintf( outname, "%s.eoltmp", arg ); |
---|
157 | |
---|
158 | out = fopen( outname, "w" ); |
---|
159 | if (!out) |
---|
160 | error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg ); |
---|
161 | } |
---|
162 | |
---|
163 | if ( verbose ) |
---|
164 | fprintf( stderr, "Processing %s\n", arg ); |
---|
165 | |
---|
166 | for ( line_number=1 ; ; line_number++ ) { |
---|
167 | bptr = fgets( buffer, BUFFER_SIZE, in ); |
---|
168 | if (!bptr) |
---|
169 | break; |
---|
170 | |
---|
171 | /* |
---|
172 | * Don't count the carriage return. |
---|
173 | */ |
---|
174 | |
---|
175 | length = strlen( buffer ) - 1; |
---|
176 | |
---|
177 | if ( buffer[ length ] != '\n' ) |
---|
178 | error(ERR_ERRNO|ERR_FATAL, "Line %d too long in %s\n", line_number, arg); |
---|
179 | |
---|
180 | while ( isspace( buffer[ length ] ) ) |
---|
181 | buffer[ length-- ] = '\0'; |
---|
182 | |
---|
183 | if ( test_only ) { |
---|
184 | fprintf( stderr, "%s\n", arg ); |
---|
185 | break; |
---|
186 | } |
---|
187 | |
---|
188 | fprintf( out, "%s\n", buffer ); |
---|
189 | } |
---|
190 | |
---|
191 | fclose( in ); |
---|
192 | if ( !test_only ) { |
---|
193 | fclose( out ); |
---|
194 | rename( outname, arg ); |
---|
195 | } |
---|
196 | return rc; |
---|
197 | } |
---|
198 | |
---|
199 | /* |
---|
200 | * error(errn, arglist) |
---|
201 | * report an error to stderr using printf(3) conventions. |
---|
202 | * Any output is preceded by '<progname>: ' |
---|
203 | * |
---|
204 | * Uses ERR_FATAL bit to request exit(errn) |
---|
205 | * ERR_ABORT to request abort() |
---|
206 | * ERR_ERRNO to indicate use of errno instead of argument. |
---|
207 | * |
---|
208 | * If resulting 'errn' is non-zero, it is assumed to be an 'errno' and its |
---|
209 | * associated error message is appended to the output. |
---|
210 | */ |
---|
211 | |
---|
212 | /*VARARGS*/ |
---|
213 | |
---|
214 | void |
---|
215 | error(int error_flag, ...) |
---|
216 | { |
---|
217 | va_list arglist; |
---|
218 | register char *format; |
---|
219 | extern char *sys_errlist[]; |
---|
220 | extern int sys_nerr; |
---|
221 | int local_errno; |
---|
222 | |
---|
223 | extern int errno; |
---|
224 | |
---|
225 | (void) fflush(stdout); /* in case stdout/stderr same */ |
---|
226 | |
---|
227 | local_errno = error_flag & ~ERR_MASK; |
---|
228 | if (error_flag & ERR_ERRNO) /* use errno? */ |
---|
229 | local_errno = errno; |
---|
230 | |
---|
231 | va_start(arglist, error_flag); |
---|
232 | format = va_arg(arglist, char *); |
---|
233 | (void) fprintf(stderr, "%s: ", progname); |
---|
234 | (void) vfprintf(stderr, format, arglist); |
---|
235 | va_end(arglist); |
---|
236 | |
---|
237 | if (local_errno) |
---|
238 | if ((local_errno > 0) && (local_errno < sys_nerr)) |
---|
239 | (void) fprintf(stderr, " (%s)\n", sys_errlist[local_errno]); |
---|
240 | else |
---|
241 | (void) fprintf(stderr, " (unknown errno=%d)\n", local_errno); |
---|
242 | else |
---|
243 | (void) fprintf(stderr, "\n"); |
---|
244 | |
---|
245 | (void) fflush(stderr); |
---|
246 | |
---|
247 | if (error_flag & (ERR_FATAL | ERR_ABORT)) |
---|
248 | { |
---|
249 | if (error_flag & ERR_FATAL) |
---|
250 | { |
---|
251 | error(0, "fatal error, exiting"); |
---|
252 | exit(local_errno ? local_errno : 1); |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | error(0, "fatal error, aborting"); |
---|
257 | abort(); |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | long |
---|
263 | getparm(char *s, |
---|
264 | long min, |
---|
265 | long max, |
---|
266 | char *msg) |
---|
267 | { |
---|
268 | long val; |
---|
269 | |
---|
270 | if ( ! strchr("0123456789-", *s)) |
---|
271 | { |
---|
272 | error(ERR_FATAL, "'%s' is not a number", s); |
---|
273 | return min; |
---|
274 | } |
---|
275 | |
---|
276 | val = strtol(s, (char **) NULL, 0); |
---|
277 | if ((val < min) || (val > max)) |
---|
278 | { |
---|
279 | if (min == max) |
---|
280 | error(ERR_FATAL, "%s can only be %ld", s, min); |
---|
281 | else |
---|
282 | error(ERR_FATAL, "%s must be between %ld and %ld", msg, min, max); |
---|
283 | } |
---|
284 | |
---|
285 | return val; |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | /* |
---|
290 | * Open() |
---|
291 | * Perform open(2), returning the file descriptor. Prints |
---|
292 | * error message if open fails. |
---|
293 | */ |
---|
294 | |
---|
295 | int |
---|
296 | Open(char *file, |
---|
297 | int oflag, |
---|
298 | int mode) |
---|
299 | { |
---|
300 | int O_fd; |
---|
301 | |
---|
302 | if (Failed(O_fd = open(file, oflag, mode))) |
---|
303 | error( |
---|
304 | ERR_ERRNO | ERR_FATAL, |
---|
305 | "open('%s', 0x%x, 0%o) failed", file, oflag, mode |
---|
306 | ); |
---|
307 | |
---|
308 | return O_fd; |
---|
309 | } |
---|
310 | |
---|
311 | /* |
---|
312 | * Read() |
---|
313 | * Perform read(2); prints error message if fails. |
---|
314 | */ |
---|
315 | |
---|
316 | int |
---|
317 | Read(int file, |
---|
318 | char *buffer, |
---|
319 | unsigned int count) |
---|
320 | { |
---|
321 | int nbytes; |
---|
322 | |
---|
323 | if (Failed(nbytes = read(file, buffer, count))) |
---|
324 | error( |
---|
325 | ERR_ERRNO | ERR_FATAL, |
---|
326 | "read(%d, 0x%x, %d) failed", file, buffer, count |
---|
327 | ); |
---|
328 | |
---|
329 | return nbytes; |
---|
330 | } |
---|
331 | |
---|
332 | /* |
---|
333 | * Write() |
---|
334 | * Perform write(2); prints error message if fails. |
---|
335 | */ |
---|
336 | |
---|
337 | int |
---|
338 | Write(int file, |
---|
339 | char *buffer, |
---|
340 | unsigned int count) |
---|
341 | { |
---|
342 | int nbytes; |
---|
343 | |
---|
344 | if (Failed(nbytes = write(file, buffer, count))) |
---|
345 | error( |
---|
346 | ERR_ERRNO | ERR_FATAL, |
---|
347 | "write(%d, 0x%x, %d) failed", file, buffer, count |
---|
348 | ); |
---|
349 | |
---|
350 | return nbytes; |
---|
351 | } |
---|