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