home *** CD-ROM | disk | FTP | other *** search
- /*
- ** If only for hack value, a C version of the old-fashioned lint script.
- */
- #include <stdio.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <signal.h>
-
- /* Fundamental constants of the universe. */
- #define ARG_CNT 30
- #define TRUE 1
- #define FALSE 0
-
- /* Argument lists. */
- char *Lint1[ARG_CNT] = {
- "/usr/lib/lint/lint1"
- };
- int Lint1C = 1;
-
- char *Lint2[ARG_CNT] = {
- "/usr/lib/lint/lint2"
- };
- int Lint2C = 1;
-
- char *Cpp[ARG_CNT] = {
- "/lib/cpp",
- "-DLINT",
- "-Dlint",
- "-C"
- };
- int CppC = 4;
-
- /* Other global variables. */
- char Htemp[] = "/tmp/lintHXXXXXX";
- char Ttemp[] = "/tmp/lintTXXXXXX";
- int Libstat;
- int Tfid;
-
- /* Linked in later. */
- extern int errno;
- extern char *calloc();
- extern char *mktemp();
- extern char *strcpy();
- extern char *strchr();
- extern char *strrchr();
- /*+GETOPT ROUTINE
- *
- * A hacked-over version of the one in the standard library.
- */
-
- int optind = 1;
- char *optarg;
-
- int
- getopt(ac, av, opts)
- int ac;
- char **av;
- char *opts;
- {
- static int sp = 1;
- register int c;
- register char *cp;
-
- if (sp == 1)
- if (optind >= ac || av[optind][0] != '-' || av[optind][1] == '\0')
- return('\0');
- else if (strcmp(av[optind], "--") == 0)
- {
- optind++;
- return('\0');
- }
-
- c = av[optind][sp];
- if (c == ':' || (cp = strchr(opts, c)) == NULL)
- yelp("illegal option \"%c\"", c);
- if (*++cp == ':')
- {
- if (av[optind][sp + 1] != '\0')
- optarg = &av[optind++][sp + 1];
- else if (++optind >= ac)
- yelp("option \"%c\" requires an argument", c);
- else
- optarg = av[optind++];
- sp = 1;
- }
- else
- {
- if (av[optind][++sp] == '\0')
- {
- sp = 1;
- optind++;
- }
- optarg = NULL;
- }
- return(c);
- }
- /*+SIGNAL CATCHERS AND EXITS
- */
-
-
- #ifdef LINT
- extern void exit();
- #else
- exit(X) int X; { _exit(X); }
- #endif LINT
-
-
- /* VARARGS1 */
- yelp(A, B)
- char *A;
- char *B;
- {
- int E;
-
- E = errno;
- printf(A, B);
- printf(" (errno = %d).\n", E);
- exit(1);
- }
-
-
- Rupt()
- {
- (void)unlink(Htemp);
- (void)unlink(Ttemp);
- yelp("\r\nInterrupted!\r\n");
- /* NOTREACHED */
- }
- /*+UTILITY ROUTINES
- */
-
-
- /*
- * This routine appends a lint library to the T file.
- */
- Lib(N, Flag)
- char *N;
- int Flag;
- {
- register int F;
- register int i;
- char Buff[BUFSIZ];
-
- if (Flag)
- sprintf(Buff, "/usr/lib/lint/llib-l%s.ln", N);
- else
- (void)strcpy(Buff, N);
-
- if ((F = open(Buff, O_RDONLY)) < 0)
- printf("cannot open \"%s\" lint library (errno=%d)", Buff, errno);
- else
- {
- while ((i = read(F, Buff, sizeof Buff)) > 0)
- (void)write(Tfid, Buff, (unsigned int)i);
- (void)close(F);
- }
- }
-
-
- /*
- * This routine concats an argument to an arglist, perhaps preceeding
- * it with a minus sign and a letter.
- */
- Add(C, Alist, Arg, Simple)
- int C;
- char **Alist;
- char *Arg;
- char Simple;
- {
- int L;
- register char *p;
-
- if (C >= ARG_CNT - 2)
- yelp("too many arguments");
- /* NOTREACHED */
-
- L = strlen(Arg) + 1;
- if (Simple)
- L += 2;
- p = Alist[C] = calloc((unsigned int)L, 1);
- if (Simple)
- {
- *p++ = '-';
- *p++ = Simple;
- }
- (void)strcpy(p, Arg);
- }
- /*!*/
- /*
- * This routine is implements this line from the script:
- * (/lib/cpp $O $A | $L/lint1 $X -H$H $A >>$T) 2>&1
- */
- Do(N)
- char *N;
- {
- int P[2];
-
- if (fork() == 0)
- {
- /* The kids run "... 2>&1" */
- (void)close(2);
- (void)dup(1);
- (void)pipe(P);
-
- Cpp[CppC] = N;
- Lint1[Lint1C] = N;
-
- if (fork() == 0)
- {
- /* Younger child runs "cpp | ..." */
- (void)close(1);
- (void)dup(P[1]);
- (void)close(P[1]);
- (void)close(P[0]);
- (void)close(Tfid);
- (void)execv(Cpp[0], Cpp);
- yelp("No cpp?");
- /* NOTREACHED */
- }
- else
- {
- /* Older child runs "... | lint >> Tfid" */
- (void)close(0);
- (void)dup(P[0]);
- (void)close(P[0]);
- (void)close(P[1]);
- (void)close(1);
- (void)dup(Tfid);
- (void)close(Tfid);
- (void)execv(Lint1[0], Lint1);
- yelp("No lint1?");
- /* NOTREACHED */
- }
- }
- else
- (void)wait((int *)0);
- }
- /*+MAIN ROUTINE
- */
-
-
- main(ac, av)
- register int ac;
- register char *av[];
- {
- register char *p;
- register int c;
- struct stat Sb;
-
- (void)mktemp(Htemp);
- (void)mktemp(Ttemp);
- Tfid = open(Ttemp, O_RDWR | O_CREAT | O_APPEND, 0666);
- Add(Lint2C++, Lint2, Ttemp, 'T');
-
- /* Parse JCL. */
- while (c = getopt(ac, av, "abhuvxI:D:U:Nnpl:o:"))
- switch (c)
- {
- /* [abhuvx] -- standard lint flags. */
- case 'a': case 'b': case 'h': case 'u': case 'v': case 'x':
- Add(Lint1C++, Lint1, "", c);
- Add(Lint2C++, Lint2, "", c);
- break;
- /* [DIU] -- standard cpp flags. */
- case 'D': case 'I': case 'U':
- Add(CppC++, Cpp, optarg, c);
- break;
- /* p -- portable library. */
- case 'p':
- Add(Lint1C++, Lint1, "", c);
- Add(Lint2C++, Lint1, "", c);
- /* [Nn] -- net or no library. */
- case 'N': case 'n':
- Libstat = c;
- break;
- /* l -- lint library. */
- case 'l':
- Lib(optarg, TRUE);
- break;
- /* o -- output file (redirection without meta chars). */
- case 'o':
- (void)close(1);
- (void)open(optarg, O_WRONLY | O_CREAT | O_TRUNC, 0666);
- break;
- }
-
- Add(Lint1C++, Lint1, Htemp, 'H');
- Add(Lint2C++, Lint2, Htemp, 'H');
- if (signal(SIGINT, SIG_IGN) != SIG_IGN)
- (void)signal(SIGINT, Rupt);
-
- /* Did flags; do filenames and libraries. */
- for (; optind < ac; optind++)
- {
- /* Check for -l.. argument. */
- if (*(p = av[optind]) == '-' && *++p == 'l')
- {
- Lib(++p, TRUE);
- continue;
- }
-
- /* Check for *.c for *.ln arguments. */
- if (p = strrchr(av[optind], '.'))
- {
- if (*++p == 'c' && p[1] == '\0')
- {
- Do(av[optind]);
- continue;
- }
- if (p[0] == 'l' && p[1] == 'n' && p[2] == '\0')
- {
- Lib(av[optind], FALSE);
- continue;
- }
- }
- printf("Not grokked -- \"%s\" -- ignored\n", av[optind]);
- }
-
- /* Any standard C library? */
- switch (Libstat)
- {
- case '\0': Lib("c", TRUE); break;
- case 'N': Lib("netc", TRUE);
- Lib("net", TRUE); break;
- case 'p': Lib("port", TRUE); break;
- }
-
- (void)close(Tfid);
-
- /* If lint1 created anything, run lint2. */
- if (stat(Htemp, &Sb) >= 0 && Sb.st_size >= 0)
- {
- Add(Lint2C, Lint2, Htemp, 'H');
- if (fork() == 0)
- {
- (void)execv(Lint2[0], Lint2);
- yelp("No lint2?");
- /* NOTREACHED */
- }
- (void)wait((int *)0);
- }
-
- /* That's all she wrote. */
- (void)unlink(Htemp);
- (void)unlink(Ttemp);
- exit(0);
- }
-