1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <unistd.h>
5
6 static void
usage(void)7 usage(void) {
8 (void) fprintf(stderr, "check80 [-v] [-n number] [files ...]\n\n");
9 (void) fprintf(stderr, "check80 checks whether lines in a file are too"
10 "long");
11 (void) fprintf(stderr, "By default ``too long'' means 80");
12 (void) fprintf(stderr, "The -v option sets this limit to 72\n");
13 (void) fprintf(stderr,
14 "The -n option sets this limit to a custom value\n");
15 exit(EXIT_FAILURE);
16 }
17
18 int
main(int argc,char ** argv)19 main(int argc, char **argv) {
20 int i;
21 FILE *fd;
22 int ch;
23 int warn;
24 int nchars;
25 int charsok = 80;
26 int lineno;
27
28
29 while ((ch = getopt(argc, argv, "vn:")) != -1) {
30 switch (ch) {
31 case 'v':
32 /*
33 * Check for vi with ``number'' option set -
34 * 72 chars ok
35 */
36 charsok = 72;
37 break;
38 case 'n': {
39 long nch;
40 char *p;
41
42 nch = strtol(optarg, &p, 10);
43 if (*p || p == optarg
44 || nch == LONG_MIN || nch == LONG_MAX) {
45 (void) fprintf(stderr, "Bad argument to -n\n");
46 usage();
47 }
48
49 charsok = nch;
50 break;
51 }
52 default:
53 usage();
54 }
55 }
56
57
58 argc -= optind;
59 argv += optind;
60
61 for (i = 0; i < argc; ++i) {
62 if ((fd = fopen(argv[i], "r")) == NULL) {
63 perror(argv[i]);
64 continue;
65 }
66
67 lineno = 1;
68 nchars = warn = 0;
69
70 while ((ch = fgetc(fd)) != EOF) {
71 if (ch == '\n') {
72 if (warn) {
73 printf(
74 "%s: Line %d too long (%d chars)\n",
75 argv[i], lineno, nchars);
76 }
77 nchars = warn = 0;
78 ++lineno;
79 } else {
80 if (ch == '\t') nchars += 8;
81 else ++nchars;
82 if (nchars > charsok) {
83 warn = 1;
84 }
85 }
86 }
87 (void) fclose(fd);
88 }
89 return 0;
90 }
91
92