source: rtems/c/build-tools/src/cklength.c @ 4442d21c

4.104.114.84.95
Last change on this file since 4442d21c was 8bdcfc4, checked in by Joel Sherrill <joel.sherrill@…>, on 12/19/95 at 20:26:32

changes remerged after disk crash -- history lost

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