OSDN Git Service

Re-autconf configure script after Mike Frysinger's recent AC_ERROR
[uclinux-h8/elf2flt.git] / flthdr.c
index 6c005aa..967d640 100644 (file)
--- a/flthdr.c
+++ b/flthdr.c
@@ -2,26 +2,45 @@
 /*
  *     A simple program to manipulate flat files
  *
- *     Copyright (C) 2001 SnapGear Inc, davidm@snapgear.com
+ *     Copyright (C) 2001-2003 SnapGear Inc, davidm@snapgear.com
  *     Copyright (C) 2001 Lineo, davidm@lineo.com
+ *
+ * This is Free Software, under the GNU Public Licence v2 or greater.
+ *
  */
 /****************************************************************************/
 
 #include <stdio.h>    /* Userland pieces of the ANSI C standard I/O package  */
 #include <unistd.h>   /* Userland prototypes of the Unix std system calls    */
+#include <time.h>
+#include <stdlib.h>   /* exit() */
+#include <string.h>   /* strcat(), strcpy() */
 
 /* 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
 
 /* from uClinux-x.x.x/include/linux */
 #include "flat.h"     /* Binary flat header description                      */
 
+#if defined(__MINGW32__)
+#include <getopt.h>
+
+#define mkstemp(p) mktemp(p)
+
+#endif
+
 /****************************************************************************/
 
 char *program_name;
 
 static char cmd[1024];
-static int print = 0, compress = 0, ramload = 0, stacksize = 0;
+static int print = 0, compress = 0, ramload = 0, stacksize = 0, ktrace = 0;
 static int short_format = 0;
 
 /****************************************************************************/
