[net.sources] file include follower

gsp@ulysses.UUCP (Gary Perlman) (05/29/84)

Maybe this should only go to net.sources, but it is pretty small.
I found it under a log.
I hope this is useful to all the people who have asked for it.
	Gary Perlman	BTL MH 5D-105	(201) 582-3624	ulysses!gsp

#include <stdio.h>
#include <ctype.h>
main (argc, argv) char **argv;
	{
	while (--argc) include (*++argv, 0);
	}

include (file, depth)
	{
	int 	indent;
	FILE	*ioptr;
	for (indent = 0; indent < depth; indent++) putchar ('\t');
	if (ioptr = fopen (file, "r"))
		{
		char	line[BUFSIZ];
		register char *lptr;
		printf ("%s\n", file);
		while (fgets (lptr = line, BUFSIZ, ioptr))
			if (*line == '#')
				{
				lptr++;
				while (isspace (*lptr)) lptr++;
				if (!strncmp (lptr, "include", 7))
					{
					int 	matcher;
					char	*eptr;
					lptr += 7;
					while (isspace (*lptr)) lptr++;
					matcher = *lptr++;
					if (matcher == '<') matcher = '>';
					for (eptr = lptr; *eptr && *eptr != matcher; eptr++);
					*eptr = NULL;
					if (matcher == '>')
						{
						char	fullname[BUFSIZ];
						sprintf (fullname, "/usr/include/%s", lptr);
						lptr = fullname;
						}
					include (lptr, depth+1);
					}
				}
		fclose (ioptr);
		}
	else printf ("%s: can't open!\n", file);
	}

kendall@wjh12.UUCP (Sam Kendall) (05/30/84)

The posted "#include follower" has the following bugs:

(1) The path specified in #include should be interpreted relative to the
    directory in which the file containing the #include resides.  In the
    posted program, all paths are interpreted relative to the current
    directory.  This makes a difference for commands like
	cc dir/file.c
    where file.c includes files in its own directory, and for nested
    #includes.
(2) Include lines like
	#include "stdio.h"
    (with "" rather than <>) should scan /usr/include after the includer
    file's directory, but they do not (I think; I didn't test the program).
(3) It does not handle -I options.
(4) It should exit(0) but does not.
(5) Error messages are posted to stdout rather than stderr.  Since
    #includes inside #ifdefs may legitimately fail, the error message
    will mess up any automatic use of this program.

A program to do all these things right is considerably longer.  Probably
one was posted in the past, but if not, I can dig mine up.

	Sam Kendall	{allegra,ihnp4,ima,amd70}!wjh12!kendall
	Delft Consulting Corp.	    decvax!genrad!wjh12!kendall