#include #include #include "input.h" #include "trans.h" #define OBUFSIZE 4 #define SBUFSIZE 5 unsigned char obuf[OBUFSIZE]; unsigned char sbuf[SBUFSIZE]; int main(int argc, char *argv[]) { int c, n, dec, hlp; char *prog = *argv; FILE *fp; dec = hlp = 0; while (--argc > 0 && (*++argv)[0] == '-') while ((c = *++argv[0])) switch (c) { case 'd': dec = 1; break; case 'h': hlp = 1; break; default: fprintf(stderr, "%s: illegal option %c\n", prog, c); exit(EXIT_FAILURE); break; } if (hlp) { fprintf(stdout, "Usage: %s -d -h file\n", prog); } else if (argc != 1) { if (dec) { while ((n = getsxts(stdin, sbuf, SBUFSIZE))) { n = decode(sbuf, n, obuf); fwrite(obuf, sizeof(*obuf), n, stdout); } } else { while ((n = getocts(stdin, obuf, OBUFSIZE))) { encode(obuf, n, sbuf); printf("%s", sbuf); } } } else { if ((fp = fopen(*argv, "r")) == NULL) { fprintf(stderr, "%s: can't open %s\n", prog, *argv); exit(EXIT_FAILURE); } else if (dec) { while ((n = getsxts(fp, sbuf, SBUFSIZE))) { n = decode(sbuf, n, obuf); fwrite(obuf, sizeof(*obuf), n, stdout); } } else { while ((n = getocts(fp, obuf, OBUFSIZE))) { encode(obuf, n, sbuf); printf("%s", sbuf); } } fclose(fp); } exit(EXIT_SUCCESS); }