My Project
mod_lib.cc
Go to the documentation of this file.
1 #include "kernel/mod2.h"
2 
3 #include "resources/feFopen.h"
4 #include "reporter/reporter.h"
5 #include "polys/mod_raw.h"
6 
7 #include "Singular/mod_lib.h"
8 
9 #include <ctype.h>
10 #include <sys/stat.h>
11 #include <errno.h>
12 
13 
14 #define SI_BUILTIN_LIBSTR(name) (char*) #name ".so",
15 
17 #ifdef HAVE_FLINT
18  "flint.so",
19 #endif
20  NULL };
21 
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  const unsigned char utf16be[]={0xfe,0xff,0};
39  const unsigned char utf16le[]={0xff,0xfe,0};
40  const unsigned char utf8ms[]={0xEF,0xBB,0xBF,0};
41 
42  const unsigned char dll[]={'M','Z',0};
43  int i=0;
44  while(si_builtin_libs[i]!=NULL)
45  {
46  if (strcmp(newlib,si_builtin_libs[i])==0)
47  {
48  if(libnamebuf!=NULL) strcpy(libnamebuf,newlib);
49  return LT_BUILTIN;
50  }
51  i++;
52  }
53  char buf[BYTES_TO_CHECK+1]; /* one extra for terminating '\0' */
54  struct stat sb;
55  int nbytes = 0;
56  int ret;
57  lib_types LT=LT_NONE;
58 
59  FILE * fp = feFopen( newlib, "r", libnamebuf, FALSE );
60 
61  do
62  {
63  ret = stat(libnamebuf, &sb);
64  } while((ret < 0) and (errno == EINTR));
65 
66  if (fp==NULL)
67  {
68  return LT_NOTFOUND;
69  }
70  if((sb.st_mode & S_IFMT) != S_IFREG)
71  {
72  goto lib_type_end;
73  }
74  if ((nbytes = fread((char *)buf, sizeof(char), BYTES_TO_CHECK, fp)) == -1)
75  {
76  goto lib_type_end;
77  /*NOTREACHED*/
78  }
79  if (nbytes == 0)
80  goto lib_type_end;
81  else
82  {
83  buf[nbytes++] = '\0'; /* null-terminate it */
84  }
85  if( (strncmp(buf, "\177ELF", 4)==0)) /* generic ELF */
86  {
87  LT = LT_ELF;
88  //omFree(newlib);
89  //newlib = omStrDup(libnamebuf);
90  goto lib_type_end;
91  }
92 
93  if( (strncmp(buf, (const char *)mach_o, 4)==0) || (strncmp(buf, (const char *)mach_O, 4)==0)) /* generic Mach-O 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, (const char *)mach_o64, 4)==0) || (strncmp(buf, (const char *)mach_O64, 4)==0)) /* generic Mach-O 64-bit module */
102  {
103  LT = LT_MACH_O;
104  //omFree(newlib);
105  //newlib = omStrDup(libnamebuf);
106  goto lib_type_end;
107  }
108 
109  if( (strncmp(buf, (const char *)mach_FAT, 4)==0) || (strncmp(buf, (const char *)mach_fat, 4)==0)) /* generic Mach-O fat universal module */
110  {
111  LT = LT_MACH_O;
112  //omFree(newlib);
113  //newlib = omStrDup(libnamebuf);
114  goto lib_type_end;
115  }
116 
117  if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0))
118  {
119  LT = LT_HPUX;
120  //omFree(newlib);
121  //newlib = omStrDup(libnamebuf);
122  goto lib_type_end;
123  }
124  if ((strncmp(buf,(const char *)utf16be,2)==0)
125  ||(strncmp(buf,(const char *)utf16le,2)==0))
126  {
127  WerrorS("UTF-16 not supported");
128  LT=LT_NOTFOUND;
129  goto lib_type_end;
130  }
131  if (strncmp(buf,(const char *)utf8ms,3)==0)
132  {
133  WarnS("UTF-8 detected - may not work");
134  LT=LT_SINGULAR;
135  goto lib_type_end;
136  }
137  if (strncmp(buf,(const char *)dll,2)==0)
138  {
139  LT=LT_DLL;
140  goto lib_type_end;
141  }
142  if(isprint(buf[0]) || buf[0]=='\n')
143  { LT = LT_SINGULAR; goto lib_type_end; }
144 
145  lib_type_end:
146  fclose(fp);
147  return LT;
148 }
#define FALSE
Definition: auxiliary.h:96
int i
Definition: cfEzgcd.cc:132
CanonicalForm fp
Definition: cfModGcd.cc:4104
#define WarnS
Definition: emacs.cc:78
void WerrorS(const char *s)
Definition: feFopen.cc:24
FILE * feFopen(const char *path, const char *mode, char *where, short useWerror, short path_only)
Definition: feFopen.cc:47
SI_FOREACH_BUILTIN(SI_GET_BUILTIN_MOD_INIT0) }
VAR char libnamebuf[1024]
Definition: libparse.cc:1098
lib_types type_of_LIB(const char *newlib, char *libnamebuf)
Definition: mod_lib.cc:27
const char *const si_builtin_libs[]
Definition: mod_lib.cc:16
#define SI_BUILTIN_LIBSTR(name)
Definition: mod_lib.cc:14
#define BYTES_TO_CHECK
Definition: mod_lib.cc:25
lib_types
Definition: mod_raw.h:16
@ LT_MACH_O
Definition: mod_raw.h:16
@ LT_HPUX
Definition: mod_raw.h:16
@ LT_DLL
Definition: mod_raw.h:16
@ LT_SINGULAR
Definition: mod_raw.h:16
@ LT_BUILTIN
Definition: mod_raw.h:16
@ LT_ELF
Definition: mod_raw.h:16
@ LT_NONE
Definition: mod_raw.h:16
@ LT_NOTFOUND
Definition: mod_raw.h:16
#define NULL
Definition: omList.c:12
int status int void * buf
Definition: si_signals.h:59