Added getopt to undpu and -c arguement for output to stdout

pull/13/head
silverwizard 5 years ago
parent e448ad953e
commit 4b1f3df74e
  1. 34
      undpu.c

@ -2,23 +2,39 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <getopt.h>
const char compare_string[] = "DOUBLEPLUS UNZIP, FACTOR="; const char compare_string[] = "DOUBLEPLUS UNZIP, FACTOR=";
int main (int argc, char **argv){ int main (int argc, char **argv){
FILE *bob, *joe; FILE *bob, *joe;
int factor, thing, i; int modifier, factor, thing, i;
int out = 0;
char output[FILENAME_MAX]; char output[FILENAME_MAX];
if(argc < 2){ while((modifier=getopt(argc,argv,"c"))+1){
switch(modifier){
case 'c':
joe=stdout;
out = 1;
break;
default:
fprintf(stderr,"modifier=%d\n",modifier);
fputs("You're going to have to be clearer about what you want.\n",stderr);
return EXIT_FAILURE;
}
}
argc-=optind;
argv+=optind;
if(argc < 1){
fputs("I pity the fool who don't have an input file!\n",stderr); fputs("I pity the fool who don't have an input file!\n",stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(strlen(argv[1]) <= 4 || strcmp(argv[1] + strlen(argv[1]) - 4, ".dpu")) if(strlen(argv[0]) <= 4 || strcmp(argv[0] + strlen(argv[0]) - 4, ".dpu"))
{ {
fputs("Where's the rest of the filename?\n", stderr); fputs("Where's the rest of the filename?\n", stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if((bob = fopen(argv[1],"r"))==NULL){ if((bob = fopen(argv[0],"r"))==NULL){
fputs("That's no input file, THAT'S A SPACE STATION!\n",stderr); fputs("That's no input file, THAT'S A SPACE STATION!\n",stderr);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -41,10 +57,12 @@ int main (int argc, char **argv){
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
strncpy(output,argv[1],strlen(argv[1])-4); if(!out){
if((joe = fopen(output,"w"))==NULL){ strncpy(output,argv[0],strlen(argv[0])-4);
fputs("Ummm, the output file doesn't work.\n",stderr); if((joe = fopen(output,"w"))==NULL){
return EXIT_FAILURE; fputs("Ummm, the output file doesn't work.\n",stderr);
return EXIT_FAILURE;
}
} }
while((thing=fgetc(bob))!=EOF){ while((thing=fgetc(bob))!=EOF){
fputc(thing,joe); fputc(thing,joe);

Loading…
Cancel
Save