1 /* 2 ** This program checks for formatting problems in source code: 3 ** 4 ** * Any use of tab characters 5 ** * White space at the end of a line 6 ** * Blank lines at the end of a file 7 ** 8 ** Any violations are reported. 9 */ 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #define CR_OK 0x001 15 #define WSEOL_OK 0x002 16 17 static void checkSpacing(const char *zFile, unsigned flags){ 18 FILE *in = fopen(zFile, "rb"); 19 int i; 20 int seenSpace; 21 int seenTab; 22 int ln = 0; 23 int lastNonspace = 0; 24 char zLine[2000]; 25 if( in==0 ){ 26 printf("cannot open %s\n", zFile); 27 return; 28 } 29 while( fgets(zLine, sizeof(zLine), in) ){ 30 seenSpace = 0; 31 seenTab = 0; 32 ln++; 33 for(i=0; zLine[i]; i++){ 34 if( zLine[i]=='\t' && seenTab==0 ){ 35 printf("%s:%d: tab (\\t) character\n", zFile, ln); 36 seenTab = 1; 37 }else if( zLine[i]=='\r' ){ 38 if( (flags & CR_OK)==0 ){ 39 printf("%s:%d: carriage-return (\\r) character\n", zFile, ln); 40 } 41 }else if( zLine[i]==' ' ){ 42 seenSpace = 1; 43 }else if( zLine[i]!='\n' ){ 44 lastNonspace = ln; 45 seenSpace = 0; 46 } 47 } 48 if( seenSpace && (flags & WSEOL_OK)==0 ){ 49 printf("%s:%d: whitespace at end-of-line\n", zFile, ln); 50 } 51 } 52 fclose(in); 53 if( lastNonspace<ln ){ 54 printf("%s:%d: blank lines at end of file (%d)\n", 55 zFile, ln, ln - lastNonspace); 56 } 57 } 58 59 int main(int argc, char **argv){ 60 int i; 61 unsigned flags = WSEOL_OK; 62 for(i=1; i<argc; i++){ 63 const char *z = argv[i]; 64 if( z[0]=='-' ){ 65 while( z[0]=='-' ) z++; 66 if( strcmp(z,"crok")==0 ){ 67 flags |= CR_OK; 68 }else if( strcmp(z, "wseol")==0 ){ 69 flags &= ~WSEOL_OK; 70 }else if( strcmp(z, "help")==0 ){ 71 printf("Usage: %s [options] FILE ...\n", argv[0]); 72 printf(" --crok Do not report on carriage-returns\n"); 73 printf(" --wseol Complain about whitespace at end-of-line\n"); 74 printf(" --help This message\n"); 75 }else{ 76 printf("unknown command-line option: [%s]\n", argv[i]); 77 printf("use --help for additional information\n"); 78 } 79 }else{ 80 checkSpacing(argv[i], flags); 81 } 82 } 83 return 0; 84 } 85