home *** CD-ROM | disk | FTP | other *** search
- /*
- * tear - tear.c - 03/26/91 - Brendan Kehoe
- *
- * a replacement for the tear shell script & awk procedure
- */
-
- #include <stdio.h>
- #include <string.h>
-
- extern char *safemalloc(), *get_a_line();
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- register char *lp;
- register int curline;
- extern char *safemalloc(), *get_a_line();
- char *line, *hdr, *body;
- short doit;
- FILE *fin, *fout;
-
- line = safemalloc(1001);
- hdr = safemalloc(100);
- body = safemalloc(100);
-
- if (argc < 2)
- exit(1);
-
- if (argc == 2)
- fin = stdin;
- else if ((fin = fopen(*(argv+2),"r"))==(FILE *)NULL)
- exit(1);
-
- strcpy(hdr,*(argv+1));
- strcpy(body,*(argv+1));
- strcat(hdr,"hdr");
- strcat(body,"body");
- fout = fopen(hdr,"w");
-
- lp = line;
- doit = curline = 0;
- while (get_a_line(line,1000,fin))
- {
- curline++;
- if (doit == 0)
- {
- /* ($0 ~ /^[^ \t]*:/ || ($0 ~ /^[ \t]/ && NR > 1)) */
-
- if ((*lp != ' ') && (*lp != '\t'))
- {
- /* if the first is non-ws, but it doesn't have
- a : in it, then it's bad */
-
- if (!strchr(lp, ':'))
- doit = 1;
- }
- else
- {
- /* if it starts with a space or a tab, and we've
- not gone past the first header, it's bad */
-
- if (curline <= 1)
- doit = 1;
- }
-
- if (doit)
- {
- fclose(fout);
- fout = fopen(body,"w");
- }
- }
- fputs(line,fout);
- }
-
- fclose(fout);
- fclose(fin);
- return(0); /* shut UP lint! */
- }
-