Selaa lähdekoodia

Allow - as a filename to mean stdin/stdout

Richard Laager 13 vuotta sitten
vanhempi
commit
61ee500bb6
2 muutettua tiedostoa jossa 13 lisäystä ja 3 poistoa
  1. 8 2
      src/docsis.c
  2. 5 1
      src/docsis_yy.y

+ 8 - 2
src/docsis.c

@@ -286,7 +286,8 @@ int encode_one_file ( char *input_file, char *output_file,
   unsigned char *buffer;
   FILE *of;
 
-  if (!strcmp (input_file, output_file))
+  /* It's not an error to specify the input and output as "-". */
+  if (!strcmp (input_file, output_file) && strcmp (input_file, "-"))
   {
 	fprintf(stderr, "docsis: Error: source file is the same as destination file\n");
 	return -1;
@@ -329,12 +330,17 @@ int encode_one_file ( char *input_file, char *output_file,
   fprintf (stderr, "Final content of config file:\n");
 
   decode_main_aggregate (buffer, buflen);
-  if ((of = fopen (output_file, "wb")) == NULL)
+  if (!strcmp (output_file, "-"))
+    {
+      of = stdout;
+    }
+  else if ((of = fopen (output_file, "wb")) == NULL)
     {
       fprintf (stderr, "docsis: error: can't open output file %s\n", output_file);
       return -2;
     }
   fwrite (buffer, sizeof (unsigned char), buflen, of);
+  fclose (of);
   free(buffer);
   return 0;
 

+ 5 - 1
src/docsis_yy.y

@@ -620,7 +620,11 @@ int parse_config_file ( char *file, struct tlv **parse_tree_result )
   FILE *cf;
   int rval;
 
-  if ( (cf = fopen ( file, "r" ))== NULL )
+  if ( !strcmp(file, "-") )
+  {
+	cf = stdin;
+  }
+  else if ( (cf = fopen ( file, "r" )) == NULL )
   {
 	fprintf (stderr, "docsis: Can't open input file %s\n", file );
 	return -1;