libscc: Add system dependant functions - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit cb8145813e5414d66e2416feb0026f3d70ae9abb
 (DIR) parent edd82a7380825ecc35d98c0db5ae602b977d3cbd
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
       Date:   Sun, 10 Mar 2024 13:30:50 +0100
       
       libscc: Add system dependant functions
       
       These functions were placed in src/cmd/posix.c and it made
       impossible to reuse them from a different place, for example
       from libmach. To solve this problem all the functions were
       moved to libscc to share them between all the modules.
       
       Diffstat:
         M include/scc/scc/scc.h               |      18 ++++++++++++++++++
         M src/cmd/Makefile                    |      12 ++++--------
         M src/cmd/ar.c                        |      11 +++++------
         M src/cmd/deps.mk                     |       3 ---
         D src/cmd/posix.c                     |      59 -------------------------------
         M src/cmd/ranlib.c                    |       8 ++------
         D src/cmd/sys.h                       |      14 --------------
         M src/libscc/Makefile                 |       5 +++++
         A src/libscc/canonical-posix.c        |      10 ++++++++++
         A src/libscc/fromepoch-posix.c        |       9 +++++++++
         A src/libscc/getstat-posix.c          |      24 ++++++++++++++++++++++++
         A src/libscc/setstat-posix.c          |      23 +++++++++++++++++++++++
         A src/libscc/totime-posix.c           |       9 +++++++++
       
       13 files changed, 109 insertions(+), 96 deletions(-)
       ---
 (DIR) diff --git a/include/scc/scc/scc.h b/include/scc/scc/scc.h
       @@ -22,6 +22,16 @@ struct items {
                unsigned n;
        };
        
       +#ifdef CLOCKS_PER_SEC
       +struct fprop {
       +        unsigned uid;
       +        unsigned gid;
       +        unsigned long mode;
       +        long size;
       +        time_t time;
       +};
       +#endif
       +
        typedef struct alloc Alloc;
        
        extern void die(const char *fmt, ...);
       @@ -37,3 +47,11 @@ extern void *new(Alloc *allocp);
        extern void delete(Alloc *allocp, void *p);
        extern int casecmp(const char *s1, const char *s2);
        extern unsigned genhash(char *name);
       +extern char *canonical(char *);
       +
       +#ifdef CLOCKS_PER_SEC
       +extern long long fromepoch(time_t);
       +extern time_t totime(long long);
       +extern int getstat(char *, struct fprop *);
       +extern int setstat(char *, struct fprop *);
       +#endif
 (DIR) diff --git a/src/cmd/Makefile b/src/cmd/Makefile
       @@ -21,10 +21,6 @@ TARGET =\
                $(BINDIR)/scc-addr2line\
                $(LIBEXEC)/scc/qbe\
        
       -RANLIBOBJS = ranlib.o $(DRIVER).o
       -AROBJS = ar.o $(DRIVER).o
       -
       -
        all: $(TARGET) $(DIRS)
        
        qbe/.git:
       @@ -47,8 +43,8 @@ $(BINDIR)/scc-strip: strip.o $(LIBMACH) $(LIBSCC)
        $(BINDIR)/scc-size: size.o $(LIBMACH) $(LIBSCC)
                $(CC) $(PROJ_LDFLAGS) size.o -lmach -lscc $(PROJ_LDLIBS) -o $@
        
       -$(BINDIR)/scc-ranlib: $(RANLIBOBJS) $(LIBMACH) $(LIBSCC)
       -        $(CC) $(PROJ_LDFLAGS) $(RANLIBOBJS) -lmach -lscc $(PROJ_LDLIBS) -o $@
       +$(BINDIR)/scc-ranlib: ranlib.o $(LIBMACH) $(LIBSCC)
       +        $(CC) $(PROJ_LDFLAGS) ranlib.o -lmach -lscc $(PROJ_LDLIBS) -o $@
        
        $(BINDIR)/scc-objdump: objdump.o $(LIBMACH)
                $(CC) $(PROJ_LDFLAGS) objdump.o -lmach $(PROJ_LDLIBS) -o $@
       @@ -59,7 +55,7 @@ $(BINDIR)/scc-objcopy: objcopy.o $(LIBMACH)
        $(BINDIR)/scc-addr2line: addr2line.o $(LIBMACH) $(LIBSCC)
                $(CC) $(PROJ_LDFLAGS) addr2line.o -lmach -lscc $(PROJ_LDLIBS) -o $@
        
       -$(BINDIR)/scc-ar: $(AROBJS)
       -        $(CC) $(PROJ_LDFLAGS) $(AROBJS) $(PROJ_LDLIBS) -o $@
       +$(BINDIR)/scc-ar: ar.o $(LIBSCC)
       +        $(CC) $(PROJ_LDFLAGS) ar.o $(PROJ_LDLIBS) -lscc -o $@
        
        include deps.mk
 (DIR) diff --git a/src/cmd/ar.c b/src/cmd/ar.c
       @@ -6,9 +6,8 @@
        #include <string.h>
        #include <time.h>
        
       -#include "sys.h"
       -
        #include <scc/ar.h>
       +#include <scc/scc.h>
        #include <scc/arg.h>
        
        enum {
       @@ -26,6 +25,7 @@ char *argv0;
        
        static int bflag, vflag, cflag, lflag, uflag, aflag;
        static char *arfile, *posname;
       +static char invalidchars[] = " ";
        
        struct member {
                FILE *src;
       @@ -123,7 +123,7 @@ archive(char *pname, FILE *to, char letter)
        {
                int c;
                FILE *from;
       -        char mtime[13], *fname;
       +        char *fname;
                struct fprop prop;
        
                fname = canonical(pname);
       @@ -135,11 +135,10 @@ archive(char *pname, FILE *to, char letter)
                if (getstat(pname, &prop) < 0)
                        error("error getting '%s' attributes", pname);
        
       -        strftime(mtime, sizeof(mtime), "%s", gmtime(&prop.time));
                fprintf(to,
       -                "%-16.16s%-12s%-6u%-6u%-8lo%-10ld`\n",
       +                "%-16.16s%-12lld%-6u%-6u%-8lo%-10ld`\n",
                        fname,
       -                mtime,
       +                fromepoch(prop.time),
                        prop.uid,
                        prop.gid,
                        prop.mode,
 (DIR) diff --git a/src/cmd/deps.mk b/src/cmd/deps.mk
       @@ -3,7 +3,6 @@ addr2line.o: $(INCDIR)/scc/scc/arg.h
        addr2line.o: $(INCDIR)/scc/scc/mach.h
        ar.o: $(INCDIR)/scc/scc/ar.h
        ar.o: $(INCDIR)/scc/scc/arg.h
       -ar.o: sys.h
        as/expr.o: $(INCDIR)/scc/scc/scc.h
        as/expr.o: as/as.h
        as/ins.o: $(INCDIR)/scc/scc/scc.h
       @@ -199,7 +198,6 @@ nm.o: $(INCDIR)/scc/scc/arg.h
        nm.o: $(INCDIR)/scc/scc/mach.h
        objdump.o: $(INCDIR)/scc/scc/arg.h
        objdump.o: $(INCDIR)/scc/scc/mach.h
       -posix.o: sys.h
        qbe/alias.o: qbe/all.h
        qbe/amd64/emit.o: qbe/amd64/all.h
        qbe/amd64/isel.o: qbe/amd64/all.h
       @@ -228,7 +226,6 @@ ranlib.o: $(INCDIR)/scc/scc/ar.h
        ranlib.o: $(INCDIR)/scc/scc/arg.h
        ranlib.o: $(INCDIR)/scc/scc/mach.h
        ranlib.o: $(INCDIR)/scc/scc/scc.h
       -ranlib.o: sys.h
        size.o: $(INCDIR)/scc/scc/ar.h
        size.o: $(INCDIR)/scc/scc/arg.h
        size.o: $(INCDIR)/scc/scc/mach.h
 (DIR) diff --git a/src/cmd/posix.c b/src/cmd/posix.c
       @@ -1,59 +0,0 @@
       -#include <sys/types.h>
       -#include <sys/time.h>
       -#include <sys/stat.h>
       -#include <string.h>
       -#include <unistd.h>
       -#include <utime.h>
       -
       -#include <limits.h>
       -
       -#include "sys.h"
       -
       -const char invalidchars[] = " ";
       -
       -time_t
       -totime(long long t)
       -{
       -        return t;
       -}
       -
       -char *
       -canonical(char *path)
       -{
       -        char *name = strrchr(path, '/');
       -        return (name && name[1]) ? name+1 : path;
       -}
       -
       -int
       -getstat(char *fname, struct fprop *prop)
       -{
       -        struct stat st;
       -
       -        if (stat(fname, &st) < 0)
       -                return -1;
       -        if (st.st_size > LONG_MAX)
       -                return -1;
       -        prop->uid = st.st_uid;
       -        prop->gid = st.st_gid;
       -        prop->mode = st.st_mode;
       -        prop->time = st.st_mtime;
       -        prop->size = st.st_size;
       -
       -        return 0;
       -}
       -
       -int
       -setstat(char *fname, struct fprop *prop)
       -{
       -        struct utimbuf ut = {prop->time, prop->time};
       -
       -        if (chown(fname, prop->uid, prop->gid) < 0) {
       -                if (chown(fname, getuid(), getgid()) < 0)
       -                        return -1;
       -        }
       -        if (chmod(fname, prop->mode) < 0)
       -                return -1;
       -        if (utime(fname, &ut) < 0)
       -                return -1;
       -        return 0;
       -}
 (DIR) diff --git a/src/cmd/ranlib.c b/src/cmd/ranlib.c
       @@ -12,8 +12,6 @@
        #include <scc/mach.h>
        #include <scc/scc.h>
        
       -#include "sys.h"
       -
        #define NR_SYMDEF 32
        
        typedef struct symdef Symdef;
       @@ -230,7 +228,6 @@ static void
        merge(FILE *to, struct fprop *prop, FILE *lib, FILE *idx)
        {
                int c;
       -        char mtime[13];
                struct ar_hdr first;
        
                rewind(lib);
       @@ -247,11 +244,10 @@ merge(FILE *to, struct fprop *prop, FILE *lib, FILE *idx)
        
                fwrite(ARMAG, SARMAG, 1, to);
        
       -        strftime(mtime, sizeof(mtime), "%s", gmtime(&prop->time));
                fprintf(to,
       -                "%-16.16s%-12s%-6u%-6u%-8lo%-10ld`\n",
       +                "%-16.16s%-12lld%-6u%-6u%-8lo%-10ld`\n",
                        namidx,
       -                mtime,
       +                fromepoch(prop->time),
                        prop->uid,
                        prop->gid,
                        prop->mode,
 (DIR) diff --git a/src/cmd/sys.h b/src/cmd/sys.h
       @@ -1,14 +0,0 @@
       -struct fprop {
       -        unsigned uid;
       -        unsigned gid;
       -        unsigned long mode;
       -        long size;
       -        time_t time;
       -};
       -
       -extern const char invalidchars[];
       -
       -extern time_t totime(long long t);
       -extern char *canonical(char *path);
       -extern int getstat(char *fname, struct fprop *prop);
       -extern int setstat(char *fname, struct fprop *prop);
 (DIR) diff --git a/src/libscc/Makefile b/src/libscc/Makefile
       @@ -14,6 +14,11 @@ OBJS =\
                alloc.o\
                casecmp.o\
                genhash.o\
       +        fromepoch-$(DRIVER).o\
       +        totime-$(DRIVER).o\
       +        canonical-$(DRIVER).o\
       +        getstat-$(DRIVER).o\
       +        setstat-$(DRIVER).o\
        
        TARGET = $(LIBDIR)/scc/libscc.a
        
 (DIR) diff --git a/src/libscc/canonical-posix.c b/src/libscc/canonical-posix.c
       @@ -0,0 +1,10 @@
       +#include <string.h>
       +
       +#include <scc/scc.h>
       +
       +char *
       +canonical(char *path)
       +{
       +        char *name = strrchr(path, '/');
       +        return (name && name[1]) ? name+1 : path;
       +}
 (DIR) diff --git a/src/libscc/fromepoch-posix.c b/src/libscc/fromepoch-posix.c
       @@ -0,0 +1,9 @@
       +#include <time.h>
       +
       +#include <scc/scc.h>
       +
       +long long
       +fromepoch(time_t t)
       +{
       +        return t;
       +}
 (DIR) diff --git a/src/libscc/getstat-posix.c b/src/libscc/getstat-posix.c
       @@ -0,0 +1,24 @@
       +#include <sys/stat.h>
       +
       +#include <limits.h>
       +#include <time.h>
       +
       +#include <scc/scc.h>
       +
       +int
       +getstat(char *fname, struct fprop *prop)
       +{
       +        struct stat st;
       +
       +        if (stat(fname, &st) < 0)
       +                return -1;
       +        if (st.st_size > LONG_MAX)
       +                return -1;
       +        prop->uid = st.st_uid;
       +        prop->gid = st.st_gid;
       +        prop->mode = st.st_mode;
       +        prop->time = st.st_mtime;
       +        prop->size = st.st_size;
       +
       +        return 0;
       +}
 (DIR) diff --git a/src/libscc/setstat-posix.c b/src/libscc/setstat-posix.c
       @@ -0,0 +1,23 @@
       +#include <sys/stat.h>
       +#include <unistd.h>
       +#include <utime.h>
       +
       +#include <time.h>
       +
       +#include <scc/scc.h>
       +
       +int
       +setstat(char *fname, struct fprop *prop)
       +{
       +        struct utimbuf ut = {prop->time, prop->time};
       +
       +        if (chown(fname, prop->uid, prop->gid) < 0) {
       +                if (chown(fname, getuid(), getgid()) < 0)
       +                        return -1;
       +        }
       +        if (chmod(fname, prop->mode) < 0)
       +                return -1;
       +        if (utime(fname, &ut) < 0)
       +                return -1;
       +        return 0;
       +}
 (DIR) diff --git a/src/libscc/totime-posix.c b/src/libscc/totime-posix.c
       @@ -0,0 +1,9 @@
       +#include <time.h>
       +
       +#include <scc/scc.h>
       +
       +time_t
       +totime(long long t)
       +{
       +        return t;
       +}