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 $
29 * ========================================================================
32 * Generates a Makefile and main C file for a crunched executable,
33 * from specs given in a .conf file.
35 #include <sys/types.h>
37 #include <sys/param.h>
47 #define CRUNCH_VERSION "0.2"
49 #define MAXLINELEN 16384
50 #define MAXFIELDS 2048
53 /* internal representation of conf file: */
55 /* simple lists of strings suffice for most parms */
57 typedef struct strlst {
62 /* progs have structure, each field can be set with "special" or calculated */
65 struct prog *next; /* link field */
66 char *name; /* program name */
67 char *ident; /* C identifier for the program name */
71 char *objvar; /* Makefile variable to replace OBJS */
72 strlst_t *objs, *objpaths;
83 strlst_t *buildopts = NULL;
84 strlst_t *srcdirs = NULL;
85 strlst_t *libs = NULL;
88 char confname[MAXPATHLEN], infilename[MAXPATHLEN];
89 char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN];
90 char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN];
91 char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */
92 char *objprefix; /* where are the objects ? */
96 int verbose, readcache; /* options */
98 int makeobj = 0; /* add 'make obj' rules to the makefile */
102 /* general library routines */
104 void status(char *str);
105 void out_of_memory(void);
106 void add_string(strlst_t **listp, char *str);
107 int is_dir(char *pathname);
108 int is_nonempty_file(char *pathname);
110 /* helper routines for main() */
113 void parse_conf_file(void);
114 void gen_outputs(void);
117 int main(int argc, char **argv)
124 *outmkname = *outcfname = *execfname = '\0';
126 p = getenv("MAKEOBJDIRPREFIX");
127 if (p == NULL || *p == '\0')
128 objprefix = "/usr/obj"; /* default */
130 if ((objprefix = strdup(p)) == NULL)
133 while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) {
146 strlcpy(outmkname, optarg, sizeof(outmkname));
149 if ((objprefix = strdup(optarg)) == NULL)
154 strlcpy(outhdrname, optarg, sizeof(outhdrname));
157 strlcpy(outcfname, optarg, sizeof(outcfname));
160 strlcpy(execfname, optarg, sizeof(execfname));
184 strlcpy(infilename, argv[0], sizeof(infilename));
186 /* confname = `basename infilename .conf` */
188 if ((p=strrchr(infilename, '/')) != NULL)
189 strlcpy(confname, p + 1, sizeof(confname));
191 strlcpy(confname, infilename, sizeof(confname));
193 if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
197 snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
199 snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
201 snprintf(execfname, sizeof(execfname), "%s", confname);
203 snprintf(cachename, sizeof(cachename), "%s.cache", confname);
204 snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX",
205 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname);
219 fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ",
220 "[-h <makefile-header-name>] [-m <makefile>]",
221 "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ",
228 * ========================================================================
229 * parse_conf_file subsystem
233 /* helper routines for parse_conf_file */
235 void parse_one_file(char *filename);
236 void parse_line(char *line, int *fc, char **fv, int nf);
237 void add_srcdirs(int argc, char **argv);
238 void add_progs(int argc, char **argv);
239 void add_link(int argc, char **argv);
240 void add_libs(int argc, char **argv);
241 void add_buildopts(int argc, char **argv);
242 void add_special(int argc, char **argv);
244 prog_t *find_prog(char *str);
245 void add_prog(char *progname);
248 void parse_conf_file(void)
250 if (!is_nonempty_file(infilename))
251 errx(1, "fatal: input file \"%s\" not found", infilename);
253 parse_one_file(infilename);
254 if (readcache && is_nonempty_file(cachename)) {
256 parse_one_file(cachename);
261 void parse_one_file(char *filename)
263 char *fieldv[MAXFIELDS];
265 void (*f)(int c, char **v);
267 char line[MAXLINELEN];
269 snprintf(line, sizeof(line), "reading %s", filename);
271 strlcpy(curfilename, filename, sizeof(curfilename));
273 if ((cf = fopen(curfilename, "r")) == NULL) {
274 warn("%s", curfilename);
280 while (fgets(line, MAXLINELEN, cf) != NULL) {
282 parse_line(line, &fieldc, fieldv, MAXFIELDS);
287 if (!strcmp(fieldv[0], "srcdirs"))
289 else if(!strcmp(fieldv[0], "progs"))
291 else if(!strcmp(fieldv[0], "ln"))
293 else if(!strcmp(fieldv[0], "libs"))
295 else if(!strcmp(fieldv[0], "buildopts"))
297 else if(!strcmp(fieldv[0], "special"))
300 warnx("%s:%d: skipping unknown command `%s'",
301 curfilename, linenum, fieldv[0]);
307 warnx("%s:%d: %s %s",
308 curfilename, linenum, fieldv[0],
309 "command needs at least 1 argument, skipping");
318 warn("%s", curfilename);
325 void parse_line(char *line, int *fc, char **fv, int nf)
336 if (*p == '\0' || *p == '#')
342 while (*p && !isspace(*p) && *p != '#')
345 if (*p == '\0' || *p == '#')
352 *p = '\0'; /* needed for '#' case */
356 void add_srcdirs(int argc, char **argv)
360 for (i = 1; i < argc; i++) {
362 add_string(&srcdirs, argv[i]);
364 warnx("%s:%d: `%s' is not a directory, skipping it",
365 curfilename, linenum, argv[i]);
372 void add_progs(int argc, char **argv)
376 for (i = 1; i < argc; i++)
381 void add_prog(char *progname)
385 /* add to end, but be smart about dups */
387 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
388 if (!strcmp(p2->name, progname))
391 p2 = malloc(sizeof(prog_t));
393 memset(p2, 0, sizeof(prog_t));
394 p2->name = strdup(progname);
396 if (!p2 || !p2->name)
407 p2->realsrcdir = NULL;
413 p2->buildopts = NULL;
417 printf("%s\n",progname);
421 void add_link(int argc, char **argv)
424 prog_t *p = find_prog(argv[1]);
427 warnx("%s:%d: no prog %s previously declared, skipping link",
428 curfilename, linenum, argv[1]);
433 for (i = 2; i < argc; i++) {
435 printf("%s\n",argv[i]);
437 add_string(&p->links, argv[i]);
442 void add_libs(int argc, char **argv)
446 for(i = 1; i < argc; i++)
447 add_string(&libs, argv[i]);
451 void add_buildopts(int argc, char **argv)
455 for (i = 1; i < argc; i++)
456 add_string(&buildopts, argv[i]);
460 void add_special(int argc, char **argv)
463 prog_t *p = find_prog(argv[1]);
469 warnx("%s:%d: no prog %s previously declared, skipping special",
470 curfilename, linenum, argv[1]);
475 if (!strcmp(argv[2], "ident")) {
478 if ((p->ident = strdup(argv[3])) == NULL)
480 } else if (!strcmp(argv[2], "srcdir")) {
483 if ((p->srcdir = strdup(argv[3])) == NULL)
485 } else if (!strcmp(argv[2], "objdir")) {
488 if((p->objdir = strdup(argv[3])) == NULL)
490 } else if (!strcmp(argv[2], "objs")) {
492 for (i = 3; i < argc; i++)
493 add_string(&p->objs, argv[i]);
494 } else if (!strcmp(argv[2], "objpaths")) {
496 for (i = 3; i < argc; i++)
497 add_string(&p->objpaths, argv[i]);
498 } else if (!strcmp(argv[2], "keep")) {
500 for(i = 3; i < argc; i++)
501 add_string(&p->keeplist, argv[i]);
502 } else if (!strcmp(argv[2], "objvar")) {
505 if ((p->objvar = strdup(argv[3])) == NULL)
507 } else if (!strcmp(argv[2], "buildopts")) {
509 for (i = 3; i < argc; i++)
510 add_string(&p->buildopts, argv[i]);
511 } else if (!strcmp(argv[2], "lib")) {
512 for (i = 3; i < argc; i++)
513 add_string(&p->libs, argv[i]);
515 warnx("%s:%d: bad parameter name `%s', skipping line",
516 curfilename, linenum, argv[2]);
522 warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
523 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
528 prog_t *find_prog(char *str)
532 for (p = progs; p != NULL; p = p->next)
533 if (!strcmp(p->name, str))
541 * ========================================================================
542 * gen_outputs subsystem
546 /* helper subroutines */
548 void remove_error_progs(void);
549 void fillin_program(prog_t *p);
550 void gen_specials_cache(void);
551 void gen_output_makefile(void);
552 void gen_output_cfile(void);
554 void fillin_program_objs(prog_t *p, char *path);
555 void top_makefile_rules(FILE *outmk);
556 void prog_makefile_rules(FILE *outmk, prog_t *p);
557 void output_strlst(FILE *outf, strlst_t *lst);
558 char *genident(char *str);
559 char *dir_search(char *progname);
562 void gen_outputs(void)
566 for (p = progs; p != NULL; p = p->next)
569 remove_error_progs();
570 gen_specials_cache();
572 gen_output_makefile();
575 "Run \"make -f %s\" to build crunched binary.\n", outmkname);
579 * run the makefile for the program to find which objects are necessary
581 void fillin_program(prog_t *p)
583 char path[MAXPATHLEN];
584 char line[MAXLINELEN];
587 snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
591 p->ident = genident(p->name);
593 /* look for the source directory if one wasn't specified by a special */
595 p->srcdir = dir_search(p->name);
598 /* Determine the actual srcdir (maybe symlinked). */
600 snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`",
604 errx(1, "Can't execute: %s\n", line);
607 fgets(path, sizeof path, f);
609 errx(1, "Can't execute: %s\n", line);
612 errx(1, "Can't perform pwd on: %s\n", p->srcdir);
614 p->realsrcdir = strdup(path);
617 /* Unless the option to make object files was specified the
618 * the objects will be built in the source directory unless
619 * an object directory already exists.
621 if (!makeobj && !p->objdir && p->srcdir) {
622 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
624 if ((p->objdir = strdup(line)) == NULL)
627 p->objdir = p->realsrcdir;
631 * XXX look for a Makefile.{name} in local directory first.
632 * This lets us override the original Makefile.
634 snprintf(path, sizeof(path), "Makefile.%s", p->name);
635 if (is_nonempty_file(path)) {
636 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
640 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
641 if (!p->objs && p->srcdir && is_nonempty_file(path))
642 fillin_program_objs(p, path);
644 if (!p->srcdir && !p->objdir && verbose)
646 "warning: could not find source directory",
647 infilename, p->name);
648 if (!p->objs && verbose)
649 warnx("%s: %s: warning: could not find any .o files",
650 infilename, p->name);
652 if ((!p->srcdir || !p->objdir) && !p->objs)
656 void fillin_program_objs(prog_t *p, char *path)
663 char line[MAXLINELEN];
665 /* discover the objs from the srcdir Makefile */
667 if ((fd = mkstemp(tempfname)) == -1) {
671 if ((f = fdopen(fd, "w")) == NULL) {
672 warn("%s", tempfname);
680 * XXX include outhdrname (e.g. to contain Make variables)
682 if (outhdrname[0] != '\0')
683 fprintf(f, ".include \"%s\"\n", outhdrname);
684 fprintf(f, ".include \"%s\"\n", path);
686 fprintf(f, "BUILDOPTS+=");
687 output_strlst(f, buildopts);
689 fprintf(f, ".if defined(PROG) && !defined(%s)\n", objvar);
690 fprintf(f, "%s=${PROG}.o\n", objvar);
691 fprintf(f, ".endif\n");
692 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
694 fprintf(f, "crunchgen_objs:\n\t@make -f %s $(BUILDOPTS) $(%s_OPTS)",
695 tempfname, p->ident);
696 for (s = p->buildopts; s != NULL; s = s->next)
697 fprintf(f, " %s", s->str);
698 fprintf(f, " loop\n");
702 snprintf(line, MAXLINELEN, "make -f %s crunchgen_objs 2>&1", tempfname);
703 if ((f = popen(line, "r")) == NULL) {
704 warn("submake pipe");
709 while(fgets(line, MAXLINELEN, f)) {
710 if (strncmp(line, "OBJS= ", 6)) {
711 warnx("make error: %s", line);
722 while (*cp && !isspace(*cp))
726 add_string(&p->objs, obj);
732 if ((rc=pclose(f)) != 0) {
733 warnx("make error: make returned %d", rc);
740 void remove_error_progs(void)
744 p1 = NULL; p2 = progs;
747 p1 = p2, p2 = p2->next;
749 /* delete it from linked list */
750 warnx("%s: %s: ignoring program because of errors",
751 infilename, p2->name);
761 void gen_specials_cache(void)
765 char line[MAXLINELEN];
767 snprintf(line, MAXLINELEN, "generating %s", cachename);
770 if ((cachef = fopen(cachename, "w")) == NULL) {
771 warn("%s", cachename);
776 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
778 cachename, infilename, CRUNCH_VERSION);
780 for (p = progs; p != NULL; p = p->next) {
781 fprintf(cachef, "\n");
783 fprintf(cachef, "special %s srcdir %s\n",
786 fprintf(cachef, "special %s objdir %s\n",
789 fprintf(cachef, "special %s objs", p->name);
790 output_strlst(cachef, p->objs);
793 fprintf(cachef, "special %s objpaths", p->name);
794 output_strlst(cachef, p->objpaths);
801 void gen_output_makefile(void)
805 char line[MAXLINELEN];
807 snprintf(line, MAXLINELEN, "generating %s", outmkname);
810 if ((outmk = fopen(outmkname, "w")) == NULL) {
811 warn("%s", outmkname);
816 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
817 outmkname, infilename, CRUNCH_VERSION);
819 if (outhdrname[0] != '\0')
820 fprintf(outmk, ".include \"%s\"\n", outhdrname);
822 top_makefile_rules(outmk);
823 for (p = progs; p != NULL; p = p->next)
824 prog_makefile_rules(outmk, p);
826 fprintf(outmk, "\n# ========\n");
831 void gen_output_cfile(void)
833 extern char *crunched_skel[];
838 char line[MAXLINELEN];
840 snprintf(line, MAXLINELEN, "generating %s", outcfname);
843 if((outcf = fopen(outcfname, "w")) == NULL) {
844 warn("%s", outcfname);
850 "/* %s - generated from %s by crunchgen %s */\n",
851 outcfname, infilename, CRUNCH_VERSION);
853 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
854 for (cp = crunched_skel; *cp != NULL; cp++)
855 fprintf(outcf, "%s\n", *cp);
857 for (p = progs; p != NULL; p = p->next)
858 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
860 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
861 for (p = progs; p != NULL; p = p->next) {
862 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
864 for (s = p->links; s != NULL; s = s->next)
865 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
869 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
870 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
875 char *genident(char *str)
880 * generates a Makefile/C identifier from a program name,
881 * mapping '-' to '_' and ignoring all other non-identifier
882 * characters. This leads to programs named "foo.bar" and
883 * "foobar" to map to the same identifier.
886 if ((n = strdup(str)) == NULL)
888 for (d = s = n; *s != '\0'; s++) {
891 else if (*s == '_' || isalnum(*s))
899 char *dir_search(char *progname)
901 char path[MAXPATHLEN];
905 for (dir = srcdirs; dir != NULL; dir = dir->next) {
906 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
910 if ((srcdir = strdup(path)) == NULL)
919 void top_makefile_rules(FILE *outmk)
923 fprintf(outmk, "LIBS+=");
924 output_strlst(outmk, libs);
927 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
928 fprintf(outmk, "MAKE=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX) "
931 fprintf(outmk, "MAKE=make\n");
935 fprintf(outmk, "BUILDOPTS+=");
936 output_strlst(outmk, buildopts);
939 fprintf(outmk, "CRUNCHED_OBJS=");
940 for (p = progs; p != NULL; p = p->next)
941 fprintf(outmk, " %s.lo", p->name);
942 fprintf(outmk, "\n");
944 fprintf(outmk, "SUBMAKE_TARGETS=");
945 for (p = progs; p != NULL; p = p->next)
946 fprintf(outmk, " %s_make", p->ident);
947 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
948 for (p = progs; p != NULL; p = p->next)
949 fprintf(outmk, " %s_clean", p->ident);
950 fprintf(outmk, "\n\n");
952 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
953 fprintf(outmk, "exe: %s\n", execfname);
954 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n", execfname, execfname);
955 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
956 execfname, execfname);
957 fprintf(outmk, "\tstrip %s\n", execfname);
958 fprintf(outmk, "realclean: clean subclean\n");
959 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
960 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
964 void prog_makefile_rules(FILE *outmk, prog_t *p)
968 fprintf(outmk, "\n# -------- %s\n\n", p->name);
970 fprintf(outmk, "%s_OBJDIR=", p->ident);
972 fprintf(outmk, "%s", p->objdir);
974 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
976 fprintf(outmk, "\n");
978 if (p->srcdir && p->objs) {
979 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
980 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
982 fprintf(outmk, "%s_OBJS=", p->ident);
983 output_strlst(outmk, p->objs);
984 if (p->buildopts != NULL) {
985 fprintf(outmk, "%s_OPTS+=", p->ident);
986 output_strlst(outmk, p->buildopts);
988 fprintf(outmk, "%s_make:\n", p->ident);
989 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
991 fprintf(outmk, "$(MAKE) obj && ");
992 fprintf(outmk, "\\\n");
993 fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
995 fprintf(outmk, "\\\n");
996 fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) "
999 fprintf(outmk, "\n");
1000 fprintf(outmk, "%s_clean:\n", p->ident);
1001 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(MAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1004 fprintf(outmk, "%s_make:\n", p->ident);
1005 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1009 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1011 output_strlst(outmk, p->objpaths);
1013 for (lst = p->objs; lst != NULL; lst = lst->next) {
1014 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1016 fprintf(outmk, "\n");
1019 fprintf(outmk, "%s_LIBS=", p->ident);
1020 output_strlst(outmk, p->libs);
1023 fprintf(outmk, "%s_stub.c:\n", p->name);
1024 fprintf(outmk, "\techo \""
1025 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1026 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1028 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1029 p->name, p->name, p->ident);
1031 fprintf(outmk, " $(%s_LIBS)", p->ident);
1032 fprintf(outmk, "\n");
1033 fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1034 p->name, p->name, p->ident);
1036 fprintf(outmk, " $(%s_LIBS)", p->ident);
1037 fprintf(outmk, "\n");
1038 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1039 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1040 fprintf(outmk, "-k _%s ", lst->str);
1041 fprintf(outmk, "%s.lo\n", p->name);
1044 void output_strlst(FILE *outf, strlst_t *lst)
1046 for (; lst != NULL; lst = lst->next)
1047 fprintf(outf, " %s", lst->str);
1048 fprintf(outf, "\n");
1053 * ========================================================================
1054 * general library routines
1058 void status(char *str)
1060 static int lastlen = 0;
1067 spaces = lastlen - len;
1071 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1077 void out_of_memory(void)
1079 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1083 void add_string(strlst_t **listp, char *str)
1087 /* add to end, but be smart about dups */
1089 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1090 if (!strcmp(p2->str, str))
1093 p2 = malloc(sizeof(strlst_t));
1096 p2->str = strdup(str);
1098 if (!p2 || !p2->str)
1108 int is_dir(char *pathname)
1112 if (stat(pathname, &buf) == -1)
1115 return S_ISDIR(buf.st_mode);
1118 int is_nonempty_file(char *pathname)
1122 if (stat(pathname, &buf) == -1)
1125 return S_ISREG(buf.st_mode) && buf.st_size > 0;