VirtualBox

Changeset 14047

Show
Ignore:
Timestamp:
11/10/08 23:42:14 (2 months ago)
Author:
vboxsync
Message:

biossums: fixed warning (64-bit MSC), return 1 not -1 on failure, delete output on failure, display the program name in the error message.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/bldprogs/biossums.c

    r13438 r14047  
    2323#include <stdio.h> 
    2424#include <string.h> 
     25#include <stdarg.h> 
    2526#include <errno.h> 
    2627 
     
    2829 
    2930static uint8_t abBios[64*1024]; 
     31static FILE *g_pIn = NULL; 
     32static FILE *g_pOut = NULL; 
     33static const char *g_pszOutFile = NULL; 
     34static const char *g_argv0; 
     35 
     36/** 
     37 * Find where the filename starts in the given path. 
     38 */ 
     39static const char *name(const char *pszPath) 
     40{ 
     41    const char *psz = strrchr(pszPath, '/'); 
     42#if defined(_MSC_VER) || defined(__OS2__) 
     43    const char *psz2 = strrchr(pszPath, '\\'); 
     44    if (!psz2) 
     45        psz2 = strrchr(pszPath, ':'); 
     46    if (psz2 && (!psz || psz2 > psz)) 
     47        psz = psz2; 
     48#endif 
     49    return psz ? psz + 1 : pszPath; 
     50} 
     51 
     52/** 
     53 * Report an error. 
     54 */ 
     55static int fatal(const char *pszFormat, ...) 
     56{ 
     57    va_list va; 
     58 
     59    fprintf(stderr, "%s: ", name(g_argv0)); 
     60 
     61    va_start(va, pszFormat); 
     62    vfprintf(stderr, pszFormat, va); 
     63    va_end(va); 
     64 
     65    /* clean up */ 
     66    if (g_pIn) 
     67        fclose(g_pIn); 
     68    if (g_pOut) 
     69        fclose(g_pOut); 
     70    if (g_pszOutFile) 
     71        unlink(g_pszOutFile); 
     72 
     73    return 1; 
     74} 
    3075 
    3176/** 
    3277 * Calculate the checksum. 
    3378 */ 
    34 static uint8_t calculateChecksum(uint8_t *pb, size_t cb, unsigned int iChecksum) 
    35 { 
    36     uint8_t       u8Sum = 0; 
    37     unsigned int  i; 
     79static uint8_t calculateChecksum(uint8_t *pb, size_t cb, size_t iChecksum) 
     80{ 
     81    uint8_t u8Sum = 0; 
     82    size_t  i; 
    3883 
    3984    for (i = 0; i < cb; i++) 
     
    4590 
    4691/** 
     92 * Find a header in the binary. 
     93 * 
    4794 * @param   pb        Where to search for the signature 
    4895 * @param   cb        Size of the search area 
     
    73120    int     fAdapterBios = 0; 
    74121 
     122    g_argv0 = argv[0]; 
     123 
    75124    if (argc != 3) 
    76     { 
    77         printf("Input file name and output file name required.\n"); 
    78         exit(-1); 
    79     } 
    80  
    81     pIn = fopen(argv[1], "rb"); 
     125        return fatal("Input file name and output file name required.\n"); 
     126 
     127    pIn = g_pIn = fopen(argv[1], "rb"); 
    82128    if (!pIn) 
    83     { 
    84         printf("Error opening '%s' for reading (%s).\n", argv[1], strerror(errno)); 
    85         exit(-1); 
    86     } 
    87      
    88     pOut = fopen(argv[2], "wb"); 
     129        return fatal("Error opening '%s' for reading (%s).\n", argv[1], strerror(errno)); 
     130 
     131    pOut = g_pOut = fopen(argv[2], "wb"); 
    89132    if (!pOut) 
    90     { 
    91         printf("Error opening '%s' for writing (%s).\n", argv[2], strerror(errno)); 
    92         exit(-1); 
    93     } 
    94  
    95     /* safety precaution */ 
     133        return fatal("Error opening '%s' for writing (%s).\n", argv[2], strerror(errno)); 
     134    g_pszOutFile = argv[2]; 
     135 
     136    /* safety precaution (aka. complete paranoia :-) */ 
    96137    memset(abBios, 0, sizeof(abBios)); 
    97138 
    98139    cbIn = fread(abBios, 1, sizeof(abBios), pIn); 
    99140    if (ferror(pIn)) 
    100     { 
    101         printf("Error reading from '%s' (%s).\n", argv[1], strerror(errno)); 
    102         fclose(pIn); 
    103         exit(-1); 
    104     } 
     141        return fatal("Error reading from '%s' (%s).\n", argv[1], strerror(errno)); 
     142    g_pIn = NULL; 
    105143    fclose(pIn); 
    106144 
     
    112150 
    113151    if (!fAdapterBios && cbIn != 64*1024) 
    114     { 
    115         printf("Size of system BIOS is not 64KB!\n"); 
    116         fclose(pOut); 
    117         exit(-1); 
    118     } 
     152        return fatal("Size of system BIOS is not 64KB!\n"); 
    119153 
    120154    if (fAdapterBios) 
    121155    { 
    122156        /* adapter BIOS */ 
    123          
     157 
    124158        /* set the length indicator */ 
    125159        abBios[2] = (uint8_t)(cbIn / 512); 
     
    136170        { 
    137171            case 0: 
    138                 printf("No BIOS32 header not found!\n"); 
    139                 exit(-1); 
     172                return fatal("No BIOS32 header not found!\n"); 
    140173            case 2: 
    141                 printf("More than one BIOS32 header found!\n"); 
    142                 exit(-1); 
     174                return fatal("More than one BIOS32 header found!\n"); 
    143175            case 1: 
    144176                cbChecksum = (size_t)pbHeader[9] * 16; 
     
    153185        { 
    154186            case 0: 
    155                 printf("No PCI IRQ routing table found!\n"); 
    156                 exit(-1); 
     187                return fatal("No PCI IRQ routing table found!\n"); 
    157188            case 2: 
    158                 printf("More than one PCI IRQ routing table found!\n"); 
    159                 exit(-1); 
     189                return fatal("More than one PCI IRQ routing table found!\n"); 
    160190            case 1: 
    161191                cbChecksum = (size_t)pbHeader[6] + (size_t)pbHeader[7] * 256; 
     
    170200        { 
    171201            case 0: 
    172                 printf("No SMBIOS header found!\n"); 
    173                 exit(-1); 
     202                return fatal("No SMBIOS header found!\n"); 
    174203            case 2: 
    175                 printf("More than one SMBIOS header found!\n"); 
    176                 exit(-1); 
     204                return fatal("More than one SMBIOS header found!\n"); 
    177205            case 1: 
    178206                /* at first fix the DMI header starting at SMBIOS header offset 16 */ 
     
    193221    cbOut = fwrite(abBios, 1, cbIn, pOut); 
    194222    if (ferror(pOut)) 
    195     { 
    196         printf("Error writing to '%s' (%s).\n", argv[2], strerror(errno)); 
    197         fclose(pOut); 
    198         exit(-1); 
    199     } 
    200  
    201     fclose(pOut); 
     223        return fatal("Error writing to '%s' (%s).\n", g_pszOutFile, strerror(errno)); 
     224    g_pOut = NULL; 
     225    if (fclose(pOut)) 
     226        return fatal("Error closing '%s' (%s).\n", g_pszOutFile, strerror(errno)); 
    202227 
    203228    return 0; 
    204229} 
     230 

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy