2 * Copyright (c) 1994 University of Maryland
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of U.M. not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. U.M. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
15 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Author: James da Silva, Systems Design and Analysis Group
23 * Computer Science Department
24 * University of Maryland at College Park
26 * $FreeBSD: src/usr.sbin/crunch/crunchgen/crunchgen.c,v 1.12.2.10 2002/04/14 20:55:21 luigi Exp $
27 * $DragonFly: src/usr.sbin/crunch/crunchgen/crunchgen.c,v 1.4 2005/10/30 23:00:57 swildner Exp $
30 * ========================================================================
33 * Generates a Makefile and main C file for a crunched executable,
34 * from specs given in a .conf file.
36 #include <sys/types.h>
38 #include <sys/param.h>
48 #define CRUNCH_VERSION "0.2"
50 #define MAXLINELEN 16384
51 #define MAXFIELDS 2048
54 /* internal representation of conf file: */
56 /* simple lists of strings suffice for most parms */
58 typedef struct strlst {
63 /* progs have structure, each field can be set with "special" or calculated */
66 struct prog *next; /* link field */
67 char *name; /* program name */
68 char *ident; /* C identifier for the program name */
72 char *objvar; /* Makefile variable to replace OBJS */
73 strlst_t *objs, *objpaths;
84 strlst_t *buildopts = NULL;
85 strlst_t *srcdirs = NULL;
86 strlst_t *libs = NULL;
89 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
90 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
91 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
92 char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */
93 char *objprefix; /* where are the objects ? */
97 int verbose, readcache; /* options */
99 int makeobj = 0; /* add 'make obj' rules to the makefile */
103 /* general library routines */
105 void status(char *str);
106 void out_of_memory(void);
107 void add_string(strlst_t **listp, char *str);
108 int is_dir(char *pathname);
109 int is_nonempty_file(char *pathname);
111 /* helper routines for main() */
114 void parse_conf_file(void);
115 void gen_outputs(void);
119 main(int argc, char **argv)
126 *outmkname = *outcfname = *execfname = '\0';
128 p = getenv("MAKEOBJDIRPREFIX");
129 if (p == NULL || *p == '\0')
130 objprefix = "/usr/obj"; /* default */
132 if ((objprefix = strdup(p)) == NULL)
135 while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) {
148 strlcpy(outmkname, optarg, sizeof(outmkname));
151 if ((objprefix = strdup(optarg)) == NULL)
156 strlcpy(outhdrname, optarg, sizeof(outhdrname));
159 strlcpy(outcfname, optarg, sizeof(outcfname));
162 strlcpy(execfname, optarg, sizeof(execfname));
186 strlcpy(infilename, argv[0], sizeof(infilename));
188 /* confname = `basename infilename .conf` */
190 if ((p=strrchr(infilename, '/')) != NULL)
191 strlcpy(confname, p + 1, sizeof(confname));
193 strlcpy(confname, infilename, sizeof(confname));
195 if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
199 snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
201 snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
203 snprintf(execfname, sizeof(execfname), "%s", confname);
205 snprintf(cachename, sizeof(cachename), "%s.cache", confname);
206 snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX",
207 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname);
222 fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ",
223 "[-h <makefile-header-name>] [-m <makefile>]",
224 "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ",
231 * ========================================================================
232 * parse_conf_file subsystem
236 /* helper routines for parse_conf_file */
238 void parse_one_file(char *filename);
239 void parse_line(char *line, int *fc, char **fv, int nf);
240 void add_srcdirs(int argc, char **argv);
241 void add_progs(int argc, char **argv);
242 void add_link(int argc, char **argv);
243 void add_libs(int argc, char **argv);
244 void add_buildopts(int argc, char **argv);
245 void add_special(int argc, char **argv);
247 prog_t *find_prog(char *str);
248 void add_prog(char *progname);
252 parse_conf_file(void)
254 if (!is_nonempty_file(infilename))
255 errx(1, "fatal: input file \"%s\" not found", infilename);
257 parse_one_file(infilename);
258 if (readcache && is_nonempty_file(cachename)) {
260 parse_one_file(cachename);
266 parse_one_file(char *filename)
268 char *fieldv[MAXFIELDS];
270 void (*f)(int c, char **v);
272 char line[MAXLINELEN];
274 snprintf(line, sizeof(line), "reading %s", filename);
276 strlcpy(curfilename, filename, sizeof(curfilename));
278 if ((cf = fopen(curfilename, "r")) == NULL) {
279 warn("%s", curfilename);
285 while (fgets(line, MAXLINELEN, cf) != NULL) {
287 parse_line(line, &fieldc, fieldv, MAXFIELDS);
292 if (!strcmp(fieldv[0], "srcdirs"))
294 else if(!strcmp(fieldv[0], "progs"))
296 else if(!strcmp(fieldv[0], "ln"))
298 else if(!strcmp(fieldv[0], "libs"))
300 else if(!strcmp(fieldv[0], "buildopts"))
302 else if(!strcmp(fieldv[0], "special"))
305 warnx("%s:%d: skipping unknown command `%s'",
306 curfilename, linenum, fieldv[0]);
312 warnx("%s:%d: %s %s",
313 curfilename, linenum, fieldv[0],
314 "command needs at least 1 argument, skipping");
323 warn("%s", curfilename);
331 parse_line(char *line, int *fc, char **fv, int nf)
342 if (*p == '\0' || *p == '#')
348 while (*p && !isspace(*p) && *p != '#')
351 if (*p == '\0' || *p == '#')
358 *p = '\0'; /* needed for '#' case */
363 add_srcdirs(int argc, char **argv)
367 for (i = 1; i < argc; i++) {
369 add_string(&srcdirs, argv[i]);
371 warnx("%s:%d: `%s' is not a directory, skipping it",
372 curfilename, linenum, argv[i]);
380 add_progs(int argc, char **argv)
384 for (i = 1; i < argc; i++)
390 add_prog(char *progname)
394 /* add to end, but be smart about dups */
396 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
397 if (!strcmp(p2->name, progname))
400 p2 = malloc(sizeof(prog_t));
402 memset(p2, 0, sizeof(prog_t));
403 p2->name = strdup(progname);
405 if (!p2 || !p2->name)
416 p2->realsrcdir = NULL;
422 p2->buildopts = NULL;
426 printf("%s\n",progname);
431 add_link(int argc, char **argv)
434 prog_t *p = find_prog(argv[1]);
437 warnx("%s:%d: no prog %s previously declared, skipping link",
438 curfilename, linenum, argv[1]);
443 for (i = 2; i < argc; i++) {
445 printf("%s\n",argv[i]);
447 add_string(&p->links, argv[i]);
453 add_libs(int argc, char **argv)
457 for(i = 1; i < argc; i++)
458 add_string(&libs, argv[i]);
463 add_buildopts(int argc, char **argv)
467 for (i = 1; i < argc; i++)
468 add_string(&buildopts, argv[i]);
473 add_special(int argc, char **argv)
476 prog_t *p = find_prog(argv[1]);
482 warnx("%s:%d: no prog %s previously declared, skipping special",
483 curfilename, linenum, argv[1]);
488 if (!strcmp(argv[2], "ident")) {
491 if ((p->ident = strdup(argv[3])) == NULL)
493 } else if (!strcmp(argv[2], "srcdir")) {
496 if ((p->srcdir = strdup(argv[3])) == NULL)
498 } else if (!strcmp(argv[2], "objdir")) {
501 if((p->objdir = strdup(argv[3])) == NULL)
503 } else if (!strcmp(argv[2], "objs")) {
505 for (i = 3; i < argc; i++)
506 add_string(&p->objs, argv[i]);
507 } else if (!strcmp(argv[2], "objpaths")) {
509 for (i = 3; i < argc; i++)
510 add_string(&p->objpaths, argv[i]);
511 } else if (!strcmp(argv[2], "keep")) {
513 for(i = 3; i < argc; i++)
514 add_string(&p->keeplist, argv[i]);
515 } else if (!strcmp(argv[2], "objvar")) {
518 if ((p->objvar = strdup(argv[3])) == NULL)
520 } else if (!strcmp(argv[2], "buildopts")) {
522 for (i = 3; i < argc; i++)
523 add_string(&p->buildopts, argv[i]);
524 } else if (!strcmp(argv[2], "lib")) {
525 for (i = 3; i < argc; i++)
526 add_string(&p->libs, argv[i]);
528 warnx("%s:%d: bad parameter name `%s', skipping line",
529 curfilename, linenum, argv[2]);
535 warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
536 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
546 for (p = progs; p != NULL; p = p->next)
547 if (!strcmp(p->name, str))
555 * ========================================================================
556 * gen_outputs subsystem
560 /* helper subroutines */
562 void remove_error_progs(void);
563 void fillin_program(prog_t *p);
564 void gen_specials_cache(void);
565 void gen_output_makefile(void);
566 void gen_output_cfile(void);
568 void fillin_program_objs(prog_t *p, char *path);
569 void top_makefile_rules(FILE *outmk);
570 void prog_makefile_rules(FILE *outmk, prog_t *p);
571 void output_strlst(FILE *outf, strlst_t *lst);
572 char *genident(char *str);
573 char *dir_search(char *progname);
581 for (p = progs; p != NULL; p = p->next)
584 remove_error_progs();
585 gen_specials_cache();
587 gen_output_makefile();
590 "Run \"make -f %s\" to build crunched binary.\n", outmkname);
594 * run the makefile for the program to find which objects are necessary
597 fillin_program(prog_t *p)
599 char path[MAXPATHLEN];
600 char line[MAXLINELEN];
603 snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
607 p->ident = genident(p->name);
609 /* look for the source directory if one wasn't specified by a special */
611 p->srcdir = dir_search(p->name);
614 /* Determine the actual srcdir (maybe symlinked). */
616 snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`",
620 errx(1, "Can't execute: %s\n", line);
623 fgets(path, sizeof path, f);
625 errx(1, "Can't execute: %s\n", line);
628 errx(1, "Can't perform pwd on: %s\n", p->srcdir);
630 p->realsrcdir = strdup(path);
633 /* Unless the option to make object files was specified the
634 * the objects will be built in the source directory unless
635 * an object directory already exists.
637 if (!makeobj && !p->objdir && p->srcdir) {
638 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
640 if ((p->objdir = strdup(line)) == NULL)
643 p->objdir = p->realsrcdir;
647 * XXX look for a Makefile.{name} in local directory first.
648 * This lets us override the original Makefile.
650 snprintf(path, sizeof(path), "Makefile.%s", p->name);
651 if (is_nonempty_file(path)) {
652 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
656 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
657 if (!p->objs && p->srcdir && is_nonempty_file(path))
658 fillin_program_objs(p, path);
660 if (!p->srcdir && !p->objdir && verbose)
662 "warning: could not find source directory",
663 infilename, p->name);
664 if (!p->objs && verbose)
665 warnx("%s: %s: warning: could not find any .o files",
666 infilename, p->name);
668 if ((!p->srcdir || !p->objdir) && !p->objs)
673 fillin_program_objs(prog_t *p, char *path)
680 char line[MAXLINELEN];
682 /* discover the objs from the srcdir Makefile */
684 if ((fd = mkstemp(tempfname)) == -1) {
688 if ((f = fdopen(fd, "w")) == NULL) {
689 warn("%s", tempfname);
697 * XXX include outhdrname (e.g. to contain Make variables)
699 if (outhdrname[0] != '\0')
700 fprintf(f, ".include \"%s\"\n", outhdrname);
701 fprintf(f, ".include \"%s\"\n", path);
703 fprintf(f, "BUILDOPTS+=");
704 output_strlst(f, buildopts);
706 fprintf(f, ".if defined(PROG) && !defined(%s)\n", objvar);
707 fprintf(f, "%s=${PROG}.o\n", objvar);
708 fprintf(f, ".endif\n");
709 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
711 fprintf(f, "crunchgen_objs:\n\t@make -f %s $(BUILDOPTS) $(%s_OPTS)",
712 tempfname, p->ident);
713 for (s = p->buildopts; s != NULL; s = s->next)
714 fprintf(f, " %s", s->str);
715 fprintf(f, " loop\n");
719 snprintf(line, MAXLINELEN, "make -f %s crunchgen_objs 2>&1", tempfname);
720 if ((f = popen(line, "r")) == NULL) {
721 warn("submake pipe");
726 while(fgets(line, MAXLINELEN, f)) {
727 if (strncmp(line, "OBJS= ", 6)) {
728 warnx("make error: %s", line);
739 while (*cp && !isspace(*cp))
743 add_string(&p->objs, obj);
749 if ((rc=pclose(f)) != 0) {
750 warnx("make error: make returned %d", rc);
758 remove_error_progs(void)
762 p1 = NULL; p2 = progs;
765 p1 = p2, p2 = p2->next;
767 /* delete it from linked list */
768 warnx("%s: %s: ignoring program because of errors",
769 infilename, p2->name);
780 gen_specials_cache(void)
784 char line[MAXLINELEN];
786 snprintf(line, MAXLINELEN, "generating %s", cachename);
789 if ((cachef = fopen(cachename, "w")) == NULL) {
790 warn("%s", cachename);
795 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
797 cachename, infilename, CRUNCH_VERSION);
799 for (p = progs; p != NULL; p = p->next) {
800 fprintf(cachef, "\n");
802 fprintf(cachef, "special %s srcdir %s\n",
805 fprintf(cachef, "special %s objdir %s\n",
808 fprintf(cachef, "special %s objs", p->name);
809 output_strlst(cachef, p->objs);
812 fprintf(cachef, "special %s objpaths", p->name);
813 output_strlst(cachef, p->objpaths);
821 gen_output_makefile(void)
825 char line[MAXLINELEN];
827 snprintf(line, MAXLINELEN, "generating %s", outmkname);
830 if ((outmk = fopen(outmkname, "w")) == NULL) {
831 warn("%s", outmkname);
836 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
837 outmkname, infilename, CRUNCH_VERSION);
839 if (outhdrname[0] != '\0')
840 fprintf(outmk, ".include \"%s\"\n", outhdrname);
842 top_makefile_rules(outmk);
843 for (p = progs; p != NULL; p = p->next)
844 prog_makefile_rules(outmk, p);
846 fprintf(outmk, "\n# ========\n");
852 gen_output_cfile(void)
854 extern char *crunched_skel[];
859 char line[MAXLINELEN];
861 snprintf(line, MAXLINELEN, "generating %s", outcfname);
864 if((outcf = fopen(outcfname, "w")) == NULL) {
865 warn("%s", outcfname);
871 "/* %s - generated from %s by crunchgen %s */\n",
872 outcfname, infilename, CRUNCH_VERSION);
874 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
875 for (cp = crunched_skel; *cp != NULL; cp++)
876 fprintf(outcf, "%s\n", *cp);
878 for (p = progs; p != NULL; p = p->next)
879 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
881 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
882 for (p = progs; p != NULL; p = p->next) {
883 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
885 for (s = p->links; s != NULL; s = s->next)
886 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
890 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
891 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
902 * generates a Makefile/C identifier from a program name,
903 * mapping '-' to '_' and ignoring all other non-identifier
904 * characters. This leads to programs named "foo.bar" and
905 * "foobar" to map to the same identifier.
908 if ((n = strdup(str)) == NULL)
910 for (d = s = n; *s != '\0'; s++) {
913 else if (*s == '_' || isalnum(*s))
922 dir_search(char *progname)
924 char path[MAXPATHLEN];
928 for (dir = srcdirs; dir != NULL; dir = dir->next) {
929 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
933 if ((srcdir = strdup(path)) == NULL)
943 top_makefile_rules(FILE *outmk)
947 fprintf(outmk, "LIBS+=");
948 output_strlst(outmk, libs);
951 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
952 fprintf(outmk, "MAKE=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX) "
955 fprintf(outmk, "MAKE=make\n");
959 fprintf(outmk, "BUILDOPTS+=");
960 output_strlst(outmk, buildopts);
963 fprintf(outmk, "CRUNCHED_OBJS=");
964 for (p = progs; p != NULL; p = p->next)
965 fprintf(outmk, " %s.lo", p->name);
966 fprintf(outmk, "\n");
968 fprintf(outmk, "SUBMAKE_TARGETS=");
969 for (p = progs; p != NULL; p = p->next)
970 fprintf(outmk, " %s_make", p->ident);
971 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
972 for (p = progs; p != NULL; p = p->next)
973 fprintf(outmk, " %s_clean", p->ident);
974 fprintf(outmk, "\n\n");
976 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
977 fprintf(outmk, "exe: %s\n", execfname);
978 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n", execfname, execfname);
979 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
980 execfname, execfname);
981 fprintf(outmk, "\tstrip %s\n", execfname);
982 fprintf(outmk, "realclean: clean subclean\n");
983 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
984 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
989 prog_makefile_rules(FILE *outmk, prog_t *p)
993 fprintf(outmk, "\n# -------- %s\n\n", p->name);
995 fprintf(outmk, "%s_OBJDIR=", p->ident);
997 fprintf(outmk, "%s", p->objdir);
999 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
1001 fprintf(outmk, "\n");
1003 if (p->srcdir && p->objs) {
1004 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
1005 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
1007 fprintf(outmk, "%s_OBJS=", p->ident);
1008 output_strlst(outmk, p->objs);
1009 if (p->buildopts != NULL) {
1010 fprintf(outmk, "%s_OPTS+=", p->ident);
1011 output_strlst(outmk, p->buildopts);
1013 fprintf(outmk, "%s_make:\n", p->ident);
1014 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
1016 fprintf(outmk, "$(MAKE) obj && ");
1017 fprintf(outmk, "\\\n");
1018 fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
1020 fprintf(outmk, "\\\n");
1021 fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) "
1023 p->ident, p->ident);
1024 fprintf(outmk, "\n");
1025 fprintf(outmk, "%s_clean:\n", p->ident);
1026 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(MAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1029 fprintf(outmk, "%s_make:\n", p->ident);
1030 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1034 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1036 output_strlst(outmk, p->objpaths);
1038 for (lst = p->objs; lst != NULL; lst = lst->next) {
1039 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1041 fprintf(outmk, "\n");
1044 fprintf(outmk, "%s_LIBS=", p->ident);
1045 output_strlst(outmk, p->libs);
1048 fprintf(outmk, "%s_stub.c:\n", p->name);
1049 fprintf(outmk, "\techo \""
1050 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1051 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1053 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1054 p->name, p->name, p->ident);
1056 fprintf(outmk, " $(%s_LIBS)", p->ident);
1057 fprintf(outmk, "\n");
1058 fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1059 p->name, p->name, p->ident);
1061 fprintf(outmk, " $(%s_LIBS)", p->ident);
1062 fprintf(outmk, "\n");
1063 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1064 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1065 fprintf(outmk, "-k _%s ", lst->str);
1066 fprintf(outmk, "%s.lo\n", p->name);
1070 output_strlst(FILE *outf, strlst_t *lst)
1072 for (; lst != NULL; lst = lst->next)
1073 fprintf(outf, " %s", lst->str);
1074 fprintf(outf, "\n");
1079 * ========================================================================
1080 * general library routines
1087 static int lastlen = 0;
1094 spaces = lastlen - len;
1098 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1107 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1112 add_string(strlst_t **listp, char *str)
1116 /* add to end, but be smart about dups */
1118 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1119 if (!strcmp(p2->str, str))
1122 p2 = malloc(sizeof(strlst_t));
1125 p2->str = strdup(str);
1127 if (!p2 || !p2->str)
1138 is_dir(char *pathname)
1142 if (stat(pathname, &buf) == -1)
1145 return S_ISDIR(buf.st_mode);
1149 is_nonempty_file(char *pathname)
1153 if (stat(pathname, &buf) == -1)
1156 return S_ISREG(buf.st_mode) && buf.st_size > 0;