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