From: Nick Downing Date: Tue, 31 Jan 2017 03:03:29 +0000 (+1100) Subject: Fix issue of host malloc() ruining the heap in bin/sh, fix pathnames in cc, diff... X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=2ed3e23edba928af8581388b0119040ee11a86f0;p=43bsd.git Fix issue of host malloc() ruining the heap in bin/sh, fix pathnames in cc, diff, ld, add proper emulation of mode_t for stat and friends, add system calls --- diff --git a/bin/Makefile b/bin/Makefile index 824aa5c..b8bc782 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -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 diff --git a/bin/cc.c b/bin/cc.c index a0c4588..1ea6cb4 100644 --- a/bin/cc.c +++ b/bin/cc.c @@ -20,6 +20,15 @@ static char sccsid[] = "@(#)cc.c 4.13 9/18/85"; /*#include */ /*#include */ +#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++) diff --git a/bin/diff/diff.c b/bin/diff/diff.c index b364a95..93d747a 100644 --- a/bin/diff/diff.c +++ b/bin/diff/diff.c @@ -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; diff --git a/bin/diff/diffdir.c b/bin/diff/diffdir.c index 1d14937..bd273d8 100644 --- a/bin/diff/diffdir.c +++ b/bin/diff/diffdir.c @@ -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(); diff --git a/bin/ld.c b/bin/ld.c index 49dc488..8429568 100644 --- 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); diff --git a/bin/sh/Makefile b/bin/sh/Makefile index 3745424..5ee77df 100644 --- a/bin/sh/Makefile +++ b/bin/sh/Makefile @@ -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 diff --git a/bin/sh/blok.c b/bin/sh/blok.c index 8772597..ee450d1 100644 --- a/bin/sh/blok.c +++ b/bin/sh/blok.c @@ -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 #include +#include #include #include #include @@ -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); +} diff --git a/lib/libc/linux/sys/linux.h b/lib/libc/linux/sys/linux.h index 71347ea..a66b3ae 100644 --- a/lib/libc/linux/sys/linux.h +++ b/lib/libc/linux/sys/linux.h @@ -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 index 0000000..7adbb30 --- /dev/null +++ b/lib/libc/linux/sys/readlink.c @@ -0,0 +1,15 @@ +#include +#include + +#include +#include +#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; +} diff --git a/lib/libc/linux/sys/vopen.c b/lib/libc/linux/sys/vopen.c index cfcddd9..d4c80f6 100644 --- a/lib/libc/linux/sys/vopen.c +++ b/lib/libc/linux/sys/vopen.c @@ -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; diff --git a/lib/libc/sys/vfork.c b/lib/libc/sys/vfork.c index 14579a2..6f7ae5a 100644 --- a/lib/libc/sys/vfork.c +++ b/lib/libc/sys/vfork.c @@ -1,8 +1,5 @@ -#include -#include -/*#include gen.h*/ +#include int vfork() { - write(2, "vfork()\n", 8); - abort(); + return fork(); }