newobj.c (566B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <errno.h> 5 6 #include <scc/mach.h> 7 8 #include "libmach.h" 9 10 #include "elf/fun.h" 11 #include "coff32/fun.h" 12 13 static int (*ops[NFORMATS])(Obj *, int) = { 14 [COFF32] = coff32new, 15 [ELF] = elfnew, 16 }; 17 18 Obj * 19 newobj(int type) 20 { 21 Obj *obj; 22 int fmt; 23 24 fmt = FORMAT(type); 25 if (fmt >= NFORMATS) { 26 errno = ERANGE; 27 return NULL; 28 } 29 30 if ((obj = malloc(sizeof(*obj))) == NULL) 31 return NULL; 32 33 obj->type = type; 34 obj->next = NULL; 35 if ((*ops[fmt])(obj, type) < 0) { 36 free(obj); 37 return NULL; 38 } 39 40 return obj; 41 }