coff32xgetidx.c - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
       coff32xgetidx.c (1132B)
       ---
            1 #include <stdio.h>
            2 #include <stdlib.h>
            3 #include <string.h>
            4 
            5 #include <scc/mach.h>
            6 #include <scc/cstd.h>
            7 #include <scc/coff32.h>
            8 
            9 #include "../libmach.h"
           10 #include "fun.h"
           11 
           12 int
           13 coff32xgetidx(int order, long *nsyms, char ***namep, long **offsp, FILE *fp)
           14 {
           15         long i, n;
           16         long *offs;
           17         char **names;
           18         unsigned char buf[EXTIDENTSIZ+1];
           19 
           20         if (fread(buf, 4, 1, fp) != 1)
           21                 return -1;
           22         unpack(order, buf, "l", &n);
           23 
           24         if (n <= 0)
           25                 return -1;
           26 
           27         if ((names = calloc(sizeof(char *), n)) == NULL)
           28                 return -1;
           29 
           30         if ((offs = calloc(sizeof(long), n)) == NULL)
           31                 goto err1;
           32 
           33         for (i = 0; i < n; i++) {
           34                 fread(buf, 4, 1, fp);
           35                 unpack(order, buf, "l", offs[i]);
           36         }
           37 
           38         for (i = 0; i < n; i++) {
           39                 int j, c;
           40                 char *s;
           41 
           42                 for (j = 0; j < EXTIDENTSIZ; j++) {
           43                         if ((c = getc(fp)) == EOF || c == '\0')
           44                                 break;
           45                         buf[j] = c;
           46                 }
           47                 if (c != '\0')
           48                         goto err2;
           49                 buf[j] = '\0';
           50 
           51                 if ((s = malloc(j)) == NULL)
           52                         goto err2;
           53                 memcpy(s, buf, j);
           54                 names[i]= s;
           55         }
           56 
           57         if (ferror(fp))
           58                 goto err2;
           59 
           60         *offsp = offs;
           61         *namep = names;
           62         *nsyms = n;
           63 
           64         return 0;
           65 
           66 err2:
           67         free(offs);
           68 err1:
           69         for (i = 0; i < n; i++)
           70                 free(names[i]);
           71         free(*names);
           72         return -1;
           73 }