From 4b4e15da92b1ed17073dabc13250bed8865ae141 Mon Sep 17 00:00:00 2001 From: Bill Chatfield Date: Mon, 4 Apr 2022 22:18:19 -0400 Subject: [PATCH] Some updates --- copy.c | 72 ++++++++++++++++++++++++++++++++------------------- cui.h | 8 ------ cui.c => io.c | 18 +++---------- io.h | 15 +++++++++++ prodos.h | 5 ---- prodosext.c | 11 ++++++++ prodosext.h | 1 + 7 files changed, 75 insertions(+), 55 deletions(-) delete mode 100644 cui.h rename cui.c => io.c (62%) create mode 100644 io.h diff --git a/copy.c b/copy.c index 4214e04..d9981e6 100644 --- a/copy.c +++ b/copy.c @@ -1,59 +1,62 @@ -#include /* fopen, fread, fwrite, fclose */ +#include /* fopen, fread, fwrite, fclose, FILENAME_MAX */ #include #include /* atexit */ #include +#include /* _filetype, _auxtype */ #include "libgen.h" /* basename */ #include "prodos.h" #include "prodosext.h" -#include "cui.h" +#include "io.h" -#define BF_SIZ 2048 +#define COPY_BUF_SIZ 2048 -void cleanup(void); -FILE *openFile(const char *name, const char *mode); -void closeFile(FILE *f, const char *name); -void concatPath(char *dest, const char *src); +enum Status {SUCCESS, FAILURE}; -char srcName[PRODOS_PATH_MAX + 1]; -char destName[PRODOS_PATH_MAX + 1]; +static void cleanup(void); +static FILE *openFile(const char *name, const char *mode); +static void closeFile(FILE *f, const char *name); +static void concatPath(char *accum, const char *src); +static void copyFile(FILE *src, FILE *dest); + +FilePath srcName; +FilePath destName; FILE *src = NULL; FILE *dest = NULL; struct GetFileInfoParams srcInfo; struct GetFileInfoParams destInfo; size_t n; -char buf[BF_SIZ]; +char buf[BUF_SIZ]; size_t bytesRead; +uint8_t result; void main(void) { atexit(cleanup); - if (! inputFileName("Source file or directory:", srcName, - sizeof(srcName), &srcInfo)) + if (! inputFileName("Source file:", srcName)) return; - if (! inputFileName("Destination file or directory:", destName, - sizeof(destName), &destInfo)) + if (! inputFileName("Destination file or directory:", destName)) return; - src = openFile(srcName, "r"); + if ((result = getFileInfo(srcName, srcInfo)) != PRODOS_E_NONE) { + fprintf(stderr, "%s: %s (code %d)\n", srcName, getMessage(result), result); + return; + } + + if ((result = getFileInfo(destName, destInfo)) != PRODOS_E_NONE) { + fprintf(stderr, "%s: %s (code %d)\n", srcName, getMessage(result), result); + return; + } if (isDirectory(&destInfo)) concatPath(destName, basename(srcName)); - dest = openFile(destName, "w"); - - while ((bytesRead = fread(buf, 1, sizeof(buf), src)) == BF_SIZ) - if (fwrite(buf, 1, bytesRead, dest) < bytesRead) { - perror(destName); - break; - } - - if (bytesRead > 0 && !feof(dest) && !ferror(dest)) - if (fwrite(buf, 1, bytesRead, dest) < bytesRead) - perror(destName); + _filetype = srcInfo->file_type; + _auxtype = srcInfo->aux_type; + copyFile(openFile(srcName, "r"), openFile(destName, "w")); cleanup(); } @@ -63,7 +66,6 @@ FILE *openFile(const char *name, const char *mode) f = fopen(name, "r"); if (f == NULL) { perror(name); - exit(EXIT_FAILURE); } return f; } @@ -92,3 +94,19 @@ void cleanup(void) closeFile(src, srcName); } +void copyFile(FILE *src, FILE *dest) +{ + if (src == NULL || dest == NULL) + return; + + while ((bytesRead = fread(buf, 1, sizeof(buf), src)) == COPY_BUF_SIZ) + if (fwrite(buf, 1, bytesRead, dest) < bytesRead) { + perror(destName); + break; + } + + if (bytesRead > 0 && !feof(dest) && !ferror(dest)) + if (fwrite(buf, 1, bytesRead, dest) < bytesRead) + perror(destName); +} + diff --git a/cui.h b/cui.h deleted file mode 100644 index 1c797ac..0000000 --- a/cui.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef CUI_H -#define CUI_H - -extern bool inputFileName(const char *prompt, char *name, size_t capacity, - struct GetFileInfoParams *params); - -#endif - diff --git a/cui.c b/io.c similarity index 62% rename from cui.c rename to io.c index be36dd1..ae3d11f 100644 --- a/cui.c +++ b/io.c @@ -1,8 +1,7 @@ #include -#include #include /* strlen */ -#include "cui.h" +#include "io.h" #include "prodos.h" #include "prodosext.h" @@ -13,15 +12,13 @@ static void chomp(char *line); static bool complete; static uint8_t result; -bool inputFileName(const char *prompt, char *name, - size_t capacity, - struct GetFileInfoParams *params) +bool inputFileName(const char *prompt, FilePath name) { complete = false; while (! complete) { printf(prompt); - readLine(name, capacity); + readLine(name, sizeof(name)); if (strlen(name) == 0) { puts("Aborting"); break; @@ -30,15 +27,6 @@ bool inputFileName(const char *prompt, char *name, puts("Escaping"); break; } - params->param_count = GET_FILE_INFO_PARAM_COUNT; - params->pathname = name; - printf("params address = %x\n", &(params->param_count)); - printf("params address = %x\n", params); - result = get_file_info(params); - if (result == PRODOS_E_NONE) - complete = true; - else - fprintf(stderr, "%s: %s (code %d)\n", name, getMessage(result), result); } return complete; } diff --git a/io.h b/io.h new file mode 100644 index 0000000..36af712 --- /dev/null +++ b/io.h @@ -0,0 +1,15 @@ +#ifndef CUI_H +#define CUI_H + +#include /* FILENAME_MAX */ +/** + * FilePath is a cc65 platform independent type because the + * FILENAME_MAX constant is defined for each platform in + * stdio.h. It includes space for a string terminator. + */ +typedef char FilePath[FILENAME_MAX]; + +extern bool inputFileName(const char *prompt, FilePath name); + +#endif + diff --git a/prodos.h b/prodos.h index 93d8e30..82eb042 100644 --- a/prodos.h +++ b/prodos.h @@ -3,11 +3,6 @@ #include -/* PATH_MAX POSIX constant - ProDOS has a 64 char prefix that can be combined - with partial pathname of 64 characters. This is - a total of 128 characters. */ -#define PRODOS_PATH_MAX 128 #define PRODOS_FILE_NAME_MAX 15 #define PRODOS_E_NONE 0x00 diff --git a/prodosext.c b/prodosext.c index 00e2a0f..6fcd5a4 100644 --- a/prodosext.c +++ b/prodosext.c @@ -1,7 +1,18 @@ #include +#include #include #include "prodos.h" #include "prodosext.h" +#include "io.h" + +uint8_t getFileInfo(FilePath name, struct GetFileInfoParams *params) +{ + params->param_count = GET_FILE_INFO_PARAM_COUNT; + params->pathname = name; + printf("params address = %x\n", &(params->param_count)); + printf("params address = %x\n", params); + return get_file_info(params); +} bool isDirectory(struct GetFileInfoParams *params) { diff --git a/prodosext.h b/prodosext.h index b877053..6a2a8b1 100644 --- a/prodosext.h +++ b/prodosext.h @@ -4,6 +4,7 @@ #include #include +extern bool getFileInfo(FileName name, struct GetFileInfoParams *params); extern bool isDirectory(struct GetFileInfoParams *params); extern const char *getMessage(uint8_t errorCode);