diff options
Diffstat (limited to 'src/alf.c')
| -rw-r--r-- | src/alf.c | 48 |
1 files changed, 35 insertions, 13 deletions
@@ -1,17 +1,13 @@ -/* eventual options: - -a append - -o overwrite (instead of backup~) - -v verbose - */ - #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <limits.h> +#include <sys/stat.h> +#include <time.h> -#define self "unalf" +#include "self.h" #ifndef u8 #define u8 unsigned char @@ -35,6 +31,8 @@ int input_len, output_len, out_bitpos; int opt_append = 0; int opt_overwrite = 0; +struct stat in_file_stat; + typedef struct s_token { u8 *start; u16 length; @@ -127,6 +125,22 @@ void atarify_filename(char *result) { *p = toupper(*p); } +/* see Arcinfo for the gory details. */ +unsigned long get_msdos_date_time(void) { + time_t t = in_file_stat.st_mtim.tv_sec; + struct tm *tm; + int msdos_year; + u16 ms_date, ms_time; + + tm = gmtime(&t); + + msdos_year = tm->tm_year + 1900 - 1980; + + ms_date = tm->tm_mday | (tm->tm_mon << 5) | (msdos_year << 9); + ms_time = (tm->tm_min << 5) | (tm->tm_hour << 11); + return ms_date | (ms_time << 16); +} + void create_header(void) { char hdr_filename[13]; @@ -137,7 +151,7 @@ void create_header(void) { strncat((char *)&output_buf[2], hdr_filename, 13); output_buf[14] = 0x00; store_quad(15, 0); /* compressed size, fill in later */ - store_quad(19, 0); /* date/time (TODO: real timestamp) */ + store_quad(19, get_msdos_date_time()); store_cksum(); store_quad(25, input_len); output_len = 29; @@ -243,6 +257,7 @@ void crunch_file(const char *filename) { /* read in entire input, couldn't do it this way on the Atari */ input_len = fread(input_buf, 1, MAX_INPUT_SIZE - 1, in_file); output_len = 0; + fstat(fileno(in_file), &in_file_stat); /* for timestamp */ fclose(in_file); memset(output_buf, 0, sizeof(output_buf)); @@ -262,15 +277,22 @@ void make_backup(void) { rename(out_filename, bak); } +void usage(void) { + puts("alf (ALF compressor) v" VERSION " by B. Watson, WTFPL."); + printf("Usage: %s [-a|-o] archive.alf file [file ...]\n", self); + puts("Options:"); + puts(" -a: append to archive"); + puts(" -o: overwrite archive (don't make backup with ~)"); + exit(0); +} + int main(int argc, char **argv) { int opt; + set_self(argv[0]); + if(argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) { - fprintf(stderr, "Usage: %s [-a|-o] archive.alf file [file ...]\n", self); - fprintf(stderr, "Options:\n"); - fprintf(stderr, " -a: append to archive\n"); - fprintf(stderr, " -o: overwrite archive (don't make backup with ~)\n"); - exit(1); + usage(); } while((opt = getopt(argc, argv, "ao")) != -1) { |
