moved to stdint types
[libha.git] / src / main.c
blob9badbd6d89e4b39d8b473127cddc251e6cbc061e
1 #include <fcntl.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
11 #include "libha.h"
14 static int fdi = -1;
15 static int fdo = -1;
18 static void error_mem_cb (const char *where) {
19 asc_cleanup();
20 if (fdi >= 0) close(fdi);
21 if (fdo >= 0) close(fdo);
22 fprintf(stderr, "FATAL: out of memory in %s!\n", where);
23 exit(1);
27 static int getbyte_cb (void) {
28 uint8_t b;
29 if (read(fdi, &b, 1) == 1) return b;
30 return -1;
34 static void putbyte_cb (int c) {
35 uint8_t b = c&0xff;
36 write(fdo, &b, 1);
40 static void flush_cb (void) {
44 int main (int argc, char *argv[]) {
45 int dopack;
46 if (argc != 4) {
47 fprintf(stderr, "usage: %s <e|d> infile outfile\n", argv[0]);
48 return 1;
50 if (strcmp(argv[1], "e") == 0 || strcmp(argv[1], "c") == 0) dopack = 1;
51 else if (strcmp(argv[1], "d") == 0 || strcmp(argv[1], "x") == 0) dopack = 0;
52 else {
53 fprintf(stderr, "FATAL: unknown mode: '%s'\n", argv[1]);
54 return 1;
56 fdi = open(argv[2], O_RDONLY|O_CLOEXEC);
57 if (fdi < 0) {
58 fprintf(stderr, "FATAL: can't open file: '%s'\n", argv[2]);
59 return 1;
61 fdo = open(argv[3], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0640);
62 if (fdo < 0) {
63 fprintf(stderr, "FATAL: can't create file: '%s'\n", argv[3]);
64 return 1;
66 error_mem = error_mem_cb;
67 getbyte = getbyte_cb;
68 putbyte = putbyte_cb;
69 flush = flush_cb;
70 if (dopack) {
71 asc_pack();
72 } else {
73 asc_unpack();
75 //asc_cleanup();
76 close(fdi);
77 close(fdo);
78 return 0;