Some updates

This commit is contained in:
Bill Chatfield 2022-04-04 22:18:19 -04:00
parent 670f41833e
commit 4b4e15da92
7 changed files with 75 additions and 55 deletions

72
copy.c
View File

@ -1,59 +1,62 @@
#include <stdio.h> /* fopen, fread, fwrite, fclose */
#include <stdio.h> /* fopen, fread, fwrite, fclose, FILENAME_MAX */
#include <stdbool.h>
#include <stdlib.h> /* atexit */
#include <string.h>
#include <apple2.h> /* _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);
}

8
cui.h
View File

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

View File

@ -1,8 +1,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h> /* 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;
}

15
io.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef CUI_H
#define CUI_H
#include <stdio.h> /* 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

View File

@ -3,11 +3,6 @@
#include <stdint.h>
/* 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

View File

@ -1,7 +1,18 @@
#include <stdbool.h>
#include <stdint.h>
#include <apple2_filetype.h>
#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)
{

View File

@ -4,6 +4,7 @@
#include <stdbool.h>
#include <stdint.h>
extern bool getFileInfo(FileName name, struct GetFileInfoParams *params);
extern bool isDirectory(struct GetFileInfoParams *params);
extern const char *getMessage(uint8_t errorCode);