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.2 2003/06/17 04:29:53 dillon 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);
118 int main(int argc, char **argv)
125 *outmkname = *outcfname = *execfname = '\0';
127 p = getenv("MAKEOBJDIRPREFIX");
128 if (p == NULL || *p == '\0')
129 objprefix = "/usr/obj"; /* default */
131 if ((objprefix = strdup(p)) == NULL)
134 while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) {
147 strlcpy(outmkname, optarg, sizeof(outmkname));
150 if ((objprefix = strdup(optarg)) == NULL)
155 strlcpy(outhdrname, optarg, sizeof(outhdrname));
158 strlcpy(outcfname, optarg, sizeof(outcfname));
161 strlcpy(execfname, optarg, sizeof(execfname));
185 strlcpy(infilename, argv[0], sizeof(infilename));
187 /* confname = `basename infilename .conf` */
189 if ((p=strrchr(infilename, '/')) != NULL)
190 strlcpy(confname, p + 1, sizeof(confname));
192 strlcpy(confname, infilename, sizeof(confname));
194 if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf"))
198 snprintf(outmkname, sizeof(outmkname), "%s.mk", confname);
200 snprintf(outcfname, sizeof(outcfname), "%s.c", confname);
202 snprintf(execfname, sizeof(execfname), "%s", confname);
204 snprintf(cachename, sizeof(cachename), "%s.cache", confname);
205 snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX",
206 getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname);
220 fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ",
221 "[-h <makefile-header-name>] [-m <makefile>]",
222 "[-p <obj-prefix>] [-c <c-file-name>] [-e <exec-file>] ",
229 * ========================================================================
230 * parse_conf_file subsystem
234 /* helper routines for parse_conf_file */
236 void parse_one_file(char *filename);
237 void parse_line(char *line, int *fc, char **fv, int nf);
238 void add_srcdirs(int argc, char **argv);
239 void add_progs(int argc, char **argv);
240 void add_link(int argc, char **argv);
241 void add_libs(int argc, char **argv);
242 void add_buildopts(int argc, char **argv);
243 void add_special(int argc, char **argv);
245 prog_t *find_prog(char *str);
246 void add_prog(char *progname);
249 void parse_conf_file(void)
251 if (!is_nonempty_file(infilename))
252 errx(1, "fatal: input file \"%s\" not found", infilename);
254 parse_one_file(infilename);
255 if (readcache && is_nonempty_file(cachename)) {
257 parse_one_file(cachename);
262 void parse_one_file(char *filename)
264 char *fieldv[MAXFIELDS];
266 void (*f)(int c, char **v);
268 char line[MAXLINELEN];
270 snprintf(line, sizeof(line), "reading %s", filename);
272 strlcpy(curfilename, filename, sizeof(curfilename));
274 if ((cf = fopen(curfilename, "r")) == NULL) {
275 warn("%s", curfilename);
281 while (fgets(line, MAXLINELEN, cf) != NULL) {
283 parse_line(line, &fieldc, fieldv, MAXFIELDS);
288 if (!strcmp(fieldv[0], "srcdirs"))
290 else if(!strcmp(fieldv[0], "progs"))
292 else if(!strcmp(fieldv[0], "ln"))
294 else if(!strcmp(fieldv[0], "libs"))
296 else if(!strcmp(fieldv[0], "buildopts"))
298 else if(!strcmp(fieldv[0], "special"))
301 warnx("%s:%d: skipping unknown command `%s'",
302 curfilename, linenum, fieldv[0]);
308 warnx("%s:%d: %s %s",
309 curfilename, linenum, fieldv[0],
310 "command needs at least 1 argument, skipping");
319 warn("%s", curfilename);
326 void parse_line(char *line, int *fc, char **fv, int nf)
337 if (*p == '\0' || *p == '#')
343 while (*p && !isspace(*p) && *p != '#')
346 if (*p == '\0' || *p == '#')
353 *p = '\0'; /* needed for '#' case */
357 void add_srcdirs(int argc, char **argv)
361 for (i = 1; i < argc; i++) {
363 add_string(&srcdirs, argv[i]);
365 warnx("%s:%d: `%s' is not a directory, skipping it",
366 curfilename, linenum, argv[i]);
373 void add_progs(int argc, char **argv)
377 for (i = 1; i < argc; i++)
382 void add_prog(char *progname)
386 /* add to end, but be smart about dups */
388 for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next)
389 if (!strcmp(p2->name, progname))
392 p2 = malloc(sizeof(prog_t));
394 memset(p2, 0, sizeof(prog_t));
395 p2->name = strdup(progname);
397 if (!p2 || !p2->name)
408 p2->realsrcdir = NULL;
414 p2->buildopts = NULL;
418 printf("%s\n",progname);
422 void add_link(int argc, char **argv)
425 prog_t *p = find_prog(argv[1]);
428 warnx("%s:%d: no prog %s previously declared, skipping link",
429 curfilename, linenum, argv[1]);
434 for (i = 2; i < argc; i++) {
436 printf("%s\n",argv[i]);
438 add_string(&p->links, argv[i]);
443 void add_libs(int argc, char **argv)
447 for(i = 1; i < argc; i++)
448 add_string(&libs, argv[i]);
452 void add_buildopts(int argc, char **argv)
456 for (i = 1; i < argc; i++)
457 add_string(&buildopts, argv[i]);
461 void add_special(int argc, char **argv)
464 prog_t *p = find_prog(argv[1]);
470 warnx("%s:%d: no prog %s previously declared, skipping special",
471 curfilename, linenum, argv[1]);
476 if (!strcmp(argv[2], "ident")) {
479 if ((p->ident = strdup(argv[3])) == NULL)
481 } else if (!strcmp(argv[2], "srcdir")) {
484 if ((p->srcdir = strdup(argv[3])) == NULL)
486 } else if (!strcmp(argv[2], "objdir")) {
489 if((p->objdir = strdup(argv[3])) == NULL)
491 } else if (!strcmp(argv[2], "objs")) {
493 for (i = 3; i < argc; i++)
494 add_string(&p->objs, argv[i]);
495 } else if (!strcmp(argv[2], "objpaths")) {
497 for (i = 3; i < argc; i++)
498 add_string(&p->objpaths, argv[i]);
499 } else if (!strcmp(argv[2], "keep")) {
501 for(i = 3; i < argc; i++)
502 add_string(&p->keeplist, argv[i]);
503 } else if (!strcmp(argv[2], "objvar")) {
506 if ((p->objvar = strdup(argv[3])) == NULL)
508 } else if (!strcmp(argv[2], "buildopts")) {
510 for (i = 3; i < argc; i++)
511 add_string(&p->buildopts, argv[i]);
512 } else if (!strcmp(argv[2], "lib")) {
513 for (i = 3; i < argc; i++)
514 add_string(&p->libs, argv[i]);
516 warnx("%s:%d: bad parameter name `%s', skipping line",
517 curfilename, linenum, argv[2]);
523 warnx("%s:%d: too %s arguments, expected \"special %s %s <string>\"",
524 curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]);
529 prog_t *find_prog(char *str)
533 for (p = progs; p != NULL; p = p->next)
534 if (!strcmp(p->name, str))
542 * ========================================================================
543 * gen_outputs subsystem
547 /* helper subroutines */
549 void remove_error_progs(void);
550 void fillin_program(prog_t *p);
551 void gen_specials_cache(void);
552 void gen_output_makefile(void);
553 void gen_output_cfile(void);
555 void fillin_program_objs(prog_t *p, char *path);
556 void top_makefile_rules(FILE *outmk);
557 void prog_makefile_rules(FILE *outmk, prog_t *p);
558 void output_strlst(FILE *outf, strlst_t *lst);
559 char *genident(char *str);
560 char *dir_search(char *progname);
563 void gen_outputs(void)
567 for (p = progs; p != NULL; p = p->next)
570 remove_error_progs();
571 gen_specials_cache();
573 gen_output_makefile();
576 "Run \"make -f %s\" to build crunched binary.\n", outmkname);
580 * run the makefile for the program to find which objects are necessary
582 void fillin_program(prog_t *p)
584 char path[MAXPATHLEN];
585 char line[MAXLINELEN];
588 snprintf(line, MAXLINELEN, "filling in parms for %s", p->name);
592 p->ident = genident(p->name);
594 /* look for the source directory if one wasn't specified by a special */
596 p->srcdir = dir_search(p->name);
599 /* Determine the actual srcdir (maybe symlinked). */
601 snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`",
605 errx(1, "Can't execute: %s\n", line);
608 fgets(path, sizeof path, f);
610 errx(1, "Can't execute: %s\n", line);
613 errx(1, "Can't perform pwd on: %s\n", p->srcdir);
615 p->realsrcdir = strdup(path);
618 /* Unless the option to make object files was specified the
619 * the objects will be built in the source directory unless
620 * an object directory already exists.
622 if (!makeobj && !p->objdir && p->srcdir) {
623 snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir);
625 if ((p->objdir = strdup(line)) == NULL)
628 p->objdir = p->realsrcdir;
632 * XXX look for a Makefile.{name} in local directory first.
633 * This lets us override the original Makefile.
635 snprintf(path, sizeof(path), "Makefile.%s", p->name);
636 if (is_nonempty_file(path)) {
637 snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name);
641 snprintf(path, sizeof(path), "%s/Makefile", p->srcdir);
642 if (!p->objs && p->srcdir && is_nonempty_file(path))
643 fillin_program_objs(p, path);
645 if (!p->srcdir && !p->objdir && verbose)
647 "warning: could not find source directory",
648 infilename, p->name);
649 if (!p->objs && verbose)
650 warnx("%s: %s: warning: could not find any .o files",
651 infilename, p->name);
653 if ((!p->srcdir || !p->objdir) && !p->objs)
657 void fillin_program_objs(prog_t *p, char *path)
664 char line[MAXLINELEN];
666 /* discover the objs from the srcdir Makefile */
668 if ((fd = mkstemp(tempfname)) == -1) {
672 if ((f = fdopen(fd, "w")) == NULL) {
673 warn("%s", tempfname);
681 * XXX include outhdrname (e.g. to contain Make variables)
683 if (outhdrname[0] != '\0')
684 fprintf(f, ".include \"%s\"\n", outhdrname);
685 fprintf(f, ".include \"%s\"\n", path);
687 fprintf(f, "BUILDOPTS+=");
688 output_strlst(f, buildopts);
690 fprintf(f, ".if defined(PROG) && !defined(%s)\n", objvar);
691 fprintf(f, "%s=${PROG}.o\n", objvar);
692 fprintf(f, ".endif\n");
693 fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar);
695 fprintf(f, "crunchgen_objs:\n\t@make -f %s $(BUILDOPTS) $(%s_OPTS)",
696 tempfname, p->ident);
697 for (s = p->buildopts; s != NULL; s = s->next)
698 fprintf(f, " %s", s->str);
699 fprintf(f, " loop\n");
703 snprintf(line, MAXLINELEN, "make -f %s crunchgen_objs 2>&1", tempfname);
704 if ((f = popen(line, "r")) == NULL) {
705 warn("submake pipe");
710 while(fgets(line, MAXLINELEN, f)) {
711 if (strncmp(line, "OBJS= ", 6)) {
712 warnx("make error: %s", line);
723 while (*cp && !isspace(*cp))
727 add_string(&p->objs, obj);
733 if ((rc=pclose(f)) != 0) {
734 warnx("make error: make returned %d", rc);
741 void remove_error_progs(void)
745 p1 = NULL; p2 = progs;
748 p1 = p2, p2 = p2->next;
750 /* delete it from linked list */
751 warnx("%s: %s: ignoring program because of errors",
752 infilename, p2->name);
762 void gen_specials_cache(void)
766 char line[MAXLINELEN];
768 snprintf(line, MAXLINELEN, "generating %s", cachename);
771 if ((cachef = fopen(cachename, "w")) == NULL) {
772 warn("%s", cachename);
777 fprintf(cachef, "# %s - parm cache generated from %s by crunchgen "
779 cachename, infilename, CRUNCH_VERSION);
781 for (p = progs; p != NULL; p = p->next) {
782 fprintf(cachef, "\n");
784 fprintf(cachef, "special %s srcdir %s\n",
787 fprintf(cachef, "special %s objdir %s\n",
790 fprintf(cachef, "special %s objs", p->name);
791 output_strlst(cachef, p->objs);
794 fprintf(cachef, "special %s objpaths", p->name);
795 output_strlst(cachef, p->objpaths);
802 void gen_output_makefile(void)
806 char line[MAXLINELEN];
808 snprintf(line, MAXLINELEN, "generating %s", outmkname);
811 if ((outmk = fopen(outmkname, "w")) == NULL) {
812 warn("%s", outmkname);
817 fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n",
818 outmkname, infilename, CRUNCH_VERSION);
820 if (outhdrname[0] != '\0')
821 fprintf(outmk, ".include \"%s\"\n", outhdrname);
823 top_makefile_rules(outmk);
824 for (p = progs; p != NULL; p = p->next)
825 prog_makefile_rules(outmk, p);
827 fprintf(outmk, "\n# ========\n");
832 void gen_output_cfile(void)
834 extern char *crunched_skel[];
839 char line[MAXLINELEN];
841 snprintf(line, MAXLINELEN, "generating %s", outcfname);
844 if((outcf = fopen(outcfname, "w")) == NULL) {
845 warn("%s", outcfname);
851 "/* %s - generated from %s by crunchgen %s */\n",
852 outcfname, infilename, CRUNCH_VERSION);
854 fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname);
855 for (cp = crunched_skel; *cp != NULL; cp++)
856 fprintf(outcf, "%s\n", *cp);
858 for (p = progs; p != NULL; p = p->next)
859 fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident);
861 fprintf(outcf, "\nstruct stub entry_points[] = {\n");
862 for (p = progs; p != NULL; p = p->next) {
863 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
865 for (s = p->links; s != NULL; s = s->next)
866 fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n",
870 fprintf(outcf, "\t{ EXECNAME, crunched_main },\n");
871 fprintf(outcf, "\t{ NULL, NULL }\n};\n");
876 char *genident(char *str)
881 * generates a Makefile/C identifier from a program name,
882 * mapping '-' to '_' and ignoring all other non-identifier
883 * characters. This leads to programs named "foo.bar" and
884 * "foobar" to map to the same identifier.
887 if ((n = strdup(str)) == NULL)
889 for (d = s = n; *s != '\0'; s++) {
892 else if (*s == '_' || isalnum(*s))
900 char *dir_search(char *progname)
902 char path[MAXPATHLEN];
906 for (dir = srcdirs; dir != NULL; dir = dir->next) {
907 snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname);
911 if ((srcdir = strdup(path)) == NULL)
920 void top_makefile_rules(FILE *outmk)
924 fprintf(outmk, "LIBS+=");
925 output_strlst(outmk, libs);
928 fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix);
929 fprintf(outmk, "MAKE=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX) "
932 fprintf(outmk, "MAKE=make\n");
936 fprintf(outmk, "BUILDOPTS+=");
937 output_strlst(outmk, buildopts);
940 fprintf(outmk, "CRUNCHED_OBJS=");
941 for (p = progs; p != NULL; p = p->next)
942 fprintf(outmk, " %s.lo", p->name);
943 fprintf(outmk, "\n");
945 fprintf(outmk, "SUBMAKE_TARGETS=");
946 for (p = progs; p != NULL; p = p->next)
947 fprintf(outmk, " %s_make", p->ident);
948 fprintf(outmk, "\nSUBCLEAN_TARGETS=");
949 for (p = progs; p != NULL; p = p->next)
950 fprintf(outmk, " %s_clean", p->ident);
951 fprintf(outmk, "\n\n");
953 fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
954 fprintf(outmk, "exe: %s\n", execfname);
955 fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\n", execfname, execfname);
956 fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n",
957 execfname, execfname);
958 fprintf(outmk, "\tstrip %s\n", execfname);
959 fprintf(outmk, "realclean: clean subclean\n");
960 fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
961 fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n");
965 void prog_makefile_rules(FILE *outmk, prog_t *p)
969 fprintf(outmk, "\n# -------- %s\n\n", p->name);
971 fprintf(outmk, "%s_OBJDIR=", p->ident);
973 fprintf(outmk, "%s", p->objdir);
975 fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n",
977 fprintf(outmk, "\n");
979 if (p->srcdir && p->objs) {
980 fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir);
981 fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir);
983 fprintf(outmk, "%s_OBJS=", p->ident);
984 output_strlst(outmk, p->objs);
985 if (p->buildopts != NULL) {
986 fprintf(outmk, "%s_OPTS+=", p->ident);
987 output_strlst(outmk, p->buildopts);
989 fprintf(outmk, "%s_make:\n", p->ident);
990 fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident);
992 fprintf(outmk, "$(MAKE) obj && ");
993 fprintf(outmk, "\\\n");
994 fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) depend &&",
996 fprintf(outmk, "\\\n");
997 fprintf(outmk, "\t\t$(MAKE) $(BUILDOPTS) $(%s_OPTS) "
1000 fprintf(outmk, "\n");
1001 fprintf(outmk, "%s_clean:\n", p->ident);
1002 fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(MAKE) $(BUILDOPTS) clean cleandepend)\n\n",
1005 fprintf(outmk, "%s_make:\n", p->ident);
1006 fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n",
1010 fprintf(outmk, "%s_OBJPATHS=", p->ident);
1012 output_strlst(outmk, p->objpaths);
1014 for (lst = p->objs; lst != NULL; lst = lst->next) {
1015 fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str);
1017 fprintf(outmk, "\n");
1020 fprintf(outmk, "%s_LIBS=", p->ident);
1021 output_strlst(outmk, p->libs);
1024 fprintf(outmk, "%s_stub.c:\n", p->name);
1025 fprintf(outmk, "\techo \""
1026 "int _crunched_%s_stub(int argc, char **argv, char **envp)"
1027 "{return main(argc,argv,envp);}\" >%s_stub.c\n",
1029 fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)",
1030 p->name, p->name, p->ident);
1032 fprintf(outmk, " $(%s_LIBS)", p->ident);
1033 fprintf(outmk, "\n");
1034 fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
1035 p->name, p->name, p->ident);
1037 fprintf(outmk, " $(%s_LIBS)", p->ident);
1038 fprintf(outmk, "\n");
1039 fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident);
1040 for (lst = p->keeplist; lst != NULL; lst = lst->next)
1041 fprintf(outmk, "-k _%s ", lst->str);
1042 fprintf(outmk, "%s.lo\n", p->name);
1045 void output_strlst(FILE *outf, strlst_t *lst)
1047 for (; lst != NULL; lst = lst->next)
1048 fprintf(outf, " %s", lst->str);
1049 fprintf(outf, "\n");
1054 * ========================================================================
1055 * general library routines
1059 void status(char *str)
1061 static int lastlen = 0;
1068 spaces = lastlen - len;
1072 fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " ");
1078 void out_of_memory(void)
1080 err(1, "%s: %d: out of memory, stopping", infilename, linenum);
1084 void add_string(strlst_t **listp, char *str)
1088 /* add to end, but be smart about dups */
1090 for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next)
1091 if (!strcmp(p2->str, str))
1094 p2 = malloc(sizeof(strlst_t));
1097 p2->str = strdup(str);
1099 if (!p2 || !p2->str)
1109 int is_dir(char *pathname)
1113 if (stat(pathname, &buf) == -1)
1116 return S_ISDIR(buf.st_mode);
1119 int is_nonempty_file(char *pathname)
1123 if (stat(pathname, &buf) == -1)
1126 return S_ISREG(buf.st_mode) && buf.st_size > 0;