source: rtems/c/build-tools/eolstrip.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.0 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 * $Log$
12 * Revision 1.4  1995/10/06  20:00:39  joel
13 * SPARC merged and successfully tested w/o interrupt support
14 *
15 * Revision 1.3  1995/10/06 16:32:21  joel
16 * moved to new development machine and went to gcc 2.7.0
17 *
18 * Revision 1.2  1995/05/31  16:44:28  joel
19 * fixing revision
20 *
21 * Revision 1.1.1.1  1995/05/11  17:35:12  joel
22 * Release 3.1.15
23 *
24 */
25
26#define GETOPTARGS "vt"
27
28char *USAGE = "\
29usage:  cklength  [ -v ] [ arg ... ] files... \n\
30            -v          -- verbose\n\
31            -t          -- test only .. DO NOT OVERWRITE FILE!!!\n\
32\n\
33Strip the white space from the end of every line on the list of files.\n\
34";
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <fcntl.h>
39#include <ctype.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <string.h>
43#include <memory.h>
44#include <stdarg.h>
45
46#define BUFFER_SIZE     2048
47#define MAX_PATH        2048
48
49#define SUCCESS         0
50#define FAILURE         -1
51#define Failed(x)       (((int) (x)) == FAILURE)
52#define TRUE    1
53#define FALSE   0
54#define STREQ(a,b)      (strcmp(a,b) == 0)
55#define NUMELEMS(arr)   (sizeof(arr) / sizeof(arr[0]))
56
57/*
58 * Definitions for unsigned "ints"; especially for use in data structures
59 *  that will be shared among (potentially) different cpu's (we punt on
60 *  byte ordering problems tho)
61 */
62
63typedef unsigned char   u8;
64typedef unsigned short  u16;
65typedef unsigned long   u32;
66
67/*
68 * vars controlled by command line options
69 */
70
71int verbose = FALSE;                    /* be verbose */
72int test_only = FALSE;                  /* test only */
73
74extern char *optarg;                    /* getopt(3) control vars */
75extern int optind, opterr;
76extern int errno;
77
78char *progname;                         /* for error() */
79
80int process(char *arg);
81void error(int errn, ...);
82long getparm(char *s, long min, long max, char *msg);
83
84#define ERR_ERRNO  (1<<((sizeof(int) * 8) - 2)) /* hi bit; use 'errno' */
85#define ERR_FATAL  (ERR_ERRNO / 2)              /* fatal error ; no return */
86#define ERR_ABORT  (ERR_ERRNO / 4)              /* fatal error ; abort */
87#define ERR_MASK   (ERR_ERRNO | ERR_FATAL | ERR_ABORT) /* all */
88
89#define stol(p) strtol(p, (char **) NULL, 0)
90int  Open(), Read(), Write();
91
92int
93main(int argc, char **argv, char **env)
94{
95    register int c;
96    int showusage = FALSE;                      /* usage error? */
97    int rc = 0;
98
99    /*
100     * figure out invocation leaf-name
101     */
102
103    if ((progname = strrchr(argv[0], '/')) == (char *) NULL)
104        progname = argv[0];
105    else
106        progname++;
107
108    argv[0] = progname;                         /* for getopt err reporting */
109
110    /*
111     *  Check options and arguments.
112     */
113
114    opterr = 0;                                 /* we'll report all errors */
115    while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
116        switch (c)
117        {
118            case 't':                           /* toggle test only mode */
119                test_only = ! test_only;
120                break;
121
122            case 'v':                           /* toggle verbose */
123                verbose = ! verbose;
124                break;
125
126            case '?':
127                showusage = TRUE;
128        }
129
130    if (showusage)
131    {
132        (void) fprintf(stderr, "%s", USAGE);
133        exit(1);
134    }
135
136    /*
137     *  traverse and process the arguments
138     */
139
140    for ( ; argv[optind]; optind++)
141        if (Failed(process(argv[optind])))
142            rc = FAILURE;
143
144    return rc;
145}
146
147
148/*
149 * process(arg)
150 */
151
152int
153process(char *arg)
154{
155  FILE   *in;
156  FILE   *out = (FILE *) 0;
157  char    outname[ MAX_PATH ];
158  char   *bptr;
159  char    buffer[ BUFFER_SIZE ];
160  int     length;
161  int     line_number;
162  int     rc = SUCCESS;  /* succeed by default */
163
164  in = fopen( arg, "r" );
165  if (!in)
166    error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg );
167
168  if ( !test_only ) {
169    sprintf( outname, "%s.eoltmp", arg );
170
171    out = fopen( outname, "w" );
172    if (!out)
173      error( ERR_ERRNO | ERR_FATAL, "Unable to open file (%s)\n", arg );
174  }
175
176  if ( verbose )
177    fprintf( stderr, "Processing %s\n", arg );
178
179  for ( line_number=1 ; ; line_number++ ) {
180    bptr = fgets( buffer, BUFFER_SIZE, in );
181    if (!bptr)
182      break;
183
184    /*
185     *  Don't count the carriage return.
186     */
187
188    length = strlen( buffer ) - 1;
189
190    if ( buffer[ length ] != '\n' )
191      error(ERR_ERRNO|ERR_FATAL, "Line %d too long in %s\n", line_number, arg);
192
193    while ( isspace( buffer[ length ] ) )
194      buffer[ length-- ] = '\0';
195
196    if ( test_only ) {
197      fprintf( stderr, "%s\n", arg );
198      break;
199    }
200
201    fprintf( out, "%s\n", buffer );
202  }
203
204  fclose( in );
205  if ( !test_only ) {
206    fclose( out );
207    rename( outname, arg );
208  }
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, local_errno ? "fatal error, exiting" : "exiting");
265            exit(local_errno);
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.