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