OSDN Git Service

The current code misses checking a few args in order to determine the
[uclinux-h8/elf2flt.git] / flthdr.c
index 90f9aa3..dfc9107 100644 (file)
--- a/flthdr.c
+++ b/flthdr.c
 #include <inttypes.h>
 #include <assert.h>
 
-/* macros for conversion between host and (internet) network byte order */
-#ifndef WIN32
-#include <netinet/in.h> /* Consts and structs defined by the internet system */
-#define        BINARY_FILE_OPTS
-#else
-#include <winsock2.h>
-#define        BINARY_FILE_OPTS "b"
-#endif
-
 #include "compress.h"
 #include <libiberty.h>
 
+#include "stubs.h"
+const char *elf2flt_progname;
+
 /* from uClinux-x.x.x/include/linux */
 #include "flat.h"     /* Binary flat header description                      */
 
@@ -48,8 +42,6 @@
 
 /****************************************************************************/
 
-char *program_name;
-
 static int print = 0, print_relocs = 0, docompress = 0, ramload = 0,
            stacksize = 0, ktrace = 0, l1stack = 0;
 
@@ -239,16 +231,14 @@ process_file(char *ifile, char *ofile)
        tfile = make_temp_file("flthdr");
 
        if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
-               fprintf(stderr, "Failed to open %s for writing\n", tfile);
                unlink(tfile);
-               exit(1);
+               fatal("Failed to open %s for writing\n", tfile);
        }
 
        /* Copy header (always uncompressed).  */
        if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
-               fprintf(stderr, "Failed to write to  %s\n", tfile);
                unlink(tfile);
-               exit(1);
+               fatal("Failed to write to  %s\n", tfile);
        }
 
        /* Whole input file (including text) is compressed: start decompressing
@@ -283,11 +273,10 @@ process_file(char *ifile, char *ofile)
        output_error = ferror_stream(&ofp);
 
        if (input_error || output_error) {
-               fprintf(stderr, "Error on file pointer%s%s\n",
+               unlink(tfile);
+               fatal("Error on file pointer%s%s\n",
                                input_error ? " input" : "",
                                output_error ? " output" : "");
-               unlink(tfile);
-               exit(1);
        }
 
        fclose_stream(&ifp);
@@ -313,7 +302,7 @@ usage(char *s)
 {
        if (s)
                fprintf(stderr, "%s\n", s);
-       fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
+       fprintf(stderr, "usage: %s [options] flat-file\n", elf2flt_progname);
        fprintf(stderr, "       Allows you to change an existing flat file\n\n");
        fprintf(stderr, "       -p      : print current settings\n");
        fprintf(stderr, "       -P      : print relocations\n");
@@ -337,11 +326,12 @@ usage(char *s)
 int
 main(int argc, char *argv[])
 {
-       int c;
+       int c, noargs;
        char *ofile = NULL, *ifile;
 
-       program_name = argv[0];
+       elf2flt_progname = argv[0];
 
+       noargs = 1;
        while ((c = getopt(argc, argv, "pPdzZrRuUkKs:o:")) != EOF) {
                switch (c) {
                case 'p': print = 1;                break;
@@ -364,6 +354,7 @@ main(int argc, char *argv[])
                        usage("invalid option");
                        break;
                }
+               noargs = 0;
        }
 
        if (optind >= argc)
@@ -372,9 +363,9 @@ main(int argc, char *argv[])
        if (ofile && argc - optind > 1)
                usage("-o can only be used with a single file");
 
-       if (!print && !docompress && !ramload && !stacksize) /* no args == print */
+       if (!print && noargs) /* no args == print */
                print = argc - optind; /* greater than 1 is short format */
-       
+
        for (c = optind; c < argc; c++) {
                ifile = argv[c];
                if (!ofile)