commit e06d5bda89394fa97fb77af7816129685b27899f
Author:     Quentin Rameau <[email protected]>
AuthorDate: Thu Jun 16 16:17:37 2016 +0200
Commit:     Quentin Rameau <[email protected]>
CommitDate: Fri Jun 17 21:41:10 2016 +0200

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

diff --git a/driver/posix/scc.c b/driver/posix/scc.c
index 70d5dd0..603b063 100644
--- a/driver/posix/scc.c
+++ b/driver/posix/scc.c
@@ -46,14 +46,9 @@ static struct tool {
        [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 @@ terminate(void)
 
        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 @@ settool(int tool, char *infile, int nexttool)
                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 @@ static void
 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 @@ build(char *file)
 
        validatetools();
 
-       objs->f = newitem(objs->f, objs->n++, outfilename(file, "o"));
+       objs->s = newitem(objs->s, objs->n++, outfilename(file, "o"));
 }
 
 static void
diff --git a/inc/cc.h b/inc/cc.h
index e801a3f..33f5bca 100644
--- 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 @@ extern int debug;
 #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);
diff --git a/lib/Makefile b/lib/Makefile
index 061384a..892c732 100644
--- 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
 
diff --git a/lib/newitem.c b/lib/newitem.c
new file mode 100644
index 0000000..97a2560
--- /dev/null
+++ b/lib/newitem.c
@@ -0,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;
+}
+

Reply via email to