shithub: scc

Download patch

ref: eedc3e57df3796c14bda0abad2da4aaff1e17e7e
parent: 672733e1e0d5279fd99b235b2b239011dde67e31
author: Quentin Rameau <quinq@fifth.space>
date: Thu Jun 16 12:17:37 EDT 2016

[lib][driver] move newitem() + related struct to lib

--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -46,14 +46,9 @@
 	[STRIP]  = { .bin = "strip", .cmd = "strip", },
 };
 
-struct objects {
-	char **f;
-	int n;
-};
-
 char *argv0;
 static char *arch, *outfile;
-static struct objects objtmp, objout;
+static struct items objtmp, objout;
 static int Eflag, Sflag, cflag, kflag, sflag;
 
 static void
@@ -75,20 +70,10 @@
 
 	if (!kflag) {
 		for (i = 0; i < objtmp.n; ++i)
-			unlink(objtmp.f[i]);
+			unlink(objtmp.s[i]);
 	}
 }
 
-static char **
-newitem(char **array, int num, char *item)
-{
-	char **ar = xrealloc(array, (num + 1) * sizeof(char **));
-
-	ar[num] = item;
-
-	return ar;
-}
-
 static void
 addarg(int tool, char *arg)
 {
@@ -212,14 +197,14 @@
 		break;
 	case LD:
 		for (i = 0; i < objtmp.n; ++i)
-			addarg(tool, xstrdup(objtmp.f[i]));
+			addarg(tool, xstrdup(objtmp.s[i]));
 		for (i = 0; i < objout.n; ++i)
-			addarg(tool, xstrdup(objout.f[i]));
+			addarg(tool, xstrdup(objout.s[i]));
 		break;
 	case STRIP:
 		if (cflag || kflag) {
 			for (i = 0; i < objout.n; ++i)
-				addarg(tool, xstrdup(objout.f[i]));
+				addarg(tool, xstrdup(objout.s[i]));
 		}
 		if (!cflag && tools[LD].outfile)
 			addarg(tool, tools[LD].outfile);
@@ -322,7 +307,7 @@
 build(char *file)
 {
 	int tool = toolfor(file), nexttool;
-	struct objects *objs = (tool == LD || cflag || kflag) ?
+	struct items *objs = (tool == LD || cflag || kflag) ?
 	                       &objout : &objtmp;
 
 	for (; tool < LAST_TOOL; tool = nexttool) {
@@ -364,7 +349,7 @@
 
 	validatetools();
 
-	objs->f = newitem(objs->f, objs->n++, outfilename(file, "o"));
+	objs->s = newitem(objs->s, objs->n++, outfilename(file, "o"));
 }
 
 static void
--- a/inc/cc.h
+++ b/inc/cc.h
@@ -1,4 +1,6 @@
 /* See LICENSE file for copyright and license details. */
+#include <sys/types.h>
+
 #ifndef NDEBUG
 extern int debug;
 #define DBG(fmt, ...) dbg(fmt, __VA_ARGS__)
@@ -12,8 +14,14 @@
 #define PREFIX "/usr/local/"
 #endif
 
+struct items {
+	char **s;
+	int n;
+};
+
 extern void die(const char *fmt, ...);
 extern void dbg(const char *fmt, ...);
+extern char **newitem(char **array, int num, 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/Makefile
+++ b/lib/Makefile
@@ -2,7 +2,7 @@
 .POSIX:
 include ../config.mk
 
-OBJS = die.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o debug.o
+OBJS = debug.o die.o newitem.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o
 
 all: libcc.a
 
--- /dev/null
+++ b/lib/newitem.c
@@ -1,0 +1,12 @@
+#include "../inc/cc.h"
+
+char **
+newitem(char **array, int num, char *item)
+{
+	char **ar = xrealloc(array, (num + 1) * sizeof(char **));
+
+	ar[num] = item;
+
+	return ar;
+}
+