1 | #ifdef HAVE_CONFIG_H |
---|
2 | #include "singularconfig.h" |
---|
3 | #endif /* HAVE_CONFIG_H */ |
---|
4 | |
---|
5 | #include <kernel/mod2.h> |
---|
6 | |
---|
7 | #include <resources/feFopen.h> |
---|
8 | #include <polys/mod_raw.h> |
---|
9 | |
---|
10 | #include <Singular/mod_lib.h> |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include <ctype.h> |
---|
15 | #include <sys/stat.h> |
---|
16 | #include <errno.h> |
---|
17 | |
---|
18 | |
---|
19 | #define SI_BUILTIN_LIBSTR(name) (char*) #name ".so", |
---|
20 | |
---|
21 | const char * const si_builtin_libs[] = { SI_FOREACH_BUILTIN(SI_BUILTIN_LIBSTR) NULL }; |
---|
22 | |
---|
23 | #undef SI_BUILTIN_LIBSTR |
---|
24 | |
---|
25 | #define BYTES_TO_CHECK 7 |
---|
26 | |
---|
27 | lib_types type_of_LIB(const char *newlib, char *libnamebuf) |
---|
28 | { |
---|
29 | const unsigned char mach_o[]={0xfe,0xed,0xfa,0xce,0}; |
---|
30 | const unsigned char mach_O[]={0xce,0xfa,0xed,0xfe,0}; |
---|
31 | |
---|
32 | const unsigned char mach_o64[]={0xfe,0xed,0xfa,0xcf,0}; |
---|
33 | const unsigned char mach_O64[]={0xcf,0xfa,0xed,0xfe,0}; |
---|
34 | |
---|
35 | const unsigned char mach_FAT[]={0xca,0xfe,0xba,0xbe,0}; |
---|
36 | const unsigned char mach_fat[]={0xbe,0xba,0xfe,0xca,0}; |
---|
37 | |
---|
38 | int i=0; |
---|
39 | while(si_builtin_libs[i]!=NULL) |
---|
40 | { |
---|
41 | if (strcmp(newlib,si_builtin_libs[i])==0) |
---|
42 | { |
---|
43 | if(libnamebuf!=NULL) strcpy(libnamebuf,newlib); |
---|
44 | return LT_BUILTIN; |
---|
45 | } |
---|
46 | i++; |
---|
47 | } |
---|
48 | char buf[BYTES_TO_CHECK+1]; /* one extra for terminating '\0' */ |
---|
49 | struct stat sb; |
---|
50 | int nbytes = 0; |
---|
51 | int ret; |
---|
52 | lib_types LT=LT_NONE; |
---|
53 | |
---|
54 | FILE * fp = feFopen( newlib, "r", libnamebuf, FALSE ); |
---|
55 | |
---|
56 | do |
---|
57 | { |
---|
58 | ret = stat(libnamebuf, &sb); |
---|
59 | } while((ret < 0) and (errno == EINTR)); |
---|
60 | |
---|
61 | if (fp==NULL) |
---|
62 | { |
---|
63 | return LT_NOTFOUND; |
---|
64 | } |
---|
65 | if((sb.st_mode & S_IFMT) != S_IFREG) |
---|
66 | { |
---|
67 | goto lib_type_end; |
---|
68 | } |
---|
69 | if ((nbytes = fread((char *)buf, sizeof(char), BYTES_TO_CHECK, fp)) == -1) |
---|
70 | { |
---|
71 | goto lib_type_end; |
---|
72 | /*NOTREACHED*/ |
---|
73 | } |
---|
74 | if (nbytes == 0) |
---|
75 | goto lib_type_end; |
---|
76 | else |
---|
77 | { |
---|
78 | buf[nbytes++] = '\0'; /* null-terminate it */ |
---|
79 | } |
---|
80 | if( (strncmp(buf, "\177ELF", 4)==0)) /* generic ELF */ |
---|
81 | { |
---|
82 | LT = LT_ELF; |
---|
83 | //omFree(newlib); |
---|
84 | //newlib = omStrDup(libnamebuf); |
---|
85 | goto lib_type_end; |
---|
86 | } |
---|
87 | |
---|
88 | if( (strncmp(buf, (const char *)mach_o, 4)==0) || (strncmp(buf, (const char *)mach_O, 4)==0)) /* generic Mach-O module */ |
---|
89 | { |
---|
90 | LT = LT_MACH_O; |
---|
91 | //omFree(newlib); |
---|
92 | //newlib = omStrDup(libnamebuf); |
---|
93 | goto lib_type_end; |
---|
94 | } |
---|
95 | |
---|
96 | if( (strncmp(buf, (const char *)mach_o64, 4)==0) || (strncmp(buf, (const char *)mach_O64, 4)==0)) /* generic Mach-O 64-bit module */ |
---|
97 | { |
---|
98 | LT = LT_MACH_O; |
---|
99 | //omFree(newlib); |
---|
100 | //newlib = omStrDup(libnamebuf); |
---|
101 | goto lib_type_end; |
---|
102 | } |
---|
103 | |
---|
104 | if( (strncmp(buf, (const char *)mach_FAT, 4)==0) || (strncmp(buf, (const char *)mach_fat, 4)==0)) /* generic Mach-O fat universal module */ |
---|
105 | { |
---|
106 | LT = LT_MACH_O; |
---|
107 | //omFree(newlib); |
---|
108 | //newlib = omStrDup(libnamebuf); |
---|
109 | goto lib_type_end; |
---|
110 | } |
---|
111 | |
---|
112 | if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0)) |
---|
113 | { |
---|
114 | LT = LT_HPUX; |
---|
115 | //omFree(newlib); |
---|
116 | //newlib = omStrDup(libnamebuf); |
---|
117 | goto lib_type_end; |
---|
118 | } |
---|
119 | if(isprint(buf[0]) || buf[0]=='\n') |
---|
120 | { LT = LT_SINGULAR; goto lib_type_end; } |
---|
121 | |
---|
122 | lib_type_end: |
---|
123 | fclose(fp); |
---|
124 | return LT; |
---|
125 | } |
---|