I have a gripe about the fread() business - I have to hit ^D twice before eof is reported - the first ^D causes fread() to return non-zero, with text I entered previously, the second ^D causes fread() to return 0. Probably not a major problem but here's a patch if anyone's interested.
Also rm.c I don't think it has any problems with symlinks, but I accept no responsibility for your lost home directory, anime collection or childhood memories. I've never ever found a use for -f, it's just a pain having rm prompt me, so I've assumed an always present -f. Stick it in if you're going for posix or whatever though. One more thing, we're not bothering with the -- flag, are we? > but I'm quite busy with exams atm. UK, right? Up late coding even with exams, I'm impressed. Finally, I have an editor in the works, unfinished, but I plan to show you lot at some point though</vapourware>. Cheers, Rob.
fread.diff
Description: plain/text
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include "util.h"
int rflag = 0;
void rm(const char *);
int
main(int argc, char *argv[])
{
int i;
if(argc == 1)
eprintf("usage: %s [-r] files...\n", argv[0]);
for(i = 1; i < argc; i++) {
if(!strcmp(argv[i], "-r"))
rflag = 1;
else
rm(argv[i]);
}
return EXIT_SUCCESS;
}
void rm(const char *path)
{
if(remove(path) == -1){
if(errno == ENOTEMPTY && rflag){
DIR *d = opendir(path);
struct dirent *ent;
char prev[BUFSIZ];
if(!getcwd(prev, sizeof prev))
eprintf("getcwd:");
if(chdir(path))
eprintf("chdir %s:", path);
while((ent = readdir(d)))
if(strcmp(ent->d_name, "..") && strcmp(ent->d_name, "."))
rm(ent->d_name);
closedir(d);
if(chdir(prev) == -1)
eprintf("chdir %s:", prev);
if(remove(path) == -1)
eprintf("remove %s:", path);
}else
eprintf("remove %s:", path);
}
}
rm.1
Description: Binary data
