9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

commit 9659cd5b37e1f0a6539a0ec5cf145d0e649fc973
parent 07cf44e6452f7928fe20cc2849c249b28d19344f
Author: anselm@garbe.us <unknown>
Date:   Mon, 22 Mar 2010 08:07:02 +0000

added mkdir as well
Diffstat:
Makefile | 2+-
mkdir/Makefile | 11+++++++++++
mkdir/mkdir.c | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -3,7 +3,7 @@ include config.mk SUBDIRS = lib9 yacc awk basename bc cal cat cleanname date dc du echo grep hoc ls \ - mk mtime rc read sed seq sleep sort tee test touch tr troff uniq + mk mkdir mtime rc read sed seq sleep sort tee test touch tr troff uniq all: @echo 9base build options: diff --git a/mkdir/Makefile b/mkdir/Makefile @@ -0,0 +1,11 @@ +# mkdir - unix port from plan9 +# +# Depends on ../lib9 + +TARG = mkdir + +include ../std.mk + +pre-uninstall: + +post-install: diff --git a/mkdir/mkdir.c b/mkdir/mkdir.c @@ -0,0 +1,80 @@ +#include <u.h> +#include <libc.h> + +char *e; +ulong mode = 0777L; + +void +usage(void) +{ + fprint(2, "usage: mkdir [-p] [-m mode] dir...\n"); + exits("usage"); +} + +int +makedir(char *s) +{ + int f; + + if(access(s, AEXIST) == 0){ + fprint(2, "mkdir: %s already exists\n", s); + e = "error"; + return -1; + } + f = create(s, OREAD, DMDIR | mode); + if(f < 0){ + fprint(2, "mkdir: can't create %s: %r\n", s); + e = "error"; + return -1; + } + close(f); + return 0; +} + +void +mkdirp(char *s) +{ + char *p; + + for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){ + *p = 0; + if(access(s, AEXIST) != 0 && makedir(s) < 0) + return; + *p = '/'; + } + if(access(s, AEXIST) != 0) + makedir(s); +} + + +void +main(int argc, char *argv[]) +{ + int i, pflag; + char *m; + + pflag = 0; + ARGBEGIN{ + default: + usage(); + case 'm': + m = ARGF(); + if(m == nil) + usage(); + mode = strtoul(m, &m, 8); + if(mode > 0777) + usage(); + break; + case 'p': + pflag = 1; + break; + }ARGEND + + for(i=0; i<argc; i++){ + if(pflag) + mkdirp(argv[i]); + else + makedir(argv[i]); + } + exits(e); +}