add rm, thanks rob
This commit is contained in:
parent
9714d7b1d3
commit
8c76381e91
1
LICENSE
1
LICENSE
|
@ -3,6 +3,7 @@ MIT/X Consortium License
|
|||
© 2011 Connor Lane Smith <cls@lubutu.com>
|
||||
© 2011 Kamil Cholewiński <harry666t@gmail.com>
|
||||
© 2011 stateless <stateless@archlinux.us>
|
||||
© 2011 Rob Pilling <robpilling@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a
|
||||
copy of this software and associated documentation files (the "Software"),
|
||||
|
|
2
Makefile
2
Makefile
|
@ -1,6 +1,6 @@
|
|||
include config.mk
|
||||
|
||||
SRC = basename.c cat.c date.c echo.c false.c grep.c pwd.c sleep.c tee.c touch.c true.c wc.c
|
||||
SRC = basename.c cat.c date.c echo.c false.c grep.c pwd.c rm.c sleep.c tee.c touch.c true.c wc.c
|
||||
OBJ = $(SRC:.c=.o) util.o
|
||||
BIN = $(SRC:.c=)
|
||||
MAN = $(SRC:.c=.1)
|
||||
|
|
17
rm.1
Normal file
17
rm.1
Normal file
|
@ -0,0 +1,17 @@
|
|||
.TH RM 1 sbase\-VERSION
|
||||
.SH NAME
|
||||
rm \- remove files and directories
|
||||
.SH SYNOPSIS
|
||||
.B rm
|
||||
.RB [ \-fr ]
|
||||
.RI [ files ...]
|
||||
.SH DESCRIPTION
|
||||
.B rm
|
||||
removes the given files and directories.
|
||||
.SH OPTIONS
|
||||
.TP
|
||||
.B \-f
|
||||
ignored, for compatibility.
|
||||
.TP
|
||||
.B \-r
|
||||
remove directories recursively.
|
58
rm.c
Normal file
58
rm.c
Normal file
|
@ -0,0 +1,58 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "util.h"
|
||||
|
||||
static void rm(const char *);
|
||||
|
||||
static bool rflag = 0;
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char c;
|
||||
|
||||
while((c = getopt(argc, argv, "fr")) != -1)
|
||||
switch(c) {
|
||||
case 'f':
|
||||
break;
|
||||
case 'r':
|
||||
rflag = true;
|
||||
break;
|
||||
default:
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
for(; optind < argc; optind++)
|
||||
rm(argv[optind]);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void rm(const char *path)
|
||||
{
|
||||
if(remove(path) == 0)
|
||||
return;
|
||||
if(errno == ENOTEMPTY && rflag) {
|
||||
struct dirent *d;
|
||||
DIR *dp;
|
||||
|
||||
if(!(dp = opendir(path)))
|
||||
eprintf("opendir %s:", path);
|
||||
if(chdir(path) != 0)
|
||||
eprintf("chdir %s:", path);
|
||||
while((d = readdir(dp)))
|
||||
if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
|
||||
rm(d->d_name);
|
||||
|
||||
closedir(dp);
|
||||
if(chdir("..") != 0)
|
||||
eprintf("chdir:");
|
||||
if(remove(path) == 0)
|
||||
return;
|
||||
}
|
||||
eprintf("remove %s:", path);
|
||||
}
|
4
touch.c
4
touch.c
|
@ -43,7 +43,7 @@ touch(const char *str)
|
|||
struct stat st;
|
||||
struct utimbuf ut;
|
||||
|
||||
if(stat(str, &st) < 0) {
|
||||
if(stat(str, &st) != 0) {
|
||||
if(errno != ENOENT)
|
||||
eprintf("stat %s:", str);
|
||||
if(cflag)
|
||||
|
@ -54,6 +54,6 @@ touch(const char *str)
|
|||
}
|
||||
ut.actime = st.st_atime;
|
||||
ut.modtime = t;
|
||||
if(utime(str, &ut) < 0)
|
||||
if(utime(str, &ut) != 0)
|
||||
eprintf("utime %s:", str);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user