@@ -59,13 +78,14 @@ process_file(char *ifile, char *ofile)
 {
        int old_flags, old_stack, new_flags, new_stack;
        FILE *ifp, *ofp;
+       int ofp_is_pipe = 0;
        struct flat_hdr old_hdr, new_hdr;
        char tfile[256];
        char tfile2[256];
 
        *tfile = *tfile2 = '\0';
 
-       if ((ifp = fopen(ifile, "r")) == NULL) {
+       if ((ifp = fopen(ifile, "r" BINARY_FILE_OPTS)) == NULL) {
                fprintf(stderr, "Cannot open %s\n", ifile);
                return;
        }
@@ -98,13 +118,22 @@ process_file(char *ifile, char *ofile)
        else if (ramload < 0)
                new_flags &= ~FLAT_FLAG_RAM;
        
+       if (ktrace > 0)
+               new_flags |= FLAT_FLAG_KTRACE;
+       else if (ktrace < 0)
+               new_flags &= ~FLAT_FLAG_KTRACE;
+       
        if (stacksize)
                new_stack = stacksize;
 
        if (print == 1) {
+               time_t t;
+
                printf("%s\n", ifile);
                printf("    Magic:        %4.4s\n", old_hdr.magic);
                printf("    Rev:          %d\n",    ntohl(old_hdr.rev));
+               t = (time_t) htonl(old_hdr.build_date);
+               printf("    Build Date:   %s",      t?ctime(&t):"not specified\n");
                printf("    Entry:        0x%x\n",  ntohl(old_hdr.entry));
                printf("    Data Start:   0x%x\n",  ntohl(old_hdr.data_start));
                printf("    Data End:     0x%x\n",  ntohl(old_hdr.data_end));
@@ -122,6 +151,8 @@ process_file(char *ifile, char *ofile)
                                printf("Gzip-Compressed ");
                        if (old_flags & FLAT_FLAG_GZDATA)
                                printf("Gzip-Data-Compressed ");
+                       if (old_flags & FLAT_FLAG_KTRACE)
+                               printf("Kernel-Traced-Load ");
                        printf(")\n");
                }
        } else if (print > 1) {
@@ -134,6 +165,7 @@ process_file(char *ifile, char *ofile)
                        first = 0;
                }
                *tfile = '\0';
+               strcat(tfile, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
                strcat(tfile, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
                strcat(tfile, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
                strcat(tfile, (old_flags & FLAT_FLAG_GZIP) ? "z" :
@@ -176,7 +208,7 @@ process_file(char *ifile, char *ofile)
 
        strcpy(tfile, "/tmp/flatXXXXXX");
        mkstemp(tfile);
-       if ((ofp = fopen(tfile, "w")) == NULL) {
+       if ((ofp = fopen(tfile, "w" BINARY_FILE_OPTS)) == NULL) {
                fprintf(stderr, "Failed to open %s for writing\n", tfile);
                unlink(tfile);
                unlink(tfile2);
@@ -201,7 +233,7 @@ process_file(char *ifile, char *ofile)
                mkstemp(tfile2);
                
                if (old_flags & FLAT_FLAG_GZDATA) {
-                       tfp = fopen(tfile2, "w");
+                       tfp = fopen(tfile2, "w" BINARY_FILE_OPTS);
                        if (!tfp) {
                                fprintf(stderr, "Failed to open %s for writing\n", tfile2);
                                exit(1);
@@ -212,12 +244,16 @@ process_file(char *ifile, char *ofile)
                }
 
                sprintf(cmd, "gunzip >> %s", tfile2);
-               tfp = popen(cmd, "w");
+               tfp = popen(cmd, "w" BINARY_FILE_OPTS);
+               if(!tfp) {
+                       perror("popen");
+                       exit(1);
+               }
                transferr(ifp, tfp, -1);
-               fclose(tfp);
+               pclose(tfp);
 
                fclose(ifp);
-               ifp = fopen(tfile2, "r");
+               ifp = fopen(tfile2, "r" BINARY_FILE_OPTS);
                if (!ifp) {
                        fprintf(stderr, "Failed to open %s for reading\n", tfile2);
                        unlink(tfile);
@@ -230,14 +266,16 @@ process_file(char *ifile, char *ofile)
                printf("zflat %s --> %s\n", ifile, ofile);
                fclose(ofp);
                sprintf(cmd, "gzip -9 -f >> %s", tfile);
-               ofp = popen(cmd, "w");
+               ofp = popen(cmd, "w" BINARY_FILE_OPTS);
+               ofp_is_pipe = 1;
        } else if (new_flags & FLAT_FLAG_GZDATA) {
                printf("zflat-data %s --> %s\n", ifile, ofile);
                transferr(ifp, ofp, ntohl(new_hdr.data_start) -
                                sizeof(struct flat_hdr));
                fclose(ofp);
                sprintf(cmd, "gzip -9 -f >> %s", tfile);
-               ofp = popen(cmd, "w");
+               ofp = popen(cmd, "w" BINARY_FILE_OPTS);
+               ofp_is_pipe = 1;
        }
 
        if (!ofp) { /* can only happen if using gzip/gunzip */
@@ -258,7 +296,10 @@ process_file(char *ifile, char *ofile)
        }
 
        fclose(ifp);
-       fclose(ofp);
+       if (ofp_is_pipe)
+               pclose(ofp);
+       else
+               fclose(ofp);
 
        /* cheat a little here to preserve file permissions */
        sprintf(cmd, "cp %s %s", tfile, ofile);
@@ -282,6 +323,8 @@ usage(char *s)
        fprintf(stderr, "       -Z      : un-compressed flat file\n");
        fprintf(stderr, "       -r      : ram load\n");
        fprintf(stderr, "       -R      : do not RAM load\n");
+       fprintf(stderr, "       -k      : kernel traced load (for debug)\n");
+       fprintf(stderr, "       -K      : normal non-kernel traced load\n");
        fprintf(stderr, "       -s size : stack size\n");
        fprintf(stderr, "       -o file : output-file\n"
                        "                 (default is to modify input file)\n");
@@ -298,7 +341,7 @@ main(int argc, char *argv[])
 
        program_name = argv[0];
 
-       while ((c = getopt(argc, argv, "pdzZrRs:o:")) != EOF) {
+       while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
                switch (c) {
                case 'p': print = 1;                break;
                case 'z': compress = 1;             break;
@@ -306,6 +349,8 @@ main(int argc, char *argv[])
                case 'Z': compress = -1;            break;
                case 'r': ramload = 1;              break;
                case 'R': ramload = -1;             break;
+               case 'k': ktrace = 1;               break;
+               case 'K': ktrace = -1;              break;
                case 's': stacksize = atoi(optarg); break;
                case 'o': ofile = optarg;           break;
                default: