ListRez -- use err() functions.

This commit is contained in:
Kelvin Sherlock 2016-07-25 22:12:03 -04:00
parent 8da18b5db3
commit fa9ce427e2
1 changed files with 23 additions and 30 deletions

View File

@ -31,8 +31,9 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <sysexits.h>
#include <err.h>
#include <sysexits.h>
#include <stdint.h> #include <stdint.h>
#include <tomcrypt.h> #include <tomcrypt.h>
@ -106,7 +107,7 @@ const char *unhash(const unsigned char *hash) {
return buffer; return buffer;
} }
const char *md5(int fd, uint32_t length) { const char *md5(int fd, uint32_t length, const char *file) {
hash_state md; hash_state md;
static uint8_t buffer[4096]; static uint8_t buffer[4096];
uint8_t hash[16]; uint8_t hash[16];
@ -122,7 +123,7 @@ const char *md5(int fd, uint32_t length) {
if (l < 0) { if (l < 0) {
if (errno == EINTR) continue; if (errno == EINTR) continue;
return NULL; err(EX_IOERR, "Error reading file: %s", file);
} }
if (l == 0) break; if (l == 0) break;
@ -130,11 +131,14 @@ const char *md5(int fd, uint32_t length) {
length -= l; length -= l;
} }
if (length) return NULL; if (length) {
errx(EX_DATAERR, "Not a Macintosh resource fork: %s", file);
}
md5_done(&md, hash); md5_done(&md, hash);
return unhash(hash); return unhash(hash);
} }
const char *TypeCode(uint8_t code[4]) const char *TypeCode(uint8_t code[4])
{ {
static char buffer[12]; static char buffer[12];
@ -172,64 +176,60 @@ int onefile(const char *file)
fd = open(file, O_RDONLY | O_BINARY | O_RSRC); fd = open(file, O_RDONLY | O_BINARY | O_RSRC);
if (fd < 0) if (fd < 0)
{ {
fprintf(stderr, "# %s: Error opening file: %s\n", file, strerror(errno)); err(EX_NOINPUT, "Errror opening file: %s", file);
return -1;
} }
size = read(fd, &header, sizeof(header)); size = read(fd, &header, sizeof(header));
if (size < 0) if (size < 0)
{ {
fprintf(stderr, "# %s Error reading file: %s\n", file, strerror(errno)); int e = errno;
close(fd); close(fd);
return -1; errc(EX_IOERR, e, "Error reading file: %s", file);
} }
if (size < sizeof(header)) if (size < sizeof(header))
{ {
fprintf(stderr, "# %s: Not a macintosh resource fork.\n", file);
close(fd); close(fd);
return -1; errx(EX_DATAERR, "Not a Macintosh resource fork: %s", file);
} }
// a couple sanity checks // a couple sanity checks
if (header.length_rmap == 0 || header.offset_rmap < sizeof(header)) if (header.length_rmap == 0 || header.offset_rmap < sizeof(header))
{ {
fprintf(stderr, "# %s: Not a macintosh resource fork.\n", file);
close(fd); close(fd);
errx(EX_DATAERR, "Not a Macintosh resource fork: %s", file);
} }
mapdata = malloc(header.length_rmap); mapdata = malloc(header.length_rmap);
if (!mapdata) if (!mapdata)
{ {
fprintf(stderr, "# %s Memory allocation failure: %s\n", file, strerror(errno));
close(fd); close(fd);
return -1; errx(EX_OSERR, NULL);
} }
off = lseek(fd, header.offset_rmap, SEEK_SET); off = lseek(fd, header.offset_rmap, SEEK_SET);
if (off < 0) if (off < 0)
{ {
fprintf(stderr, "# %s: Not a macintosh resource fork.\n", file);
close(fd); close(fd);
free(mapdata); free(mapdata);
return -1; errx(EX_DATAERR, "Not a Macintosh resource fork: %s", file);
} }
size = read(fd, mapdata, header.length_rmap); size = read(fd, mapdata, header.length_rmap);
if (size < 0) if (size < 0)
{ {
fprintf(stderr, "# %s Error reading file: %s\n", file, strerror(errno)); int e = errno;
close(fd); close(fd);
free(mapdata); free(mapdata);
return -1; errc(EX_IOERR, e, "Error reading file: %s", file);
} }
if (size != header.length_rmap) if (size != header.length_rmap)
{ {
fprintf(stderr, "# %s: Not a macintosh resource fork.\n", file);
close(fd); close(fd);
free(mapdata); free(mapdata);
return -1; errx(EX_DATAERR, "Not a Macintosh resource fork: %s", file);
} }
memcpy(&map, mapdata, sizeof(map)); memcpy(&map, mapdata, sizeof(map));
@ -292,10 +292,9 @@ int onefile(const char *file)
off = lseek(fd, header.offset_rdata + rOff, SEEK_SET); off = lseek(fd, header.offset_rdata + rOff, SEEK_SET);
if (off < 0) if (off < 0)
{ {
fprintf(stderr, "# %s: Not a macintosh resource fork.\n", file);
close(fd); close(fd);
free(mapdata); free(mapdata);
return -1; errx(EX_DATAERR, "Not a Macintosh resource fork: %s", file);
} }
size = read(fd, &rSize, sizeof(rSize)); size = read(fd, &rSize, sizeof(rSize));
@ -317,13 +316,7 @@ int onefile(const char *file)
if (mflag) { if (mflag) {
const char * hash = md5(fd, rSize); const char * hash = md5(fd, rSize, file);
if (!hash) {
fprintf(stderr, "# %s: Not a macintosh resource fork.\n", file);
close(fd);
free(mapdata);
return -1;
}
printf("%-10s $%04x $%06x $%04x %-16s %s\n", printf("%-10s $%04x $%06x $%04x %-16s %s\n",
tc, tc,
@ -354,7 +347,7 @@ int onefile(const char *file)
void help(int status) void help(int status)
{ {
printf("# Usage: ListRez [-mh] file\n"); printf("# Usage: ListRez [-h] [-m] file\n");
exit(status); exit(status);
} }
@ -383,7 +376,7 @@ int main(int argc, char **argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
// options for resID, resType, list only // options for resID, resType, list only?
if (argc != 1) if (argc != 1)
help(EX_USAGE); help(EX_USAGE);