Skip to content

Commit

Permalink
Merge pull request #1750 from ghaerr/perror
Browse files Browse the repository at this point in the history
[libc,cmds] Fix perror and rm -f, improve rmdir
  • Loading branch information
ghaerr authored Oct 8, 2023
2 parents e6b268e + cf7b0ee commit 838f4ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
45 changes: 23 additions & 22 deletions elkscmd/file_utils/rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <fcntl.h>
#include <limits.h>

static int errcode;
int fflg, rflg, iflg;
int errcode;

int usage(void)
{
Expand Down Expand Up @@ -37,24 +38,24 @@ int dotname(char *s) {
return(0);
}

void rm(char *arg, int fflg, int rflg, int iflg, int level) {
struct stat buf;
void rm(char *arg, int level)
{
struct dirent *dp;
DIR *dirp;
struct stat buf;
char name[PATH_MAX];

if(lstat(arg, &buf)) {
if (fflg==0) {
printf("rm: %s nonexistent\n", arg);
++errcode;
}
if (!fflg)
perror(arg);
errcode++;
return;
}
if ((buf.st_mode&S_IFMT) == S_IFDIR) {
if(rflg) {
if (access(arg, O_WRONLY) < 0) {
if (fflg==0)
printf("rm: %s not changed\n", arg);
if (!fflg)
fprintf(stderr, "rm: %s not changed\n", arg);
errcode++;
return;
}
Expand All @@ -64,13 +65,14 @@ void rm(char *arg, int fflg, int rflg, int iflg, int level) {
return;
}
if((dirp = opendir(arg)) == NULL) {
printf("rm: cannot read %s?\n", arg);
perror(arg);
errcode++;
return;
}
while((dp = readdir(dirp)) != NULL) {
if(dp->d_ino != 0 && !dotname(dp->d_name)) {
sprintf(name, "%s/%s", arg, dp->d_name);
rm(name, fflg, rflg, iflg, level+1);
rm(name, level+1);
}
}
closedir(dirp);
Expand All @@ -84,7 +86,7 @@ void rm(char *arg, int fflg, int rflg, int iflg, int level) {
return;
}
fprintf(stderr, "rm: %s is a directory\n", arg);
++errcode;
errcode++;
return;
}

Expand All @@ -99,20 +101,19 @@ void rm(char *arg, int fflg, int rflg, int iflg, int level) {
return;
}
}
if (unlink(arg) && (fflg==0 || iflg)) {
if (unlink(arg) && (!fflg || iflg)) {
fprintf(stderr, "rm: %s not removed\n", arg);
++errcode;
errcode++;
}
return;
}

int main(int argc, char **argv) {
int force=0, recurse = 0, interact =0 ;
int main(int argc, char **argv)
{
char *arg;

if (argc < 2)
return usage();
errcode = 0;

while(argc>1 && argv[1][0]=='-') {
arg = *++argv;
Expand All @@ -126,13 +127,13 @@ int main(int argc, char **argv) {
while(*++arg != '\0')
switch(*arg) {
case 'f':
force++;
fflg = 1;
break;
case 'i':
interact++;
iflg = 1;
break;
case 'r':
recurse++;
rflg = 1;
break;
default:
return usage();
Expand All @@ -143,7 +144,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "rm: cannot remove `..'\n");
continue;
}
rm(*argv, force, recurse, interact, 0);
rm(*argv, 0);
}
return(errcode);
return fflg? 0: errcode;
}
51 changes: 26 additions & 25 deletions elkscmd/file_utils/rmdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@

static int remove_dir(char *name, int f)
{
int ret, once = 1;
char *line;
while (((ret = rmdir(name)) == 0) && ((line = strrchr(name,'/')) != NULL) && f) {
while ((line > name) && (*line == '/'))
--line;
line[1] = 0;
once = 0;
}
return (ret && once);
int ret, once = 1;
char *line;

while (((ret = rmdir(name)) == 0) && ((line = strrchr(name,'/')) != NULL) && f) {
while ((line > name) && (*line == '/'))
--line;
line[1] = 0;
once = 0;
}
return (ret && once);
}


int main(int argc, char **argv)
{
int i, parent = 0, force = 0, er = 0;
if (argc < 2) goto usage;
int i, parent = 0, force = 0, ret = 0;

if (argc < 2) goto usage;

while (argv[1][0] == '-') {
switch (argv[1][1]) {
Expand All @@ -36,17 +35,19 @@ int main(int argc, char **argv)
argc--;
}

for (i = 1; i < argc; i++) {
while (argv[i][strlen(argv[i])-1] == '/')
argv[i][strlen(argv[i])-1] = '\0';
if (remove_dir(argv[i],parent)) {
perror(argv[i]);
er = 1;
}
}
return force? 0: er;
for (i = 1; i < argc; i++) {
while (argv[i][strlen(argv[i])-1] == '/')
argv[i][strlen(argv[i])-1] = '\0';
if (remove_dir(argv[i],parent)) {
errstr(argv[i]);
errmsg(": cannot remove directory\n");
if (!force)
ret = 1;
}
}
return ret;

usage:
errmsg("usage: rmdir [-pf] directory [...]\n");
return 1;
errmsg("usage: rmdir [-pf] directory [...]\n");
return 1;
}
2 changes: 1 addition & 1 deletion libc/error/perror.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ perror(const char *str)
{
char *ptr;

if (!str) {
if (str) {
write(2, str, strlen(str));
write(2, ": ", 2);
}
Expand Down

0 comments on commit 838f4ba

Please sign in to comment.