[hackers] [9base] added mkdir as well || anselm

From: <hg_AT_suckless.org>
Date: Mon, 22 Mar 2010 08:07:06 +0000 (UTC)

changeset: 51:c8d16fc44638
tag: tip
user: anselm_AT_garbe.us
date: Mon Mar 22 08:07:02 2010 +0000
files: Makefile mkdir/Makefile mkdir/mkdir.c
description:
added mkdir as well

diff -r f46cbd1821d1 -r c8d16fc44638 Makefile
--- a/Makefile Mon Mar 22 08:04:31 2010 +0000
+++ b/Makefile Mon Mar 22 08:07:02 2010 +0000
@@ -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 -r f46cbd1821d1 -r c8d16fc44638 mkdir/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mkdir/Makefile Mon Mar 22 08:07:02 2010 +0000
@@ -0,0 +1,11 @@
+# mkdir - unix port from plan9
+#
+# Depends on ../lib9
+
+TARG = mkdir
+
+include ../std.mk
+
+pre-uninstall:
+
+post-install:
diff -r f46cbd1821d1 -r c8d16fc44638 mkdir/mkdir.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mkdir/mkdir.c Mon Mar 22 08:07:02 2010 +0000
@@ -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);
+}
Received on Mon Mar 22 2010 - 08:07:06 UTC

This archive was generated by hypermail 2.2.0 : Mon Mar 22 2010 - 08:12:07 UTC