1 | #ifdef HAVE_CONFIG_H |
---|
2 | #include "config.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(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 | int i=0; |
---|
36 | while(si_builtin_libs[i]!=NULL) |
---|
37 | { |
---|
38 | if (strcmp(newlib,si_builtin_libs[i])==0) |
---|
39 | { |
---|
40 | if(libnamebuf!=NULL) strcpy(libnamebuf,newlib); |
---|
41 | return LT_BUILTIN; |
---|
42 | } |
---|
43 | i++; |
---|
44 | } |
---|
45 | char buf[BYTES_TO_CHECK+1]; /* one extra for terminating '\0' */ |
---|
46 | struct stat sb; |
---|
47 | int nbytes = 0; |
---|
48 | int ret; |
---|
49 | lib_types LT=LT_NONE; |
---|
50 | |
---|
51 | FILE * fp = feFopen( newlib, "r", libnamebuf, FALSE ); |
---|
52 | |
---|
53 | do |
---|
54 | { |
---|
55 | ret = stat(libnamebuf, &sb); |
---|
56 | } while((ret < 0) and (errno == EINTR)); |
---|
57 | |
---|
58 | if (fp==NULL) |
---|
59 | { |
---|
60 | return LT_NOTFOUND; |
---|
61 | } |
---|
62 | if((sb.st_mode & S_IFMT) != S_IFREG) |
---|
63 | { |
---|
64 | goto lib_type_end; |
---|
65 | } |
---|
66 | if ((nbytes = fread((char *)buf, sizeof(char), BYTES_TO_CHECK, fp)) == -1) |
---|
67 | { |
---|
68 | goto lib_type_end; |
---|
69 | /*NOTREACHED*/ |
---|
70 | } |
---|
71 | if (nbytes == 0) |
---|
72 | goto lib_type_end; |
---|
73 | else |
---|
74 | { |
---|
75 | buf[nbytes++] = '\0'; /* null-terminate it */ |
---|
76 | } |
---|
77 | if( (strncmp(buf, "\177ELF", 4)==0)) /* generic ELF */ |
---|
78 | { |
---|
79 | LT = LT_ELF; |
---|
80 | //omFree(newlib); |
---|
81 | //newlib = omStrDup(libnamebuf); |
---|
82 | goto lib_type_end; |
---|
83 | } |
---|
84 | |
---|
85 | if( (strncmp(buf, (const char *)mach_o, 4)==0) || (strncmp(buf, (const char *)mach_O, 4)==0)) /* generic Mach-O module */ |
---|
86 | { |
---|
87 | LT = LT_MACH_O; |
---|
88 | //omFree(newlib); |
---|
89 | //newlib = omStrDup(libnamebuf); |
---|
90 | goto lib_type_end; |
---|
91 | } |
---|
92 | |
---|
93 | if( (strncmp(buf, (const char *)mach_o64, 4)==0) || (strncmp(buf, (const char *)mach_O64, 4)==0)) /* generic Mach-O 64-bit module */ |
---|
94 | { |
---|
95 | LT = LT_MACH_O; |
---|
96 | //omFree(newlib); |
---|
97 | //newlib = omStrDup(libnamebuf); |
---|
98 | goto lib_type_end; |
---|
99 | } |
---|
100 | |
---|
101 | if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0)) |
---|
102 | { |
---|
103 | LT = LT_HPUX; |
---|
104 | //omFree(newlib); |
---|
105 | //newlib = omStrDup(libnamebuf); |
---|
106 | goto lib_type_end; |
---|
107 | } |
---|
108 | if(isprint(buf[0]) || buf[0]=='\n') |
---|
109 | { LT = LT_SINGULAR; goto lib_type_end; } |
---|
110 | |
---|
111 | lib_type_end: |
---|
112 | fclose(fp); |
---|
113 | return LT; |
---|
114 | } |
---|