add writing to file output option
This commit is contained in:
parent
848c3749d2
commit
221a8588c0
1 changed files with 16 additions and 21 deletions
37
main.c
37
main.c
|
@ -14,7 +14,7 @@ main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c, n, dec, hlp;
|
int c, n, dec, hlp;
|
||||||
char *prog = *argv;
|
char *prog = *argv;
|
||||||
FILE *fp;
|
FILE *in, *out;
|
||||||
|
|
||||||
dec = hlp = 0;
|
dec = hlp = 0;
|
||||||
while (--argc > 0 && (*++argv)[0] == '-')
|
while (--argc > 0 && (*++argv)[0] == '-')
|
||||||
|
@ -32,36 +32,31 @@ main(int argc, char *argv[])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (hlp) {
|
if (hlp) {
|
||||||
fprintf(stdout, "Usage: %s -d -h file\n", prog);
|
fprintf(stdout, "Usage: %s -d -h infile outfile\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 {
|
} else {
|
||||||
if ((fp = fopen(*argv, "r")) == NULL) {
|
if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) {
|
||||||
fprintf(stderr, "%s: can't open %s\n", prog, *argv);
|
fprintf(stderr, "%s: can't open %s\n", prog, *argv);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
else if (dec) {
|
if (argc == 2 && (out = fopen(*(argv+1), "w")) == NULL) {
|
||||||
while ((n = getsxts(fp, sbuf, SBUFSIZE))) {
|
fprintf(stderr, "%s: can't open %s\n", prog, *(argv+1));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (dec) {
|
||||||
|
while ((n = getsxts((argc >= 1) ? in : stdin, sbuf, SBUFSIZE))) {
|
||||||
n = decode(sbuf, n, obuf);
|
n = decode(sbuf, n, obuf);
|
||||||
fwrite(obuf, sizeof(*obuf), n, stdout);
|
fwrite(obuf, sizeof(*obuf), n, (argc == 2) ? out : stdout);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while ((n = getocts(fp, obuf, OBUFSIZE))) {
|
while ((n = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) {
|
||||||
encode(obuf, n, sbuf);
|
encode(obuf, n, sbuf);
|
||||||
printf("%s", sbuf);
|
fprintf((argc == 2) ? out : stdout, "%s", sbuf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
if (in)
|
||||||
|
fclose(in);
|
||||||
|
if (out)
|
||||||
|
fclose(out);
|
||||||
}
|
}
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue