add mkdir

This commit is contained in:
Connor Lane Smith 2011-05-26 05:47:58 +01:00
parent 6c7f288bd8
commit 5629972223
5 changed files with 69 additions and 5 deletions

View File

@ -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)

17
mkdir.1 Normal file
View File

@ -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)

48
mkdir.c Normal file
View File

@ -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);
}

1
pwd.c
View File

@ -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

View File

@ -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