shithub: scc

Download patch

ref: d82055a0e5f8f818fb086b16f28d05d65b339cad
parent: fa392c44270193bb1f0e3c3570e903a68703c9d0
parent: ef15a4a3654853bb188d80b91578fdbddf462e3b
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Jun 21 04:17:37 EDT 2016

Merge remote-tracking branch 'quinq/master'

--- a/cc1/main.c
+++ b/cc1/main.c
@@ -15,7 +15,7 @@
 int warnings;
 jmp_buf recover;
 
-static char *base, *output;
+static char *name, *output;
 static struct items uflags;
 int onlycpp;
 
@@ -31,7 +31,7 @@
 static void
 usage(void)
 {
-	die(!strcmp(base, "cpp") ?
+	die(!strcmp(name, "cpp") ?
 	    "usage: cpp [-wd] [-D def[=val]]... [-U def]... [-I dir]... "
 	    "[input]" :
 	    "usage: cc1 [-Ewd] [-D def[=val]]... [-U def]... [-I dir]... "
@@ -41,7 +41,7 @@
 int
 main(int argc, char *argv[])
 {
-	char *base;
+	char *cp;
 	int i;
 
 	atexit(clean);
@@ -48,10 +48,7 @@
 	icpp();
 
 	/* if run as cpp, only run the preprocessor */
-	if ((base = strrchr(argv0, '/')))
-		++base;
-	else
-		base = argv0;
+	name = (cp = strrchr(*argv, '/')) ? cp + 1 : *argv;
 
 	ARGBEGIN {
 	case 'D':
@@ -64,7 +61,7 @@
 		incdir(EARGF(usage()));
 		break;
 	case 'U':
-		uflags.s = newitem(uflags.s, uflags.n++, EARGF(usage()));
+		newitem(&uflags, EARGF(usage()));
 		break;
 	case 'd':
 		DBGON();
@@ -85,7 +82,7 @@
 	if (output && !freopen(output, "w", stdout))
 		die("error opening output: %s", strerror(errno));
 
-	if (!strcmp(base, "cpp"))
+	if (!strcmp(name, "cpp"))
 		onlycpp = 1;
 
 	for (i = 0; i < uflags.n; ++i)
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -29,10 +29,11 @@
 
 static struct tool {
 	char   cmd[PATH_MAX];
-	char **args;
 	char   bin[16];
 	char  *outfile;
-	int    nparams, nargs, in, out, init;
+	struct items args;
+	unsigned nparams;
+	int    in, out, init;
 	pid_t  pid;
 } tools[] = {
 	[CC1]    = { .bin = "cc1",   .cmd = PREFIX "/libexec/scc/", },
@@ -56,7 +57,7 @@
 static void
 terminate(void)
 {
-	int i;
+	unsigned i;
 
 	if (!kflag) {
 		for (i = 0; i < objtmp.n; ++i)
@@ -69,10 +70,10 @@
 {
 	struct tool *t = &tools[tool];
 
-	if (t->nargs < 1)
-		t->nargs = 1;
+	if (t->args.n < 1)
+		t->args.n = 1;
 
-	t->args = newitem(t->args, t->nargs++, arg);
+	newitem(&t->args, arg);
 }
 
 static void
@@ -80,10 +81,10 @@
 {
 	struct tool *t = &tools[tool];
 
-	if (t->nargs > 0)
-		t->args[0] = arg;
+	if (t->args.n > 0)
+		t->args.s[0] = arg;
 	else
-		t->args = newitem(t->args, t->nargs++, arg);
+		newitem(&t->args, arg);
 }
 
 static int
@@ -126,7 +127,7 @@
 	}
 
 	setargv0(tool, t->bin);
-	t->nparams = t->nargs;
+	t->nparams = t->args.n;
 	t->init = 1;
 
 	return tool;
@@ -165,7 +166,8 @@
 settool(int tool, char *infile, int nexttool)
 {
 	struct tool *t = &tools[tool];
-	int i, fds[2];
+	unsigned i;
+	int fds[2];
 	static int fdin = -1;
 
 	switch (tool) {
@@ -239,7 +241,7 @@
 			dup2(t->out, 1);
 		if (t->in > -1)
 			dup2(t->in, 0);
-		execvp(t->cmd, t->args);
+		execvp(t->cmd, t->args.s);
 		fprintf(stderr, "scc: execvp %s: %s\n",
 		        t->cmd, strerror(errno));
 		_exit(1);
@@ -277,7 +279,8 @@
 validatetools(void)
 {
 	struct tool *t;
-	int i, tool, st, failed = LAST_TOOL;
+	unsigned i;
+	int tool, st, failed = LAST_TOOL;
 
 	for (tool = 0; tool < LAST_TOOL; ++tool) {
 		t = &tools[tool];
@@ -289,9 +292,9 @@
 			}
 			if (tool >= failed && t->outfile)
 				unlink(t->outfile);
-			for (i = t->nparams; i < t->nargs; ++i)
-				free(t->args[i]);
-			t->nargs = t->nparams;
+			for (i = t->nparams; i < t->args.n; ++i)
+				free(t->args.s[i]);
+			t->args.n = t->nparams;
 			t->pid = 0;
 		}
 	}
@@ -344,7 +347,7 @@
 	}
 
 	if (validatetools())
-		objs->s = newitem(objs->s, objs->n++, outfilename(file, "o"));
+		newitem(objs, outfilename(file, "o"));
 }
 
 static void
--- a/inc/cc.h
+++ b/inc/cc.h
@@ -16,12 +16,12 @@
 
 struct items {
 	char **s;
-	int n;
+	unsigned n;
 };
 
 extern void die(const char *fmt, ...);
 extern void dbg(const char *fmt, ...);
-extern char **newitem(char **array, int num, char *item);
+extern void newitem(struct items *items, char *item);
 extern void *xmalloc(size_t size);
 extern void *xcalloc(size_t nmemb, size_t size);
 extern char *xstrdup(const char *s);
--- a/lib/newitem.c
+++ b/lib/newitem.c
@@ -1,12 +1,12 @@
 #include "../inc/cc.h"
 
-char **
-newitem(char **array, int num, char *item)
+void
+newitem(struct items *items, char *item)
 {
-	char **ar = xrealloc(array, (num + 1) * sizeof(char **));
+	if ((items->n + 1) < items->n)
+		die("newitem: overflow (%u + 1)", items->n);
 
-	ar[num] = item;
-
-	return ar;
+	items->s = xrealloc(items->s, (items->n + 1) * sizeof(char **));
+	items->s[items->n++] = item;
 }