scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

coff32getidx.c (967B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 #include <scc/mach.h>
      6 #include <scc/cstd.h>
      7 
      8 #include "../libmach.h"
      9 #include "coff32.h"
     10 
     11 int
     12 coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp)
     13 {
     14 	int j, c;
     15 	long i, n;
     16 	char *s;
     17 	Objsymdef *bp;
     18 	unsigned char buf[EXTIDENTSIZ];
     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 ((bp = calloc(sizeof(*bp), n)) == NULL)
     28 		return -1;
     29 
     30 	for (i = 1; i < n-1; i++)
     31 		bp[i].next = &bp[i-1];
     32 
     33 	for (i = 0; i < n; i++) {
     34 		fread(buf, 4, 1, fp);
     35 		unpack(order, buf, "l", &bp[i].offset);
     36 	}
     37 
     38 	for (i = 0; i < n; i++) {
     39 		for (j = 0; (c = getc(fp)) != EOF && c != '\0'; j++)
     40 			buf[j] = c;
     41 		buf[j++] = '\0';
     42 
     43 		if ((s = malloc(j)) == NULL)
     44 			goto error;
     45 		memcpy(s, buf, j);
     46 		bp[i].name = s;
     47 	}
     48 
     49 	if (ferror(fp))
     50 		goto error;
     51 
     52 	*nsyms = n;
     53 	*def = bp;
     54 
     55 	return 0;
     56 
     57 error:
     58 	for (i = 0; i < n; i++)
     59 		free(bp[i].name);
     60 	free(bp);
     61 	return -1;
     62 }