Fix issue of host malloc() ruining the heap in bin/sh, fix pathnames in cc, diff...
authorNick Downing <downing.nick@gmail.com>
Tue, 31 Jan 2017 03:03:29 +0000 (14:03 +1100)
committerNick Downing <downing.nick@gmail.com>
Tue, 31 Jan 2017 03:03:46 +0000 (14:03 +1100)
16 files changed:
bin/Makefile
bin/cc.c
bin/diff/diff.c
bin/diff/diffdir.c
bin/ld.c
bin/sh/Makefile
bin/sh/blok.c
bin/sh/defs.h
lib/libc/linux/sys/Makefile
lib/libc/linux/sys/chmod.c
lib/libc/linux/sys/fchmod.c
lib/libc/linux/sys/linux.c
lib/libc/linux/sys/linux.h
lib/libc/linux/sys/readlink.c [new file with mode: 0644]
lib/libc/linux/sys/vopen.c
lib/libc/sys/vfork.c

index 824aa5c..b8bc782 100644 (file)
@@ -58,12 +58,21 @@ ${STD} ${SETUID} ${OPERATOR} ${TTY} mv cp strip:
 
 # take care with cp, strip, and mv, install uses them
 install:
+# minor adjustments to how the difficult programs are installed
+#      install -s cp ${DESTDIR}/bin/newcp; \
+#              mv ${DESTDIR}/bin/newcp ${DESTDIR}/bin/cp;
+#      install -s strip ${DESTDIR}/bin/newstrip; \
+#              mv ${DESTDIR}/bin/newstrip ${DESTDIR}/bin/strip;
+#      rm -f ${DESTDIR}/bin/mv; cp mv ${DESTDIR}/bin/mv; rm -f mv; \
+#              strip ${DESTDIR}/bin/mv; chmod 755 ${DESTDIR}/bin/mv
        install -s cp ${DESTDIR}/bin/newcp; \
-               mv ${DESTDIR}/bin/newcp ${DESTDIR}/bin/cp;
+               mv ${DESTDIR}/bin/newcp ${DESTDIR}/bin/cp
        install -s strip ${DESTDIR}/bin/newstrip; \
-               mv ${DESTDIR}/bin/newstrip ${DESTDIR}/bin/strip;
-       rm -f ${DESTDIR}/bin/mv; cp mv ${DESTDIR}/bin/mv; rm -f mv; \
-               strip ${DESTDIR}/bin/mv; chmod 755 ${DESTDIR}/bin/mv
+               mv ${DESTDIR}/bin/newstrip ${DESTDIR}/bin/strip
+       install -s mv ${DESTDIR}/bin/newmv; \
+               cp ${DESTDIR}/bin/newmv ${DESTDIR}/bin/mv; \
+               rm ${DESTDIR}/bin/newmv
+# to here
        -for i in ${SUBDIR}; do \
                (cd $$i; make ${MFLAGS} DESTDIR=${DESTDIR} install); done
        -for i in ${SCRIPT}; do (install -c $$i.sh ${DESTDIR}/bin/$$i); done
index a0c4588..1ea6cb4 100644 (file)
--- a/bin/cc.c
+++ b/bin/cc.c
@@ -20,6 +20,15 @@ static       char sccsid[] = "@(#)cc.c 4.13 9/18/85";
 /*#include <signal.h>*/
 /*#include <sys/dir.h>*/
 
+#ifdef X_
+char   *cpp;
+char   *ccom;
+char   *sccom;
+char   *c2;
+char   *as;
+char   *ld;
+char   *crt0;
+#else
 char   *cpp = "/lib/cpp";
 char   *ccom = "/lib/ccom";
 char   *sccom = "/lib/sccom";
@@ -27,6 +36,7 @@ char  *c2 = "/lib/c2";
 char   *as = "/bin/as";
 char   *ld = "/bin/ld";
 char   *crt0 = "/lib/crt0.o";
+#endif
 
 char   tmp0[30];               /* big enough for /tmp/ctm%05.5d */
 char   *tmp1, *tmp2, *tmp3, *tmp4, *tmp5;
