amiga-tz/utility/zic_amiga.c

108 lines
2.3 KiB
C

#include "private.h"
#include "tzversion.h"
const char *vers = "\0$VER: zic" AMIGA_VERSION;
// Return 1 if NAME is a directory, 0 if it's something else, -1 if trouble.
int itsdir(const char *name)
{
BPTR lock;
struct FileInfoBlock *fib;
int result = -1;
if ((lock = Lock(name, SHARED_LOCK))) {
if ((fib = AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR | MEMF_ANY))) {
if ((Examine(lock, fib)) == DOSTRUE) {
if (fib->fib_DirEntryType > 0) {
result = 1;
} else {
result = 0;
}
}
FreeMem(fib, sizeof(struct FileInfoBlock));
}
UnLock(lock);
}
return result;
}
bool mkdirs(char *argname)
{
register char *name;
register char *cp;
BPTR lock;
if (argname == NULL || *argname == '\0')
return true;
cp = name = strdup(argname);
// Skip Amiga DOS drives
if (!(cp = strchr(++cp, ':'))) {
cp = name;
}
while ((cp = strchr(cp + 1, '/')) != 0) {
*cp = '\0';
//printf("mkdir %s\n", name); // DEBUG INFO
if ((lock = CreateDir(name))) {
UnLock(lock);
} else {
if (itsdir(name) <= 0) {
fprintf(stderr, "Can't create directory %s", name);
free(name);
return false;
}
}
*cp = '/';
}
free(name);
return true;
}
void copyfile(char *fromname, char *toname)
{
const int bufsize = 512;
STRPTR buffer;
LONG s;
BPTR fromfile, tofile;
buffer = (STRPTR)AllocMem(bufsize, MEMF_PUBLIC);
if (!buffer) {
return;
}
fromfile = Open((STRPTR)fromname, MODE_OLDFILE);
if (!fromfile) {
fprintf(stderr, "Can't read %s\n", fromname);
FreeMem(buffer, bufsize);
exit(EXIT_FAILURE);
}
tofile = Open((STRPTR)toname, MODE_NEWFILE);
if (!tofile) {
Close(fromfile);
FreeMem(buffer, bufsize);
fprintf(stderr, "Can't create %s\n", toname);
exit(EXIT_FAILURE);
}
do {
Flush(fromfile);
if (
(s = Read(fromfile, buffer, bufsize)) == -1 || Write(tofile, buffer, s)
!= s)
{
break;
}
} while (s > 0);
FreeMem(buffer, bufsize);
Close(tofile);
Close(fromfile);
}