Advertisement
slatenails

findtabs.c

Aug 1st, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4.  
  5. int main (int argc, char* argv[]) {
  6.     char buf[1024];
  7.     int i, ln;
  8.     char* c, *lineEnd;
  9.    
  10.     for (i = 1; i < argc; ++i) {
  11.         FILE* fp = fopen (argv[i], "r");
  12.        
  13.         if (!fp) {
  14.             printf ("%s: %s\n", argv[i], strerror (errno));
  15.             continue;
  16.         }
  17.        
  18.         for (ln = 1; fgets (buf, sizeof buf, fp); ++ln) {
  19.             lineEnd = &buf[strlen (buf)]; // position of '\0'
  20.            
  21.             for (c = &buf[0]; *c == '\t'; ++c)
  22.                 ;
  23.            
  24.             if (c != &buf[0] && (*c == '\n' || *c == '\r'))
  25.                 printf ("%s:%d: line all tabs\n", argv[i], ln);
  26.             else {
  27.                 // lineEnd - 2 to skip '\0' and '\n'
  28.                 if (lineEnd != &buf[0] && *(lineEnd - 2) == '\t')
  29.                     printf ("%s:%d: trailing tabs\n", argv[i], ln);
  30.             }
  31.         }
  32.        
  33.         fclose (fp);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement