source: rtems/c/build-tools/cklength.c @ e399e182

4.104.114.84.95
Last change on this file since e399e182 was c64e4ed4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/15/96 at 21:50:28

updates from Tony Bennett for PA and UNIX ports

  • 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
87main(int argc, char **argv, char **env)
88{
89    register int c;
90    int showusage = FALSE;                      /* usage error? */
91    int rc = 0;
92
93    /*
94     * figure out invocation leaf-name
95     */
96
97    if ((progname = strrchr(argv[0], '/')) == (char *) NULL)
98        progname = argv[0];
99    else
100        progname++;
101
102    argv[0] = progname;                         /* for getopt err reporting */
103
104    /*
105     *  Check options and arguments.
106     */
107
108    opterr = 0;                                 /* we'll report all errors */
109    while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
110        switch (c)
111        {
112            case 'l':                            /* line length */
113                line_length = atoi( optarg );
114                if ( line_length < 0 || line_length > BUFFER_SIZE )
115                  error(ERR_FATAL, "(%d) is illegal line length\n",line_length);
116                break;
117
118            case 'n':                           /* toggle report_line_numbers */
119                report_line_numbers = ! report_line_numbers;
120                break;
121
122            case 'N':                           /* toggle both reports */
123                report_line_numbers = ! report_line_numbers;
124                report_line_length  = ! report_line_length;
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  char   *bptr;
162  char    buffer[ BUFFER_SIZE ];
163  int     line_number;
164  int     length;
165  int     count;
166  int     rc = SUCCESS;  /* succeed by default */
167
168  in = fopen( arg, "r" );
169  if (!in)
170    error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg );
171
172  count = 0;
173
174  for ( line_number=1 ; ; line_number++ ) {
175    bptr = fgets( buffer, BUFFER_SIZE, in );
176    if (!bptr)
177      break;
178
179    /*
180     *  Don't count the carriage return.
181     */
182
183    length = strlen( buffer ) - 1;
184
185    if ( length <= line_length )
186      continue;
187
188    if ( count == 0 ) {
189      fprintf( stderr, "%s\n", arg );
190      if ( !report_line_numbers )
191        break;
192    }
193
194    if ( verbose )
195      fprintf( stderr, "TOO LONG:%d: %s\n", line_number, buffer );
196
197    if ( report_line_numbers ) {
198      if ( report_line_length )
199        fprintf( stderr, "%d: %d\n" , line_number, length );
200      else
201        fprintf( stderr, "%d\n" , line_number );
202    }
203
204    count++;
205
206  }
207
208  fclose( in );
209  return rc;
210}
211
212/*
213 * error(errn, arglist)
214 *      report an error to stderr using printf(3) conventions.
215 *      Any output is preceded by '<progname>: '
216 *
217 * Uses ERR_FATAL bit to request exit(errn)
218 *      ERR_ABORT to request abort()
219 *      ERR_ERRNO to indicate use of errno instead of argument.
220 *
221 * If resulting 'errn' is non-zero, it is assumed to be an 'errno' and its
222 *      associated error message is appended to the output.
223 */
224
225/*VARARGS*/
226
227void
228error(int error_flag, ...)
229{
230    va_list arglist;
231    register char *format;
232    extern char *sys_errlist[];
233    extern int sys_nerr;
234    int local_errno;
235
236    extern int errno;
237
238    (void) fflush(stdout);          /* in case stdout/stderr same */
239
240    local_errno = error_flag & ~ERR_MASK;
241    if (error_flag & ERR_ERRNO)     /* use errno? */
242        local_errno = errno;
243
244    va_start(arglist, error_flag);
245    format = va_arg(arglist, char *);
246    (void) fprintf(stderr, "%s: ", progname);
247    (void) vfprintf(stderr, format, arglist);
248    va_end(arglist);
249
250    if (local_errno)
251        if ((local_errno > 0) && (local_errno < sys_nerr))
252            (void) fprintf(stderr, " (%s)\n", sys_errlist[local_errno]);
253        else
254            (void) fprintf(stderr, " (unknown errno=%d)\n", local_errno);
255    else
256        (void) fprintf(stderr, "\n");
257
258    (void) fflush(stderr);
259
260    if (error_flag & (ERR_FATAL | ERR_ABORT))
261    {
262        if (error_flag & ERR_FATAL)
263        {
264            error(0, "fatal error, exiting");
265            exit(local_errno ? local_errno : 1);
266        }
267        else
268        {
269            error(0, "fatal error, aborting");
270            abort();
271        }
272    }
273}
274
275long
276getparm(char *s,
277        long min,
278        long max,
279        char *msg)
280{
281    long val;
282
283    if ( ! strchr("0123456789-", *s))
284    {
285        error(ERR_FATAL, "'%s' is not a number", s);
286        return min;
287    }
288
289    val = strtol(s, (char **) NULL, 0);
290    if ((val < min) || (val > max))
291    {
292        if (min == max)
293            error(ERR_FATAL, "%s can only be %ld", s, min);
294        else
295            error(ERR_FATAL, "%s must be between %ld and %ld", msg, min, max);
296    }
297
298    return val;
299}
300
301
302/*
303 * Open()
304 *      Perform open(2), returning the file descriptor.  Prints
305 *      error message if open fails.
306 */
307
308int
309Open(char *file,
310     int oflag,
311     int mode)
312{
313    int O_fd;
314
315    if (Failed(O_fd = open(file, oflag, mode)))
316        error(
317          ERR_ERRNO | ERR_FATAL,
318          "open('%s', 0x%x, 0%o) failed", file, oflag, mode
319        );
320
321    return O_fd;
322}
323
324/*
325 * Read()
326 *      Perform read(2); prints error message if fails.
327 */
328
329int
330Read(int file,
331     char *buffer,
332     unsigned int count)
333{
334    int nbytes;
335
336    if (Failed(nbytes = read(file, buffer, count)))
337        error(
338          ERR_ERRNO | ERR_FATAL,
339          "read(%d, 0x%x, %d) failed", file, buffer, count
340         );
341
342    return nbytes;
343}
344
345/*
346 * Write()
347 *      Perform write(2); prints error message if fails.
348 */
349
350int
351Write(int file,
352      char *buffer,
353      unsigned int count)
354{
355    int nbytes;
356
357    if (Failed(nbytes = write(file, buffer, count)))
358        error(
359          ERR_ERRNO | ERR_FATAL,
360          "write(%d, 0x%x, %d) failed", file, buffer, count
361        );
362
363    return nbytes;
364}
Note: See TracBrowser for help on using the repository browser.