/* check file for adherence to Plauger's rules of portable i/o. * see: C Users Journal 7:6 (Aug89) 17-25. * The authors of this software are Eric Grosse and David Gay. * Copyright (c) 1989 by AT&T. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ #include #include #include jmp_buf env; static char Isprint[256] = { /* tab or >= ' ' && < 0x7f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; static char Isspace[256] = { /* " \t\v\r\n\f" */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; static char cutstring[] = "CUT HERE............"; /* or whatever your shar uses */ static int cutsize; char *infname, *progname; FILE *infile; int rc; extern void exit(int); void vgripe(char *msg, long NR) { if (infname) printf("%s: %ld: %s\n", infname, NR, msg); else printf("%ld: %s\n", NR, msg); rc |= 1; } void lgripe(char *msg, long NR) { printf("%s\n", infname ? infname : "/dev/stdin"); fclose(infile); longjmp(env, rc |= 1); } void (*gripe)(char*, long) = vgripe; void usage(int rc) { fprintf(stderr, "usage: %s [-l] [file [file...]]\n\ -l ==> file names only (that mail may mash)\n", progname); exit(rc); } #define isspace(x) Issp[x] #define isprint(x) Ispr[x] void process(FILE *inf, char *name) { char line[512]; register unsigned char *s; register char *Issp = Isspace, *Ispr = Isprint; int i, n, nonpr, nonpr1; static cnt[256]; long NR, crlf; if (setjmp(env)) return; infile = inf; infname = name; NR = 0; crlf = nonpr1 = 0; while(fgets(line,sizeof(line),inf)){ NR++; n = strlen(line); if (n > 510) (*gripe)("long", NR); else { if (line[--n] == '\n') { line[n--] = 0; if (n < 0) continue; } else (*gripe)("no newline", NR); } if( n==1 && line[0]=='.' ) (*gripe)(". on line by itself",NR); if( n>0 && line[0]=='~' ) (*gripe)("leading ~ bothers Berkeley mail",NR); if(line[n]=='\r') { if (!crlf++) (*gripe)("CR LF",NR); } else if(isspace(line[n])) (*gripe)("trailing space", NR); else if(*line==*cutstring && strncmp(line,cutstring,cutsize)==0 ){ (*gripe)("has a cutstring\n",NR); } nonpr = 0; for(s = (unsigned char *)line; *s; s++) { if(!isprint(*s)) { cnt[*s]++; if(!nonpr) { nonpr = nonpr1 = 1; (*gripe)("nonprinting",NR); } } } } fclose(inf); if(NR==0) (*gripe)("empty files may be discarded", NR); if (nonpr1) for(i=0; i<256; i++) if(cnt[i]) { printf("%d occurrences of 0x%.2x\n",cnt[i],i); cnt[i] = 0; } if (crlf) { printf("%ld CR LF pairs", crlf); printf(crlf == NR ? "\n" : " but %ld lines\n", NR); } } int main(int argc, char **argv) { char *s; FILE *inf; progname = *argv; cutsize = strlen(cutstring); while ((s = *++argv) && *s == '-') switch(*++s) { case 'l': gripe = lgripe; continue; case '?': usage(0); case '-': goto break2; default: usage(4); } break2: if (!s) process(stdin,s); else do { if (inf = fopen(s, "r")) process(inf,s); else { fprintf(stderr, "%s: can't open %s\n", progname, s); rc |= 2; } } while(s = *++argv); return rc; }