From 8fa64baae0b97758f0798622742303ecb97dc583 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Mon, 30 Jul 2018 20:27:36 -0400 Subject: [PATCH] simplify Delete code. --- Delete.c | 120 ++++++++++++++----------------------------------------- makefile | 2 +- 2 files changed, 31 insertions(+), 91 deletions(-) diff --git a/Delete.c b/Delete.c index 6fe263c..7d7741d 100644 --- a/Delete.c +++ b/Delete.c @@ -24,87 +24,44 @@ #include #include "Delete-flags.h" - -char *c2p(const char *cp) -{ - int length; - char *p; +#include "FileCommon.h" - if (!cp) return NULL; - length = strlen(cp); - if (length > 255) - { - fprintf(stderr, "Error: Pathname is too long.\n"); - exit(1); - return NULL; - } - - p = malloc(length + 2); // + 2 for \0 and length. - if (!p) - { - fprintf(stderr, "Error: unable to allocate memory.\n"); - exit(1); - return NULL; - } - - p[0] = length; - memcpy(p + 1, cp, length + 1); - return p; -} - -// -1 - error. -// 0 - no file -// 1 - regular file -// 2 - directory. -int mode(const char *pname) -{ - //char *pname; - CInfoPBRec rec; - OSErr status; - - memset(&rec, 0, sizeof(rec)); - - //pname = c2p(path); - //if (!pname) return -1; - - rec.hFileInfo.ioNamePtr = (unsigned char *)pname; - status = PBGetCatInfo(&rec, false); - //free(pname); - - if (status) return 0; - if (rec.hFileInfo.ioFlAttrib & kioFlAttribDirMask) - return 2; - - return 1; -} - -int prompt(const char *file) { - char ch; - char first; - - fprintf(stderr, "remove %s? ", file); - fflush(stderr); - - first = ch = getchar(); - while (ch != '\n' && ch != EOF) ch = getchar(); - if (first == 'y' || first == 'Y') return 1; - if (first == 'c' || first == 'C') return -1; - return 0; -} - -int check(const char *name) { - int m = mode(name); +int check(const unsigned char *pname) { + int m = mode(pname, NULL); if (m != 2 || flags._y) return 1; // directory. if (flags._n) return 0; if (flags._c) return -1; - return prompt(name); + return prompt(pname, "remove"); } static char error_message[255]; + +int do_delete(const unsigned char *file) { + OSErr err; + int st; + + st = check(file); + if (st < 0) return 4; + + err = FSDelete(file, 0); + if (err) { + if (flags._i) return 0; + fprintf(stderr, "### Delete - unable to delete %s\n", file+1); + fprintf(stderr, "# %s\n", GetSysErrText(err, error_message)); + return 2; + } + + if (flags._p) { + fprintf(stderr, "%s deleted.\n", file+1); + } + + return 0; +} + int main(int argc, char **argv) { int i; int status = 0; @@ -129,29 +86,12 @@ int main(int argc, char **argv) { InitErrMgr(NULL, NULL, true); for (i = 1 ; i < argc; ++i) { - OSErr err; - int st; char *file = argv[i]; - char *p = c2p(file); - // todo -- y/n/c flags. - - st = check(file); - if (st == 0) continue; - if (st == -1) { status = 4; break; } - - - err = FSDelete((unsigned char *)p, 0); - if (err && !flags._i) { - fprintf(stderr, "### Delete - unable to delete %s\n", file); - fprintf(stderr, "# %s\n", GetSysErrText(err, error_message)); - status = 2; - break; - } - if (!err && flags._p) { - fprintf(stderr, "%s deleted.\n", file); - } + unsigned char *p = c2p(file); + status = do_delete(p); free(p); + if (status) break; } CloseErrMgr(); return status; diff --git a/makefile b/makefile index 13754e5..18bc223 100644 --- a/makefile +++ b/makefile @@ -62,7 +62,7 @@ lib/libc: libc/strcasecmp.c.o libc/_getprogname.c.o libc/err.c.o libc/getopt.c.o -Delete: Delete.c.o Delete-flags.c.o $(LIBRARIES) +Delete: Delete.c.o Delete-flags.c.o FileCommon.c.o $(LIBRARIES) $(MPW) Link $(LDFLAGS) -o $@ $^ $(LIBS)