source: rtems/tools/build/eolstrip.c @ b164303

4.115
Last change on this file since b164303 was b164303, checked in by Josh Oguin <josh.oguin@…>, on 11/25/14 at 21:55:49

tools/build/*.c: Clean up issues reported by CodeSonar?

This code is built without warnings and ignored by Coverity Scan.
CodeSonar? found a wide range of issues including buffer overruns,
buffer underruns, questionable type conversions, leaks, etc. This
set of patches addresses all reported issues.

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