simplify Delete code.

This commit is contained in:
Kelvin Sherlock 2018-07-30 20:27:36 -04:00
parent 2566d45238
commit 8fa64baae0
2 changed files with 31 additions and 91 deletions

120
Delete.c
View File

@ -24,87 +24,44 @@
#include <ErrMgr.h>
#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;

View File

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