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