From 56299722236031c9ba416d8129eca8726c3a79da Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Thu, 26 May 2011 05:47:58 +0100 Subject: [PATCH] add mkdir --- Makefile | 2 +- mkdir.1 | 17 +++++++++++++++++ mkdir.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ pwd.c | 1 - touch.1 | 6 +++--- 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 mkdir.1 create mode 100644 mkdir.c diff --git a/Makefile b/Makefile index 6b69ec0..84c092f 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ include config.mk LIB = util/afgets.o util/agetcwd.o util/enmasse.o util/eprintf.o util/recurse.o SRC = basename.c cat.c chown.c date.c dirname.c echo.c false.c grep.c head.c \ - ln.c ls.c mkfifo.c pwd.c rm.c sleep.c tee.c touch.c true.c wc.c + ln.c ls.c mkdir.c mkfifo.c pwd.c rm.c sleep.c tee.c touch.c true.c wc.c OBJ = $(SRC:.c=.o) $(LIB) BIN = $(SRC:.c=) MAN = $(SRC:.c=.1) diff --git a/mkdir.1 b/mkdir.1 new file mode 100644 index 0000000..3992e8f --- /dev/null +++ b/mkdir.1 @@ -0,0 +1,17 @@ +.TH MKDIR 1 sbase\-VERSION +.SH NAME +mkdir \- make directory +.SH SYNOPSIS +.B mkdir +.RB [ \-p ] +.RI [ name ...] +.SH DESCRIPTION +.B mkdir +creates the specified directories. +.SH OPTIONS +.TP +.B \-p +creates any necessary parent directories, and does not fail if the target +already exists. +.SH SEE ALSO +.IR mkdir (2) diff --git a/mkdir.c b/mkdir.c new file mode 100644 index 0000000..6fe5534 --- /dev/null +++ b/mkdir.c @@ -0,0 +1,48 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include +#include +#include +#include +#include +#include "util.h" + +static void mkdirp(char *); + +int +main(int argc, char *argv[]) +{ + bool pflag = false; + char c; + + while((c = getopt(argc, argv, "p")) != -1) + switch(c) { + case 'p': + pflag = true; + break; + default: + exit(EXIT_FAILURE); + } + for(; optind < argc; optind++) + if(pflag) + mkdirp(argv[optind]); + else if(mkdir(argv[optind], S_IRWXU|S_IRWXG|S_IRWXO) != 0) + eprintf("mkdir %s:", argv[optind]); + return EXIT_SUCCESS; +} + +void +mkdirp(char *path) +{ + char *p = path; + + do { + if((p = strchr(&p[1], '/'))) + *p = '\0'; + if(mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO) != 0 && errno != EEXIST) + eprintf("mkdir %s:", path); + if(p) + *p = '/'; + } while(p); +} diff --git a/pwd.c b/pwd.c index 0c0bbda..b098953 100644 --- a/pwd.c +++ b/pwd.c @@ -1,7 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include #include -#include #include "util.h" int diff --git a/touch.1 b/touch.1 index 85a1118..60d0a93 100644 --- a/touch.1 +++ b/touch.1 @@ -1,6 +1,6 @@ .TH TOUCH 1 sbase\-VERSION .SH NAME -touch \- set files' modification time +touch \- set files' timestamps .SH SYNOPSIS .B touch .RB [ \-c ] @@ -9,8 +9,8 @@ touch \- set files' modification time .RI [ file ...] .SH DESCRIPTION .B touch -sets the files' modification time to the current time. If a file does not exist -it is created. +sets the given files' modification time to the current time. If a file does not +exist it is created. .SH OPTIONS .TP .B \-c