@@ -65,10 +75,26 @@ char *savestr __P((register char *cp));
 char *strspl __P((char *left, char *right));
 
 void main(argc, argv) int argc; char **argv; {
+#ifdef X_
+       char *root;
+#endif
        char *t;
        char *assource;
        int i, j, c;
 
+#ifdef X_
+       root = savestr(argv[0]);
+       for (i = strlen(root); i > 0 && root[i - 1] != '/'; --i)
+               ;
+       root[i] = 0;
+       cpp = strspl(root, "../lib/cpp");
+       ccom = strspl(root, "../lib/ccom");
+       sccom = strspl(root, "../lib/sccom");
+       c2 = strspl(root, "../lib/c2");
+       as = strspl(root, "as");
+       ld = strspl(root, "ld");
+       crt0 = strspl(root, "../lib/crt0.o");
+#endif
        /* ld currently adds upto 5 args; 10 is room to spare */
        av = (char **)calloc(argc+10, sizeof (char **));
        clist = (char **)calloc(argc, sizeof (char **));
@@ -101,9 +127,16 @@ void main(argc, argv) int argc; char **argv; {
                        continue;
                case 'p':
                        proflag++;
+#ifdef X_
+                       t = "../lib/mcrt0.o";
+                       if (argv[i][2] == 'g')
+                               t = "../usr/lib/gcrt0.o";
+                       crt0 = strspl(root, t);
+#else
                        crt0 = "/lib/mcrt0.o";
                        if (argv[i][2] == 'g')
                                crt0 = "/usr/lib/gcrt0.o";
+#endif
                        continue;
                case 'f':
                        fflag++;
@@ -389,6 +422,9 @@ int callsys(f, v) char *f; char **v; {
        int t, status;
        char **cpp;
 
+#ifdef X_
+       v[0] = f; /* we have to tell cpp and ld where they are located */
+#endif
        if (debug) {
                fprintf(stderr, "%s:", f);
                for (cpp = v; *cpp != 0; cpp++)
index b364a95..93d747a 100644 (file)
@@ -74,13 +74,31 @@ char        **diffargv;             /* option list to pass to recursive diffs */
 char   *file1, *file2, *efile1, *efile2;
 struct stat stb1, stb2;
 
+#ifdef X_
+char   diff[MAXPATHLEN];
+char   diffh[MAXPATHLEN];
+char   pr[MAXPATHLEN];
+#else
 char   diff[] = DIFF;
 char   diffh[] = DIFFH;
 char   pr[] = PR;
+#endif
 
 void main(argc, argv) int argc; char **argv; {
        register char *argp;
+#ifdef X_
+       int i;
 
+       argp = argv[0];
+       for (i = strlen(argp); i > 0 && argp[i - 1] != '/'; --i)
+               ;
+       bcopy(argp, diff, i);
+       strcpy(diff + i, "diff");
+       bcopy(argp, diffh, i);
+       strcpy(diffh + i, "../usr/lib/diffh");
+       bcopy(argp, pr, i);
+       strcpy(pr + i, "pr");
+#endif
        ifdef1 = "FILE1"; ifdef2 = "FILE2";
        status = 2;
        diffargv = argv;
index 1d14937..bd273d8 100644 (file)
@@ -335,7 +335,9 @@ static void calldiff(wantpr) char *wantpr; {
                        dup(pv[0]);
                        close(pv[0]);
                        close(pv[1]);
+#ifndef X_
                        execv(pr+4, prargs);
+#endif
                        execv(pr, prargs);
                        perror(pr);
                        done();
@@ -353,7 +355,9 @@ static void calldiff(wantpr) char *wantpr; {
                        close(pv[0]);
                        close(pv[1]);
                }
+#ifndef X_
                execv(diff+4, diffargv);
+#endif
                execv(diff, diffargv);
                perror(diff);
                done();
index 49dc488..8429568 100644 (file)
--- a/bin/ld.c
+++ b/bin/ld.c
@@ -426,6 +426,9 @@ void error __P((int n, char *s));
 void readhdr __P((off_t loc));
 int _round __P((int v, u_long r));
 char *savestr __P((register char *cp));
+#ifdef X_
+char *strspl __P((char *left, char *right));
+#endif
 void bopen __P((register struct biobuf *bp, int off, int bufsize));
 void bwrite __P((void *_p, register int cnt, register struct biobuf *bp));
 void bflush __P((void));
@@ -434,11 +437,20 @@ void bflushc __P((register struct biobuf *bp, int c));
 void bseek __P((register struct biobuf *bp, register off_t off));
 
 void main(argc, argv) int argc; char **argv; {
+#ifdef X_
+       char *root;
+#endif
        register int c, i;
        int num;
        register char *ap, **p;
        char save;
 
+#ifdef X_
+       root = savestr(argv[0]);
+       for (i = strlen(root); i > 0 && root[i - 1] != '/'; --i)
+               ;
+       root[i] = 0;
+#endif
        if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
                signal(SIGINT, (void (*) __P((int sig)))delexit);
                signal(SIGTERM, (void (*) __P((int sig)))delexit);
@@ -461,9 +473,15 @@ void main(argc, argv) int argc; char **argv; {
                }
        }
        /* add default search directories */
+#ifdef X_
+       dirs[ndir++] = strspl(root, "../lib");
+       dirs[ndir++] = strspl(root, "../usr/lib");
+       dirs[ndir++] = strspl(root, "../usr/local/lib");
+#else
        dirs[ndir++] = "/lib";
        dirs[ndir++] = "/usr/lib";
        dirs[ndir++] = "/usr/local/lib";
+#endif
 
        p = argv+1;
        /*
@@ -2026,6 +2044,16 @@ char *savestr(cp) register char *cp; {
        return (cp);
 }
 
+#ifdef X_
+char *strspl(left, right) char *left; char *right; {
+       char buf[BUFSIZ];
+
+       strcpy(buf, left);
+       strcat(buf, right);
+       return (savestr(buf));
+}
+#endif
+
 void bopen(bp, off, bufsize) register struct biobuf *bp; int off; int bufsize; {
 
        bp->b_ptr = bp->b_buf = malloc(bufsize);
index 3745424..5ee77df 100644 (file)
@@ -3,7 +3,7 @@
 OBJS=  args.o blok.o builtin.o cmd.o ctype.o error.o expand.o fault.o io.o \
        macro.o main.o msg.o name.o print.o service.o setbrk.o stak.o \
        string.o word.o xec.o
-CFLAGS = -O -w -g
+CFLAGS = -O -w
 
 all:   sh
 
@@ -21,7 +21,7 @@ main.o:               brkincr.h
 stak.o:                brkincr.h
 
 sh: ${OBJS}
-       ${CC} -o sh ${OBJS}
+       ${CC} ${CFLAGS} -o sh ${OBJS}
 
 install:
        install -s sh $(DESTDIR)/bin
index 8772597..ee450d1 100644 (file)
@@ -86,6 +86,21 @@ void free(ap) void *ap; {
 #undef p
 }
 
+#ifdef X_
+/* simple realloc, because host's libc malloc() has been redirected to here */
+void *realloc(ap, nbytes) void *ap; unsigned nbytes; {
+#define p ((struct blk *)ap)
+       void *res = malloc(nbytes);
+       if (res && p && p<bloktop) {
+               unsigned onbytes = (char *)p[-1].word - (char *)p;
+               bcopy(p, res, onbytes < nbytes ? onbytes : nbytes);
+               free(p);
+       }
+       return res;
+#undef p
+}
+#endif
+
 #ifdef DEBUG
 int chkbptr(ptr) struct blk *ptr; {
        int             exf=0;
index 3842f3e..56bdef6 100644 (file)
@@ -307,6 +307,10 @@ struct dolnod *useargs __P((void));
 void *malloc __P((unsigned nbytes));
 void addblok __P((unsigned reqd));
 void free __P((void *ap));
+#ifdef X_
+/* simple realloc, because host's libc malloc() has been redirected to here */
+void *realloc __P((void *ap, unsigned nbytes));
+#endif
 int chkbptr __P((struct blk *ptr));
 
 /* builtin.c */
index 9dfb86b..8b385f0 100644 (file)
@@ -11,17 +11,19 @@ SRCS=       chdir.c chmod.c chown.c chroot.c close.c dup2.c dup.c execve.c \
        _exit.c fchmod.c fchown.c fork.c fstat.c ftruncate.c getdtablesize.c \
        getegid.c geteuid.c getgid.c getpagesize.c getpgrp.c getpid.c \
        getppid.c getrusage.c gettimeofday.c getuid.c ioctl.c link.c linux.c \
-       lseek.c lstat.c read.c readv.c sbrk.c setpgrp.c setregid.c \
-       setreuid.c settimeofday.c sigvec.c stat.c symlink.c sync.c \
-       truncate.c umask.c unlink.c utimes.c vopen.c wait.c write.c writev.c
+       lseek.c lstat.c read.c readlink.c readv.c sbrk.c setpgrp.c \
+       setregid.c setreuid.c settimeofday.c sigvec.c stat.c symlink.c \
+       sync.c truncate.c umask.c unlink.c utimes.c vopen.c wait.c write.c \
+       writev.c
 
 OBJS=  chdir.o chmod.o chown.o chroot.o close.o dup2.o dup.o execve.o \
        _exit.o fchmod.o fchown.o fork.o fstat.o ftruncate.o getdtablesize.o \
        getegid.o geteuid.o getgid.o getpagesize.o getpgrp.o getpid.o \
        getppid.o getrusage.o gettimeofday.o getuid.o ioctl.o link.o linux.o \
-       lseek.o lstat.o read.o readv.o sbrk.o setpgrp.o setregid.o \
-       setreuid.o settimeofday.o sigvec.o stat.o symlink.o sync.o \
-       truncate.o umask.o unlink.o utimes.o vopen.o wait.o write.o writev.o
+       lseek.o lstat.o read.o readlink.o readv.o sbrk.o setpgrp.o \
+       setregid.o setreuid.o settimeofday.o sigvec.o stat.o symlink.o \
+       sync.o truncate.o umask.o unlink.o utimes.o vopen.o wait.o write.o \
+       writev.o
 
 TAGSFILE=tags
 
index 752e4e8..9f3d0d2 100644 (file)
@@ -7,7 +7,7 @@
 #include "linux.h"
 
 int chmod(s, m) char *s; int m; {
-       if (nox_chmod(s, (nox_mode_t)m)) {
+       if (nox_chmod(s, ttoh_mode(m))) {
                errno = htot_errno(nox_errno);
                return -1;
        }
index 4a28b55..7705868 100644 (file)
@@ -7,7 +7,7 @@
 #include "linux.h"
 
 int fchmod(f, m) int f; int m; {
-       if (nox_fchmod((nox_int)f, (nox_mode_t)m)) {
+       if (nox_fchmod((nox_int)f, ttoh_mode(m))) {
                errno = htot_errno(nox_errno);
                return -1;
        }
index f0209d6..b2bcdf6 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/nox_types.h>
 #include <nox_unistd.h>
 
+#include <gen.h>
 #include <string.h>
 #include <sys/dir.h>
 #include <sys/errno.h>
@@ -150,11 +151,85 @@ int htot_sigmask(set) nox_sigset_t *set; {
        return res;
 }
 
+nox_mode_t ttoh_mode(m) unsigned m; {
+       nox_mode_t res = m & 0777;
+       if (m & S_ISVTX)
+               res |= nox_S_ISVTX;
+       if (m & S_ISGID)
+               res |= nox_S_ISGID;
+       if (m & S_ISUID)
+               res |= nox_S_ISUID;
+       switch (m & S_IFMT) {
+       /*case S_IFIFO:
+               res |= nox_S_IFIFO;
+               break;*/
+       case S_IFCHR:
+               res |= nox_S_IFCHR;
+               break;
+       case S_IFDIR:
+               res |= nox_S_IFDIR;
+               break;
+       case S_IFBLK:
+               res |= nox_S_IFBLK;
+               break;
+       case S_IFREG:
+               res |= nox_S_IFREG;
+               break;
+       case S_IFLNK:
+               res |= nox_S_IFLNK;
+               break;
+       case S_IFSOCK:
+               res |= nox_S_IFSOCK;
+               break;
+       default:
+               nox_write((nox_int)2, "ttoh_mode()\n", 12);
+               nox_abort();
+       }
+       return res;
+}
+
+unsigned htot_mode(m) nox_mode_t m; {
+       unsigned res = m & 0777;
+       if (m & nox_S_ISVTX)
+               res |= S_ISVTX;
+       if (m & nox_S_ISGID)
+               res |= S_ISGID;
+       if (m & nox_S_ISUID)
+               res |= S_ISUID;
+       switch (m & nox_S_IFMT) {
+       /*case nox_S_IFIFO:
+               res |= S_IFIFO;
+               break;*/
+       case nox_S_IFCHR:
+               res |= S_IFCHR;
+               break;
+       case nox_S_IFDIR:
+               res |= S_IFDIR;
+               break;
+       case nox_S_IFBLK:
+               res |= S_IFBLK;
+               break;
+       case nox_S_IFREG:
+               res |= S_IFREG;
+               break;
+       case nox_S_IFLNK:
+               res |= S_IFLNK;
+               break;
+       case nox_S_IFSOCK:
+               res |= S_IFSOCK;
+               break;
+       default:
+               nox_write((nox_int)2, "htot_mode()\n", 12);
+               nox_abort();
+       }
+       return res;
+}
+
 void htot_stat(stat, res) struct nox_stat *stat; struct stat *res; {
        bzero(res, sizeof(struct stat));
        res->st_dev = (dev_t)stat->nox_st_dev;
        res->st_ino = (ino_t)stat->nox_st_ino;
-       res->st_mode = (unsigned short)stat->nox_st_mode;
+       res->st_mode = htot_mode(stat->nox_st_mode);
        res->st_nlink = (short)stat->nox_st_nlink;
        res->st_uid = (uid_t)stat->nox_st_uid;
        res->st_gid = (gid_t)stat->nox_st_gid;
@@ -163,7 +238,7 @@ void htot_stat(stat, res) struct nox_stat *stat; struct stat *res; {
        res->st_atime = (time_t)stat->nox_st_atime;
        res->st_mtime = (time_t)stat->nox_st_mtime;
        res->st_ctime = (time_t)stat->nox_st_ctime;
-       res->st_blksize = /*0x2000;*/ (long)stat->nox_st_blksize;
+       res->st_blksize = (long)stat->nox_st_blksize;
        res->st_blocks = (long)stat->nox_st_blocks;
 }
 
@@ -244,3 +319,15 @@ nox_int htot_dir(fd) nox_int fd; {
        }
        return 0;
 }
+
+void *nox_malloc(nox_size_t size) {
+       return malloc((unsigned)size);
+}
+
+void *nox_realloc(void *p, nox_size_t size) {
+       return realloc(p, (unsigned)size);
+}
+
+void nox_free(void *p) {
+       free(p);
+}
index 71347ea..a66b3ae 100644 (file)
@@ -13,6 +13,8 @@ int htot_errno(nox_int n);
 int htot_signo(nox_int n);
 void ttoh_sigmask(int m, nox_sigset_t *res);
 int htot_sigmask(nox_sigset_t *set);
+nox_mode_t ttoh_mode(unsigned m);
+unsigned htot_mode(nox_mode_t m);
 void htot_stat(struct nox_stat *statbuf, struct stat *res);
 int htot_dir(nox_int fd);
 
diff --git a/lib/libc/linux/sys/readlink.c b/lib/libc/linux/sys/readlink.c
new file mode 100644 (file)
index 0000000..7adbb30
--- /dev/null
@@ -0,0 +1,15 @@
+#include <nox_errno.h>
+#include <nox_unistd.h>
+
+#include <errno.h>
+#include <sys/file.h>
+#include "linux.h"
+
+int readlink(p, b, s) char *p; char *b; int s; {
+       nox_ssize_t res;
+
+       res = nox_readlink(p, b, (nox_size_t)s);
+       if (res == (nox_ssize_t)-1)
+               errno = htot_errno(nox_errno);
+       return (int)res;
+}
index cfcddd9..d4c80f6 100644 (file)
@@ -40,7 +40,7 @@ int vopen(f, m, argp) char *f; int m; va_list argp; {
        mode = 0;
        if (m & O_CREAT) {
                flags |= nox_O_CREAT;
-               mode = va_arg(argp, int);
+               mode = (nox_mode_t)va_arg(argp, int);
        }
        if (m & O_TRUNC)
                flags |= nox_O_TRUNC;
index 14579a2..6f7ae5a 100644 (file)
@@ -1,8 +1,5 @@
-#include <gen.h>
-#include <sys/file.h>
-/*#include <sys/proc.h> gen.h*/
+#include <sys/proc.h>
 
 int vfork() {
-       write(2, "vfork()\n", 8);
-       abort();
+       return fork();
 }