Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!ginosko!usc!rutgers!att!chinet!bill From: bill@chinet.chi.il.us (Bill Mitchell) Newsgroups: comp.os.minix Subject: important new program: cleanit.c Message-ID: <9771@chinet.chi.il.us> Date: 8 Oct 89 23:16:14 GMT Organization: Chinet - Chicago, Ill. Lines: 335 AST recently posted a program, cleanit.c, to strip leading junk ahead of a shar file from a USENET posting. Problem: The leading junk often contains very useful info about the shar file. Problem: Cleanup.c requires posters to discipine themselves to include a "cut line" with 20 leading dashes. Fat chance. Problem: Postings often include unshared C programs. Solution: One is needed. My contribution below. This is my first cut at a program, comment.c, which is intended to comment leading junk in a posting containing a shar file or an unshared C program, leaving the junk intact in case it is useful. Emphasize that this is a first cut. It has not been used or tested much at all. It seems to work OK except for postings I've seen which include the cute trailer line "#include ". The leading '#' causes comment.c to think a shar file is coming, and it considers all text before the cute trailer line to be leading junk. The moral of that story is that it ought to be smarter about identifying shar files. Anybody care to add the needed smarts? /* ** comment.c - bill mitchell 07 oct 89 ** ** this program reads files and tries to identify an imbedded shar file ** or c program ** ** if a shar file or c program is found, it comments out leading junk ** leaving the commented leading junk in the file for future reference ** ** it announces its conclusions about c programs and shar files on stdout ** it tries to avoid trashing the files if run multiple times ** it has options to test files without modifying them, to back out changes, ** and to force "# " shell comments onto a specified number of lines. ** ** it thinks all c programs begin with "/*" ** and shar files begin with "echo x - " ** or on the line following one or more "#" shell comment lines ** ** usage: comment [options & filenames] ** see the usage() function for details on the options ** ** with Microsoft C -- cl comment.c \msc5\lib\setargv /link /NOE ** under MINIX -- cc -DMINIX -o comment comment.c ** */ #include #include #include #ifdef MSDOS #include #include #include #endif #ifdef MINIX #define fputs(a, b) fputs(a, b), 0 #endif #define NOTHING 0 #define CPROG 1 #define SHARFILE 2 #define BUFSZ 256 /* longer than the longest line in a usenet article */ int Line; /* line number starting shar file or c prog */ #ifdef MINIX main(argc, argv) int argc; char **argv; { #else main(int argc, char **argv) { #endif FILE *fp; char *progname = *argv; char *cp; int tflag = 0; int type; if (--argc) { while(argc--) { cp = *++argv; if (!strncmp(cp, "-t", 2)) { if (!argc--) break; tflag = 1; cp = *++argv; } else if (!strncmp(cp, "-c", 2)) { if (!argc--) break; recomment(*++argv, atoi(cp + 2)); continue; } if ((type = scan(cp)) != NOTHING && tflag == 0) comment(*argv, type); } } else { usage(progname); } exit(0); } #ifdef MINIX usage(name) char *name; { #else usage(char *name) { #endif printf("this program comments out junk ahead of a C program or shar file\n\n"); printf("usage: %s [options & filenames]\n", name); printf("options: -t tests but does not modify subsequent files\n"); printf(" -c file removes inserted comments\n"); printf(" -clines file inserts \"# \" comments onto lines at the head of file\n"); } #ifdef MINIX scan(name) char *name; { #else scan(char *name) { #endif FILE *fp; char buffer[BUFSZ]; static char starget[] = "echo x - "; static int slen = sizeof(starget) -1; static char ctarget[] = "/*"; static int clen = sizeof(ctarget) -1; int rv = NOTHING; if (fp = fopen(name, "r")) { for (Line = 1; fgets(buffer, sizeof(buffer) - 1, fp); Line++) { if (*buffer == '#') { rv = SHARFILE; continue; /* Line++ while the comments last */ } else if (rv == SHARFILE) { printf("%s contains a shar file at line %d\n", name, Line); break; } if (!strncmp(buffer, ctarget, clen)) { rv = CPROG; if (Line == 1) continue; /* probably inserted by me - look for another */ printf("%s contains a C comment at line %d\n", name, Line); break; } if (!strncmp(buffer, starget, slen)) { rv = SHARFILE; break; } } fclose(fp); } else fprintf(stderr, "unable to open file %s\n", name); return(rv); } #ifdef MINIX comment(name, type) char *name; int type; { #else comment(char *name, int type) { #endif FILE *fp1, *fp2; static char template[9] = "zzXXXXXX"; char tempname[9]; char buffer[BUFSZ]; int line; if (fp1 = fopen(name, "r")) { fgets(buffer, sizeof(buffer) - 1, fp1); if (*buffer == '#' || !strncmp(buffer, "/*", 2)) { fclose(fp1); /* already commented - bail out */ return; } strcpy(tempname, template); if (mktemp(tempname) == NULL) { fprintf(stderr, "mktemp() error\n"); exit(1); } if ( (fp2 = fopen(tempname, "w")) == NULL) { fprintf(stderr, "fopen(%s, \"w\") error\n", tempname); exit(1); } for (line = 1; line < Line; line++) { if (line == 1 && type == CPROG) my_fputs("/*\n", fp2); else if (type == SHARFILE) { if (*buffer == '#') break; my_fputs("# ", fp2); } my_fputs(buffer, fp2); fgets(buffer, sizeof(buffer) - 1, fp1); } if (type == CPROG) my_fputs("*/\n", fp2); my_fputs(buffer, fp2); while (fgets(buffer, sizeof(buffer) - 1, fp1)) my_fputs(buffer, fp2); fclose(fp1); fclose(fp2); movefile(tempname, name); } else fprintf(stderr, "unable to open file %s\n", name); } #ifdef MINIX recomment(name, nlines) char *name; int nlines; { #else recomment(char *name, int nlines) { #endif FILE *fp1, *fp2; static char template[9] = "zzXXXXXX"; char tempname[9]; char buffer[BUFSZ]; int line; if (fp1 = fopen(name, "r")) { strcpy(tempname, template); if (mktemp(tempname) == NULL) { fprintf(stderr, "mktemp() error\n"); exit(1); } if ( (fp2 = fopen(tempname, "w")) == NULL) { fprintf(stderr, "fopen(%s, \"w\") error\n", tempname); exit(1); } } if (nlines) { /* insert "# " comments onto nlines */ for (line = 0; line < nlines; line++) { if (!fgets(buffer, sizeof(buffer) - 1, fp1)) break; my_fputs("# ", fp2); my_fputs(buffer, fp2); } while (fgets(buffer, sizeof(buffer) - 1, fp1)) my_fputs(buffer, fp2); } else { fgets(buffer, sizeof(buffer) - 1, fp1); if (!strncmp(buffer, "/*", 2)) { /* uncomment C comment block beginning on line 1 */ while (fgets(buffer, sizeof(buffer) - 1, fp1)) { if (!strncmp(buffer, "*/", 2)) break; my_fputs(buffer, fp2); } while (fgets(buffer, sizeof(buffer) - 1, fp1)) my_fputs(buffer, fp2); } else { /* strip leading "# " from all lines */ do { if (!strncmp(buffer, "# ", 2)) my_fputs(buffer + 2, fp2); else my_fputs(buffer, fp2); } while (fgets(buffer, sizeof(buffer) - 1, fp1)); } } fclose(fp1); fclose(fp2); movefile(tempname, name); } #ifdef MINIX movefile(from, to) char *from, *to; { #else movefile (char *from, char *to) { #endif if (remove(to)) { fprintf(stderr, "problem with remove(\"%s\")\n", to); exit(1); } else if (rename(from, to)) { fprintf(stderr, "problem with rename(\"%s\", \"%s\")\n", from, to); exit(1); } } #ifdef MINIX my_fputs(cp, fp) char *cp; FILE *fp; { #else my_fputs(char *cp, FILE *fp) { #endif if (fputs(cp, fp)) { fprintf(stderr, "fputs() error\n"); exit(1); } } #ifdef MINIX rename(old, new) char *old, *new; { if (link(old, new) < 0) fprintf(stderr, "problem with link(\"%s\", \"%s\")\n", old, new); else unlink(old); } remove(old) char *old; { unlink(old); } #endif