source: git/Singular/mod_lib.cc @ f47408

fieker-DuValspielwiese
Last change on this file since f47408 was 117e00e, checked in by Hans Schoenemann <hannes@…>, 6 years ago
removed unused system includes, math.h ->cmath for .cc files
  • Property mode set to 100644
File size: 3.4 KB
Line 
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
16const char * const si_builtin_libs[] = { SI_FOREACH_BUILTIN(SI_BUILTIN_LIBSTR) NULL };
17
18#undef SI_BUILTIN_LIBSTR
19
20#define BYTES_TO_CHECK 7
21
22lib_types type_of_LIB(const char *newlib, char *libnamebuf)
23{
24  const unsigned char mach_o[]={0xfe,0xed,0xfa,0xce,0};
25  const unsigned char mach_O[]={0xce,0xfa,0xed,0xfe,0};
26
27  const unsigned char mach_o64[]={0xfe,0xed,0xfa,0xcf,0};
28  const unsigned char mach_O64[]={0xcf,0xfa,0xed,0xfe,0};
29
30  const unsigned char mach_FAT[]={0xca,0xfe,0xba,0xbe,0};
31  const unsigned char mach_fat[]={0xbe,0xba,0xfe,0xca,0};
32
33  const unsigned char utf16be[]={0xfe,0xff,0};
34  const unsigned char utf16le[]={0xff,0xfe,0};
35  const unsigned char utf8ms[]={0xEF,0xBB,0xBF,0};
36
37  const unsigned char dll[]={'M','Z',0};
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 ((strncmp(buf,(const char *)utf16be,2)==0)
120  ||(strncmp(buf,(const char *)utf16le,2)==0))
121  {
122    WerrorS("UTF-16 not supported");
123    LT=LT_NOTFOUND;
124    goto lib_type_end;
125  }
126  if (strncmp(buf,(const char *)utf8ms,3)==0)
127  {
128    WarnS("UTF-8 detected - may not work");
129    LT=LT_SINGULAR;
130    goto lib_type_end;
131  }
132  if (strncmp(buf,(const char *)dll,2)==0)
133  {
134    LT=LT_DLL;
135    goto lib_type_end;
136  }
137  if(isprint(buf[0]) || buf[0]=='\n')
138  { LT = LT_SINGULAR; goto lib_type_end; }
139
140  lib_type_end:
141  fclose(fp);
142  return LT;
143}
Note: See TracBrowser for help on using the repository browser.