sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit 56299722236031c9ba416d8129eca8726c3a79da
parent 6c7f288bd81648e789a3d6717a8a651fc557b059
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Thu, 26 May 2011 05:47:58 +0100

add mkdir
Diffstat:
Makefile | 2+-
mkdir.1 | 17+++++++++++++++++
mkdir.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
pwd.c | 1-
touch.1 | 6+++---
5 files changed, 69 insertions(+), 5 deletions(-)

diff --git 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 @@ -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 @@ -0,0 +1,48 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> +#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 @@ -1,7 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <stdio.h> #include <stdlib.h> -#include <unistd.h> #include "util.h" int diff --git 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