source: rtems/tools/build/src/eolstrip.c @ 419fdf1

4.104.114.84.95
Last change on this file since 419fdf1 was 419fdf1, checked in by Joel Sherrill <joel.sherrill@…>, on 01/21/98 at 16:50:18

Corrected prototypes for main per Ralf Corsepius' report of warnings
generated when egcs is used with "-Wall -pedantic".

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