ref: 407cf4ac6e59556b65d29b3c0de6ada6fdcc3cc7
parent: 61b1134df944eee5188214a9e249101f1ec095e7
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Jul 16 11:22:42 EDT 2020
ape: simplify mkfile (thanks amavect) ape cp, mv, and cc build with ?c, not pcc ape cp and mv just ignore one or two extra flags, instead of providing posix compatibility it's better to fail then do nothing remove cp.c and mv.c move cc.c to /sys/src/ape/9src so it doesn't need its own mkfile rule
--- /dev/null
+++ b/sys/src/ape/9src/cc.c
@@ -1,0 +1,389 @@
+#include <u.h>
+#include <libc.h>
+
+/*
+ POSIX standard c89
+
+ standard options: -c, -D name[=val], -E (preprocess to stdout),
+ -g, -L dir, -o outfile, -O, -s, -U name
+ (and operands can have -l lib interspersed)
+
+ nonstandard but specified options: -S (assembly language left in .s),
+ -Wx,arg1[,arg2...] (pass arg(s) to phase x, where x is p (cpp)
+ 0 (compiler), or l (loader)
+ nonstandard options: -v (echo real commands to stdout as they execute)
+ -A: turn on ANSI prototype warnings
+ */
+
+typedef struct Objtype {
+ char *name;
+ char *cc;
+ char *ld;
+ char *o;
+} Objtype;
+
+Objtype objtype[] = {
+ {"68020", "2c", "2l", "2"},
+ {"arm", "5c", "5l", "5"},
+ {"arm64", "7c", "7l", "7"},
+ {"amd64", "6c", "6l", "6"},
+ {"386", "8c", "8l", "8"},
+ {"sparc", "kc", "kl", "k"},
+ {"power", "qc", "ql", "q"},
+ {"mips", "vc", "vl", "v"},
+ {"spim", "0c", "0l", "0"},
+};
+
+enum {
+ Nobjs = (sizeof objtype)/(sizeof objtype[0]),
+ Maxlist = 2000,
+};
+
+typedef struct List {
+ char *strings[Maxlist];
+ int n;
+} List;
+
+List srcs, objs, cpp, cc, ld, ldargs, srchlibs;
+int cflag, vflag, Eflag, Sflag, Aflag;
+char *allos = "2678kqv";
+
+void append(List *, char *);
+char *changeext(char *, char *);
+void doexec(char *, List *);
+void dopipe(char *, List *, char *, List *);
+void fatal(char *);
+Objtype *findoty(void);
+void printlist(List *);
+char *searchlib(char *, char*);
+
+void
+main(int argc, char *argv[])
+{
+ char *s, *suf, *ccpath, *lib;
+ char *oname;
+ int haveoname = 0;
+ int i, cppn, ccn;
+ Objtype *ot;
+
+ ot = findoty();
+ oname = "a.out";
+ append(&cpp, "cpp");
+ append(&cpp, "-D__STDC__=1"); /* ANSI says so */
+ append(&cpp, "-D_POSIX_SOURCE=");
+ append(&cpp, "-N"); /* turn off standard includes */
+ append(&cc, ot->cc);
+ append(&ld, ot->ld);
+ append(&srchlibs, smprint("/%s/lib/ape", ot->name));
+ while(argc > 0) {
+ ARGBEGIN {
+ case 'c':
+ cflag = 1;
+ break;
+ case 'l':
+ lib = searchlib(ARGF(), ot->name);
+ if(!lib)
+ fprint(2, "cc: can't find library for -l\n");
+ else
+ append(&objs, lib);
+ break;
+ case 'o':
+ oname = ARGF();
+ haveoname = 1;
+ if(!oname)
+ fatal("cc: no -o argument");
+ break;
+ case 'D':
+ case 'I':
+ case 'U':
+ append(&cpp, smprint("-%c%s", ARGC(), ARGF()));
+ break;
+ case 'E':
+ Eflag = 1;
+ cflag = 1;
+ break;
+ case 's':
+ case 'g':
+ break;
+ case 'L':
+ lib = ARGF();
+ if(!lib)
+ fprint(2, "cc: no -L argument\n");
+ else
+ append(&srchlibs, lib);
+ break;
+ case 'N':
+ case 'T':
+ case 'w':
+ append(&cc, smprint("-%c", ARGC()));
+ break;
+ case 'O':
+ break;
+ case 'W':
+ s = ARGF();
+ if(s && s[1]==',') {
+ switch (s[0]) {
+ case 'p':
+ append(&cpp, s+2);
+ break;
+ case '0':
+ append(&cc, s+2);
+ break;
+ case 'l':
+ append(&ldargs, s+2);
+ break;
+ default:
+ fprint(2, "cc: pass letter after -W should be one of p0l; ignored\n");
+ }
+ } else
+ fprint(2, "cc: bad option after -W; ignored\n");
+ break;
+ case 'v':
+ vflag = 1;
+ append(&ldargs, "-v");
+ break;
+ case 'A':
+ Aflag = 1;
+ break;
+ case 'S':
+ Sflag = 1;
+ break;
+ default:
+ fprint(2, "cc: flag -%c ignored\n", ARGC());
+ break;
+ } ARGEND
+ if(!Aflag) {
+ append(&cc, "-J"); /* old/new decl mixture hack */
+ append(&cc, "-B"); /* turn off non-prototype warnings */
+ Aflag = 1;
+ }
+ if(argc > 0) {
+ s = argv[0];
+ suf = utfrrune(s, '.');
+ if(suf) {
+ suf++;
+ if(strcmp(suf, "c") == 0) {
+ append(&srcs, s);
+ append(&objs, changeext(s, "o"));
+ } else if(strcmp(suf, "o") == 0 ||
+ strcmp(suf, ot->o) == 0 ||
+ strcmp(suf, "a") == 0 ||
+ (suf[0] == 'a' && strcmp(suf+1, ot->o) == 0)) {
+ append(&objs, s);
+ } else if(utfrune(allos, suf[0]) != 0) {
+ fprint(2, "cc: argument %s ignored: wrong architecture\n",
+ s);
+ }
+ }
+ }
+ }
+ if(objs.n == 0)
+ fatal("no files to compile or load");
+ ccpath = smprint("/bin/%s", ot->cc);
+ append(&cpp, smprint("-I/%s/include/ape", ot->name));
+ append(&cpp, "-I/sys/include/ape");
+ cppn = cpp.n;
+ ccn = cc.n;
+ for(i = 0; i < srcs.n; i++) {
+ append(&cpp, srcs.strings[i]);
+ if(Eflag)
+ doexec("/bin/cpp", &cpp);
+ else {
+ if(Sflag)
+ append(&cc, "-S");
+ else {
+ append(&cc, "-o");
+ if (haveoname && cflag)
+ append(&cc, oname);
+ else
+ append(&cc, changeext(srcs.strings[i], "o"));
+ }
+ dopipe("/bin/cpp", &cpp, ccpath, &cc);
+ }
+ cpp.n = cppn;
+ cc.n = ccn;
+ }
+ if(!cflag) {
+ append(&ld, "-o");
+ append(&ld, oname);
+ for(i = 0; i < ldargs.n; i++)
+ append(&ld, ldargs.strings[i]);
+ for(i = 0; i < objs.n; i++)
+ append(&ld, objs.strings[i]);
+ append(&ld, smprint("/%s/lib/ape/libap.a", ot->name));
+ doexec(smprint("/bin/%s", ot->ld), &ld);
+ if(objs.n == 1)
+ remove(objs.strings[0]);
+ }
+
+ exits(0);
+}
+
+char *
+searchlib(char *s, char *objtype)
+{
+ char *l;
+ int i;
+
+ if(!s)
+ return 0;
+ for(i = srchlibs.n-1; i>=0; i--) {
+ l = smprint("%s/lib%s.a", srchlibs.strings[i], s);
+ if(access(l, 0) >= 0)
+ return l;
+ }
+ if(s[1] == 0)
+ switch(s[0]) {
+ case 'c':
+ l = smprint("/%s/lib/ape/libap.a", objtype);
+ break;
+ case 'm':
+ l = smprint("/%s/lib/ape/libap.a", objtype);
+ break;
+ case 'l':
+ l = smprint("/%s/lib/ape/libl.a", objtype);
+ break;
+ case 'y':
+ l = smprint("/%s/lib/ape/liby.a", objtype);
+ break;
+ default:
+ l = 0;
+ }
+ else
+ l = 0;
+ return l;
+}
+
+void
+append(List *l, char *s)
+{
+ if(l->n >= Maxlist-1)
+ fatal("too many arguments");
+ l->strings[l->n++] = s;
+ l->strings[l->n] = 0;
+}
+
+void
+doexec(char *c, List *a)
+{
+ Waitmsg *w;
+
+ if(vflag) {
+ printlist(a);
+ fprint(2, "\n");
+ }
+ switch(fork()) {
+ case -1:
+ fatal("fork failed");
+ case 0:
+ exec(c, a->strings);
+ fatal("exec failed");
+ }
+ if((w = wait()) == nil)
+ fatal("wait failed");
+ if(w->msg[0])
+ fatal(smprint("%s: %s", a->strings[0], w->msg));
+ free(w);
+}
+
+void
+dopipe(char *c1, List *a1, char *c2, List *a2)
+{
+ Waitmsg *w;
+ int pid1, got;
+ int fd[2];
+
+ if(vflag) {
+ printlist(a1);
+ fprint(2, " | ");
+ printlist(a2);
+ fprint(2, "\n");
+ }
+ if(pipe(fd) < 0)
+ fatal("pipe failed");
+ switch((pid1 = fork())) {
+ case -1:
+ fatal("fork failed");
+ case 0:
+ dup(fd[0], 0);
+ close(fd[0]);
+ close(fd[1]);
+ exec(c2, a2->strings);
+ fatal("exec failed");
+ }
+ switch(fork()) {
+ case -1:
+ fatal("fork failed");
+ case 0:
+ close(0);
+ dup(fd[1], 1);
+ close(fd[0]);
+ close(fd[1]);
+ exec(c1, a1->strings);
+ fatal("exec failed");
+ }
+ close(fd[0]);
+ close(fd[1]);
+ for(got = 0; got < 2; got++) {
+ if((w = wait()) == nil)
+ fatal("wait failed");
+ if(w->msg[0])
+ fatal(smprint("%s: %s", (w->pid == pid1) ? a1->strings[0] : a2->strings[0], w->msg));
+ free(w);
+ }
+}
+
+Objtype *
+findoty(void)
+{
+ char *o;
+ Objtype *oty;
+
+ o = getenv("objtype");
+ if(!o)
+ fatal("no $objtype in environment");
+ for(oty = objtype; oty < &objtype[Nobjs]; oty++)
+ if(strcmp(o, oty->name) == 0)
+ return oty;
+ fatal("unknown $objtype");
+ return 0; /* shut compiler up */
+}
+
+void
+fatal(char *msg)
+{
+ fprint(2, "cc: %s\n", msg);
+ exits(msg);
+}
+
+/* src ends in .something; return copy of basename with .ext added */
+char *
+changeext(char *src, char *ext)
+{
+ char *b, *e, *ans;
+
+ b = utfrrune(src, '/');
+ if(b)
+ b++;
+ else
+ b = src;
+ e = utfrrune(src, '.');
+ if(!e)
+ return 0;
+ *e = 0;
+ ans = smprint("%s.%s", b, ext);
+ *e = '.';
+ return ans;
+}
+
+void
+printlist(List *l)
+{
+ int i;
+
+ for(i = 0; i < l->n; i++) {
+ fprint(2, "%s", l->strings[i]);
+ if(i < l->n - 1)
+ fprint(2, " ");
+ }
+}
--- a/sys/src/ape/9src/mkfile
+++ b/sys/src/ape/9src/mkfile
@@ -4,6 +4,7 @@
TARG=\
stty\
tar\
+ cc\
BIN=/$objtype/bin/ape
</sys/src/cmd/mkmany
--- a/sys/src/ape/cmd/cc.c
+++ /dev/null
@@ -1,389 +1,0 @@
-#include <u.h>
-#include <libc.h>
-
-/*
- POSIX standard c89
-
- standard options: -c, -D name[=val], -E (preprocess to stdout),
- -g, -L dir, -o outfile, -O, -s, -U name
- (and operands can have -l lib interspersed)
-
- nonstandard but specified options: -S (assembly language left in .s),
- -Wx,arg1[,arg2...] (pass arg(s) to phase x, where x is p (cpp)
- 0 (compiler), or l (loader)
- nonstandard options: -v (echo real commands to stdout as they execute)
- -A: turn on ANSI prototype warnings
- */
-
-typedef struct Objtype {
- char *name;
- char *cc;
- char *ld;
- char *o;
-} Objtype;
-
-Objtype objtype[] = {
- {"68020", "2c", "2l", "2"},
- {"arm", "5c", "5l", "5"},
- {"arm64", "7c", "7l", "7"},
- {"amd64", "6c", "6l", "6"},
- {"386", "8c", "8l", "8"},
- {"sparc", "kc", "kl", "k"},
- {"power", "qc", "ql", "q"},
- {"mips", "vc", "vl", "v"},
- {"spim", "0c", "0l", "0"},
-};
-
-enum {
- Nobjs = (sizeof objtype)/(sizeof objtype[0]),
- Maxlist = 2000,
-};
-
-typedef struct List {
- char *strings[Maxlist];
- int n;
-} List;
-
-List srcs, objs, cpp, cc, ld, ldargs, srchlibs;
-int cflag, vflag, Eflag, Sflag, Aflag;
-char *allos = "2678kqv";
-
-void append(List *, char *);
-char *changeext(char *, char *);
-void doexec(char *, List *);
-void dopipe(char *, List *, char *, List *);
-void fatal(char *);
-Objtype *findoty(void);
-void printlist(List *);
-char *searchlib(char *, char*);
-
-void
-main(int argc, char *argv[])
-{
- char *s, *suf, *ccpath, *lib;
- char *oname;
- int haveoname = 0;
- int i, cppn, ccn;
- Objtype *ot;
-
- ot = findoty();
- oname = "a.out";
- append(&cpp, "cpp");
- append(&cpp, "-D__STDC__=1"); /* ANSI says so */
- append(&cpp, "-D_POSIX_SOURCE=");
- append(&cpp, "-N"); /* turn off standard includes */
- append(&cc, ot->cc);
- append(&ld, ot->ld);
- append(&srchlibs, smprint("/%s/lib/ape", ot->name));
- while(argc > 0) {
- ARGBEGIN {
- case 'c':
- cflag = 1;
- break;
- case 'l':
- lib = searchlib(ARGF(), ot->name);
- if(!lib)
- fprint(2, "cc: can't find library for -l\n");
- else
- append(&objs, lib);
- break;
- case 'o':
- oname = ARGF();
- haveoname = 1;
- if(!oname)
- fatal("cc: no -o argument");
- break;
- case 'D':
- case 'I':
- case 'U':
- append(&cpp, smprint("-%c%s", ARGC(), ARGF()));
- break;
- case 'E':
- Eflag = 1;
- cflag = 1;
- break;
- case 's':
- case 'g':
- break;
- case 'L':
- lib = ARGF();
- if(!lib)
- fprint(2, "cc: no -L argument\n");
- else
- append(&srchlibs, lib);
- break;
- case 'N':
- case 'T':
- case 'w':
- append(&cc, smprint("-%c", ARGC()));
- break;
- case 'O':
- break;
- case 'W':
- s = ARGF();
- if(s && s[1]==',') {
- switch (s[0]) {
- case 'p':
- append(&cpp, s+2);
- break;
- case '0':
- append(&cc, s+2);
- break;
- case 'l':
- append(&ldargs, s+2);
- break;
- default:
- fprint(2, "cc: pass letter after -W should be one of p0l; ignored\n");
- }
- } else
- fprint(2, "cc: bad option after -W; ignored\n");
- break;
- case 'v':
- vflag = 1;
- append(&ldargs, "-v");
- break;
- case 'A':
- Aflag = 1;
- break;
- case 'S':
- Sflag = 1;
- break;
- default:
- fprint(2, "cc: flag -%c ignored\n", ARGC());
- break;
- } ARGEND
- if(!Aflag) {
- append(&cc, "-J"); /* old/new decl mixture hack */
- append(&cc, "-B"); /* turn off non-prototype warnings */
- Aflag = 1;
- }
- if(argc > 0) {
- s = argv[0];
- suf = utfrrune(s, '.');
- if(suf) {
- suf++;
- if(strcmp(suf, "c") == 0) {
- append(&srcs, s);
- append(&objs, changeext(s, "o"));
- } else if(strcmp(suf, "o") == 0 ||
- strcmp(suf, ot->o) == 0 ||
- strcmp(suf, "a") == 0 ||
- (suf[0] == 'a' && strcmp(suf+1, ot->o) == 0)) {
- append(&objs, s);
- } else if(utfrune(allos, suf[0]) != 0) {
- fprint(2, "cc: argument %s ignored: wrong architecture\n",
- s);
- }
- }
- }
- }
- if(objs.n == 0)
- fatal("no files to compile or load");
- ccpath = smprint("/bin/%s", ot->cc);
- append(&cpp, smprint("-I/%s/include/ape", ot->name));
- append(&cpp, "-I/sys/include/ape");
- cppn = cpp.n;
- ccn = cc.n;
- for(i = 0; i < srcs.n; i++) {
- append(&cpp, srcs.strings[i]);
- if(Eflag)
- doexec("/bin/cpp", &cpp);
- else {
- if(Sflag)
- append(&cc, "-S");
- else {
- append(&cc, "-o");
- if (haveoname && cflag)
- append(&cc, oname);
- else
- append(&cc, changeext(srcs.strings[i], "o"));
- }
- dopipe("/bin/cpp", &cpp, ccpath, &cc);
- }
- cpp.n = cppn;
- cc.n = ccn;
- }
- if(!cflag) {
- append(&ld, "-o");
- append(&ld, oname);
- for(i = 0; i < ldargs.n; i++)
- append(&ld, ldargs.strings[i]);
- for(i = 0; i < objs.n; i++)
- append(&ld, objs.strings[i]);
- append(&ld, smprint("/%s/lib/ape/libap.a", ot->name));
- doexec(smprint("/bin/%s", ot->ld), &ld);
- if(objs.n == 1)
- remove(objs.strings[0]);
- }
-
- exits(0);
-}
-
-char *
-searchlib(char *s, char *objtype)
-{
- char *l;
- int i;
-
- if(!s)
- return 0;
- for(i = srchlibs.n-1; i>=0; i--) {
- l = smprint("%s/lib%s.a", srchlibs.strings[i], s);
- if(access(l, 0) >= 0)
- return l;
- }
- if(s[1] == 0)
- switch(s[0]) {
- case 'c':
- l = smprint("/%s/lib/ape/libap.a", objtype);
- break;
- case 'm':
- l = smprint("/%s/lib/ape/libap.a", objtype);
- break;
- case 'l':
- l = smprint("/%s/lib/ape/libl.a", objtype);
- break;
- case 'y':
- l = smprint("/%s/lib/ape/liby.a", objtype);
- break;
- default:
- l = 0;
- }
- else
- l = 0;
- return l;
-}
-
-void
-append(List *l, char *s)
-{
- if(l->n >= Maxlist-1)
- fatal("too many arguments");
- l->strings[l->n++] = s;
- l->strings[l->n] = 0;
-}
-
-void
-doexec(char *c, List *a)
-{
- Waitmsg *w;
-
- if(vflag) {
- printlist(a);
- fprint(2, "\n");
- }
- switch(fork()) {
- case -1:
- fatal("fork failed");
- case 0:
- exec(c, a->strings);
- fatal("exec failed");
- }
- if((w = wait()) == nil)
- fatal("wait failed");
- if(w->msg[0])
- fatal(smprint("%s: %s", a->strings[0], w->msg));
- free(w);
-}
-
-void
-dopipe(char *c1, List *a1, char *c2, List *a2)
-{
- Waitmsg *w;
- int pid1, got;
- int fd[2];
-
- if(vflag) {
- printlist(a1);
- fprint(2, " | ");
- printlist(a2);
- fprint(2, "\n");
- }
- if(pipe(fd) < 0)
- fatal("pipe failed");
- switch((pid1 = fork())) {
- case -1:
- fatal("fork failed");
- case 0:
- dup(fd[0], 0);
- close(fd[0]);
- close(fd[1]);
- exec(c2, a2->strings);
- fatal("exec failed");
- }
- switch(fork()) {
- case -1:
- fatal("fork failed");
- case 0:
- close(0);
- dup(fd[1], 1);
- close(fd[0]);
- close(fd[1]);
- exec(c1, a1->strings);
- fatal("exec failed");
- }
- close(fd[0]);
- close(fd[1]);
- for(got = 0; got < 2; got++) {
- if((w = wait()) == nil)
- fatal("wait failed");
- if(w->msg[0])
- fatal(smprint("%s: %s", (w->pid == pid1) ? a1->strings[0] : a2->strings[0], w->msg));
- free(w);
- }
-}
-
-Objtype *
-findoty(void)
-{
- char *o;
- Objtype *oty;
-
- o = getenv("objtype");
- if(!o)
- fatal("no $objtype in environment");
- for(oty = objtype; oty < &objtype[Nobjs]; oty++)
- if(strcmp(o, oty->name) == 0)
- return oty;
- fatal("unknown $objtype");
- return 0; /* shut compiler up */
-}
-
-void
-fatal(char *msg)
-{
- fprint(2, "cc: %s\n", msg);
- exits(msg);
-}
-
-/* src ends in .something; return copy of basename with .ext added */
-char *
-changeext(char *src, char *ext)
-{
- char *b, *e, *ans;
-
- b = utfrrune(src, '/');
- if(b)
- b++;
- else
- b = src;
- e = utfrrune(src, '.');
- if(!e)
- return 0;
- *e = 0;
- ans = smprint("%s.%s", b, ext);
- *e = '.';
- return ans;
-}
-
-void
-printlist(List *l)
-{
- int i;
-
- for(i = 0; i < l->n; i++) {
- fprint(2, "%s", l->strings[i]);
- if(i < l->n - 1)
- fprint(2, " ");
- }
-}
--- a/sys/src/ape/cmd/cp.c
+++ /dev/null
@@ -1,179 +1,0 @@
-#include <u.h>
-#include <libc.h>
-
-#define DEFB (8*1024)
-
-int failed;
-int gflag;
-int uflag;
-int xflag;
-void copy(char *from, char *to, int todir);
-int copy1(int fdf, int fdt, char *from, char *to);
-
-void
-main(int argc, char *argv[])
-{
- Dir *dirb;
- int todir, i;
-
- ARGBEGIN {
- case 'g':
- gflag++;
- break;
- case 'u':
- uflag++;
- gflag++;
- break;
- case 'x':
- xflag++;
- break;
- case 'p':
- break;
- default:
- goto usage;
- } ARGEND
-
- todir=0;
- if(argc < 2)
- goto usage;
- dirb = dirstat(argv[argc-1]);
- if(dirb!=nil && (dirb->mode&DMDIR))
- todir=1;
- if(argc>2 && !todir){
- fprint(2, "cp: %s not a directory\n", argv[argc-1]);
- exits("bad usage");
- }
- for(i=0; i<argc-1; i++)
- copy(argv[i], argv[argc-1], todir);
- if(failed)
- exits("errors");
- exits(0);
-
-usage:
- fprint(2, "usage:\tcp [-gux] fromfile tofile\n");
- fprint(2, "\tcp [-x] fromfile ... todir\n");
- exits("usage");
-}
-
-int
-samefile(Dir *a, char *an, char *bn)
-{
- Dir *b;
- int ret;
-
- ret = 0;
- b=dirstat(bn);
- if(b != nil)
- if(b->qid.type==a->qid.type)
- if(b->qid.path==a->qid.path)
- if(b->qid.vers==a->qid.vers)
- if(b->dev==a->dev)
- if(b->type==a->type){
- fprint(2, "cp: %s and %s are the same file\n", an, bn);
- ret = 1;
- }
- free(b);
- return ret;
-}
-
-void
-copy(char *from, char *to, int todir)
-{
- Dir *dirb, dirt;
- char name[256];
- int fdf, fdt, mode;
-
- if(todir){
- char *s, *elem;
- elem=s=from;
- while(*s++)
- if(s[-1]=='/')
- elem=s;
- sprint(name, "%s/%s", to, elem);
- to=name;
- }
-
- if((dirb=dirstat(from))==nil){
- fprint(2,"cp: can't stat %s: %r\n", from);
- failed = 1;
- return;
- }
- mode = dirb->mode;
- if(mode&DMDIR){
- fprint(2, "cp: %s is a directory\n", from);
- free(dirb);
- failed = 1;
- return;
- }
- if(samefile(dirb, from, to)){
- free(dirb);
- failed = 1;
- return;
- }
- mode &= 0777;
- fdf=open(from, OREAD);
- if(fdf<0){
- fprint(2, "cp: can't open %s: %r\n", from);
- free(dirb);
- failed = 1;
- return;
- }
- fdt=create(to, OWRITE, mode);
- if(fdt<0){
- fprint(2, "cp: can't create %s: %r\n", to);
- close(fdf);
- free(dirb);
- failed = 1;
- return;
- }
- if(copy1(fdf, fdt, from, to)==0 && (xflag || gflag || uflag)){
- nulldir(&dirt);
- if(xflag){
- dirt.mtime = dirb->mtime;
- dirt.mode = dirb->mode;
- }
- if(uflag)
- dirt.uid = dirb->uid;
- if(gflag)
- dirt.gid = dirb->gid;
- if(dirfwstat(fdt, &dirt) < 0)
- fprint(2, "cp: warning: can't wstat %s: %r\n", to);
- }
- free(dirb);
- close(fdf);
- close(fdt);
-}
-
-int
-copy1(int fdf, int fdt, char *from, char *to)
-{
- char *buf;
- long n, n1, rcount;
- int rv;
- char err[ERRMAX];
-
- buf = malloc(DEFB);
- /* clear any residual error */
- err[0] = '\0';
- errstr(err, ERRMAX);
- rv = 0;
- for(rcount=0;; rcount++) {
- n = read(fdf, buf, DEFB);
- if(n <= 0)
- break;
- n1 = write(fdt, buf, n);
- if(n1 != n) {
- fprint(2, "cp: error writing %s: %r\n", to);
- failed = 1;
- rv = -1;
- break;
- }
- }
- if(n < 0) {
- fprint(2, "cp: error reading %s: %r\n", from);
- failed = 1;
- rv = -1;
- }
- free(buf);
- return rv;
-}
--- a/sys/src/ape/cmd/mkfile
+++ b/sys/src/ape/cmd/mkfile
@@ -2,11 +2,8 @@
<$APE/config
TARG=basename\
- cc\
- cp\
dirname\
kill\
- mv\
uname
DIRS=\
@@ -45,24 +42,6 @@
echo '----'$i'----'
mk all
}
-
-cc.$O: cc.c
- mk -f /sys/src/cmd/mkfile cc.$O
-
-$O.cc: cc.$O
- mk -f /sys/src/cmd/mkfile $O.cc
-
-cp.$O: cp.c
- mk -f /sys/src/cmd/mkfile cp.$O
-
-$O.cp: cp.$O
- mk -f /sys/src/cmd/mkfile $O.cp
-
-mv.$O: mv.c
- mk -f /sys/src/cmd/mkfile mv.$O
-
-$O.mv: mv.$O
- mk -f /sys/src/cmd/mkfile $O.mv
$BIN/%: %.rc
cp -x $stem.rc $BIN/$stem
--- a/sys/src/ape/cmd/mv.c
+++ /dev/null
@@ -1,250 +1,0 @@
-#include <u.h>
-#include <libc.h>
-
-int copy1(int fdf, int fdt, char *from, char *to);
-void hardremove(char *);
-int mv(char *from, char *todir, char *toelem);
-int mv1(char *from, Dir *dirb, char *todir, char *toelem);
-int samefile(char *, char *);
-void split(char *, char **, char **);
-
-void
-main(int argc, char *argv[])
-{
- int i, failed;
- Dir *dirto, *dirfrom;
- char *todir, *toelem;
-
- if(argc<3){
- fprint(2, "usage: mv fromfile tofile\n");
- fprint(2, " mv fromfile ... todir\n");
- exits("bad usage");
- }
-
- /* Skip -f */
- if(argv[1][0] == '-' && argv[1][1] == 'f' && argv[1][2] == 0) {
- for(i=2; i<argc; i++) {
- argv[i-1] = argv[i];
- }
- argc--;
- }
-
- /* prepass to canonicalise names before splitting, etc. */
- for(i=1; i < argc; i++)
- cleanname(argv[i]);
-
- if((dirto = dirstat(argv[argc-1])) != nil && (dirto->mode&DMDIR)){
- dirfrom = nil;
- if(argc == 3
- && (dirfrom = dirstat(argv[1])) != nil
- && (dirfrom->mode & DMDIR))
- split(argv[argc-1], &todir, &toelem); /* mv dir1 dir2 */
- else{ /* mv file... dir */
- todir = argv[argc-1];
- toelem = nil; /* toelem will be fromelem */
- }
- free(dirfrom);
- }else
- split(argv[argc-1], &todir, &toelem); /* mv file1 file2 */
- free(dirto);
- if(argc>3 && toelem != nil){
- fprint(2, "mv: %s not a directory\n", argv[argc-1]);
- exits("bad usage");
- }
-
- failed = 0;
- for(i=1; i < argc-1; i++)
- if(mv(argv[i], todir, toelem) < 0)
- failed++;
- if(failed)
- exits("failure");
- exits(0);
-}
-
-int
-mv(char *from, char *todir, char *toelem)
-{
- int stat;
- Dir *dirb;
-
- dirb = dirstat(from);
- if(dirb == nil){
- fprint(2, "mv: can't stat %s: %r\n", from);
- return -1;
- }
- stat = mv1(from, dirb, todir, toelem);
- free(dirb);
- return stat;
-}
-
-int
-mv1(char *from, Dir *dirb, char *todir, char *toelem)
-{
- int fdf, fdt, i, j, stat;
- char toname[4096], fromname[4096];
- char *fromdir, *fromelem;
- Dir *dirt, null;
-
- i = strlen(from);
- if(i >= sizeof(fromname)){
- fprint(2, "mv: path too big (max %d): %s\n",
- sizeof(fromname), from);
- return -1;
- }
- memmove(fromname, from, i+1);
- split(from, &fromdir, &fromelem);
- if(toelem == 0)
- toelem = fromelem;
- i = strlen(toelem);
- if(i==0){
- fprint(2, "mv: null last name element moving %s\n", fromname);
- return -1;
- }
- j = strlen(todir);
- if(i + j + 2 > sizeof toname){
- fprint(2, "mv: path too big (max %d): %s/%s\n",
- sizeof toname, todir, toelem);
- return -1;
- }
- memmove(toname, todir, j);
- toname[j] = '/';
- memmove(toname+j+1, toelem, i);
- toname[i+j+1] = 0;
-
- if(samefile(fromdir, todir)){
- if(samefile(fromname, toname)){
- fprint(2, "mv: %s and %s are the same\n",
- fromname, toname);
- return -1;
- }
-
- /* remove target if present */
- dirt = dirstat(toname);
- if(dirt != nil) {
- hardremove(toname);
- free(dirt);
- }
-
- /* try wstat */
- nulldir(&null);
- null.name = toelem;
- if(dirwstat(fromname, &null) >= 0)
- return 0;
- if(dirb->mode & DMDIR){
- fprint(2, "mv: can't rename directory %s: %r\n",
- fromname);
- return -1;
- }
- }
- /*
- * Renaming won't work --- must copy
- */
- if(dirb->mode & DMDIR){
- fprint(2, "mv: %s is a directory, not copied to %s\n",
- fromname, toname);
- return -1;
- }
- fdf = open(fromname, OREAD);
- if(fdf < 0){
- fprint(2, "mv: can't open %s: %r\n", fromname);
- return -1;
- }
-
- dirt = dirstat(toname);
- if(dirt != nil && (dirt->mode & DMAPPEND))
- hardremove(toname); /* because create() won't truncate file */
- free(dirt);
-
- fdt = create(toname, OWRITE, dirb->mode);
- if(fdt < 0){
- fprint(2, "mv: can't create %s: %r\n", toname);
- close(fdf);
- return -1;
- }
- stat = copy1(fdf, fdt, fromname, toname);
- close(fdf);
-
- if(stat >= 0){
- nulldir(&null);
- null.mtime = dirb->mtime;
- null.mode = dirb->mode;
- dirfwstat(fdt, &null); /* ignore errors; e.g. user none always fails */
- if(remove(fromname) < 0){
- fprint(2, "mv: can't remove %s: %r\n", fromname);
- stat = -1;
- }
- }
- close(fdt);
- return stat;
-}
-
-int
-copy1(int fdf, int fdt, char *from, char *to)
-{
- char buf[8192];
- long n, n1;
-
- while ((n = read(fdf, buf, sizeof buf)) > 0) {
- n1 = write(fdt, buf, n);
- if(n1 != n){
- fprint(2, "mv: error writing %s: %r\n", to);
- return -1;
- }
- }
- if(n < 0){
- fprint(2, "mv: error reading %s: %r\n", from);
- return -1;
- }
- return 0;
-}
-
-void
-split(char *name, char **pdir, char **pelem)
-{
- char *s;
-
- s = utfrrune(name, '/');
- if(s){
- *s = 0;
- *pelem = s+1;
- *pdir = name;
- }else if(strcmp(name, "..") == 0){
- *pdir = "..";
- *pelem = ".";
- }else{
- *pdir = ".";
- *pelem = name;
- }
-}
-
-int
-samefile(char *a, char *b)
-{
- Dir *da, *db;
- int ret;
-
- if(strcmp(a, b) == 0)
- return 1;
- da = dirstat(a);
- db = dirstat(b);
- ret = (da != nil && db != nil &&
- da->qid.type==db->qid.type &&
- da->qid.path==db->qid.path &&
- da->qid.vers==db->qid.vers &&
- da->dev==db->dev &&
- da->type==db->type);
- free(da);
- free(db);
- return ret;
-}
-
-void
-hardremove(char *a)
-{
- if(remove(a) == -1){
- fprint(2, "mv: can't remove %s: %r\n", a);
- exits("mv");
- }
- while(remove(a) != -1)
- ;
-}