source: git/Singular/mod_lib.cc @ 8d1432e

spielwiese
Last change on this file since 8d1432e was 9b9d9a, checked in by Hans Schoenemann <hannes@…>, 9 years ago
check for utf16/utf8 prefix
  • Property mode set to 100644
File size: 3.3 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 <stdio.h>
10#include <string.h>
11#include <ctype.h>
12#include <sys/stat.h>
13#include <errno.h>
14
15
16#define SI_BUILTIN_LIBSTR(name) (char*) #name ".so",
17
18const char * const si_builtin_libs[] = { SI_FOREACH_BUILTIN(SI_BUILTIN_LIBSTR) NULL };
19
20#undef SI_BUILTIN_LIBSTR
21
22#define BYTES_TO_CHECK 7
23
24lib_types type_of_LIB(const char *newlib, char *libnamebuf)
25{
26  const unsigned char mach_o[]={0xfe,0xed,0xfa,0xce,0};
27  const unsigned char mach_O[]={0xce,0xfa,0xed,0xfe,0};
28
29  const unsigned char mach_o64[]={0xfe,0xed,0xfa,0xcf,0};
30  const unsigned char mach_O64[]={0xcf,0xfa,0xed,0xfe,0};
31
32  const unsigned char mach_FAT[]={0xca,0xfe,0xba,0xbe,0};
33  const unsigned char mach_fat[]={0xbe,0xba,0xfe,0xca,0};
34
35  const unsigned char utf16be[]={0xfe,0xff,0};
36  const unsigned char utf16le[]={0xff,0xfe,0};
37  const unsigned char utf8ms[]={0xEF,0xBB,0xBF,0};
38
39  int i=0;
40  while(si_builtin_libs[i]!=NULL)
41  {
42    if (strcmp(newlib,si_builtin_libs[i])==0)
43    {
44      if(libnamebuf!=NULL) strcpy(libnamebuf,newlib);
45      return LT_BUILTIN;
46    }
47    i++;
48  }
49  char        buf[BYTES_TO_CHECK+1];        /* one extra for terminating '\0' */
50  struct stat sb;
51  int nbytes = 0;
52  int ret;
53  lib_types LT=LT_NONE;
54
55  FILE * fp = feFopen( newlib, "r", libnamebuf, FALSE );
56
57  do
58  {
59    ret = stat(libnamebuf, &sb);
60  } while((ret < 0) and (errno == EINTR));
61
62  if (fp==NULL)
63  {
64    return LT_NOTFOUND;
65  }
66  if((sb.st_mode & S_IFMT) != S_IFREG)
67  {
68    goto lib_type_end;
69  }
70  if ((nbytes = fread((char *)buf, sizeof(char), BYTES_TO_CHECK, fp)) == -1)
71  {
72    goto lib_type_end;
73    /*NOTREACHED*/
74  }
75  if (nbytes == 0)
76    goto lib_type_end;
77  else
78  {
79    buf[nbytes++] = '\0';        /* null-terminate it */
80  }
81  if( (strncmp(buf, "\177ELF", 4)==0)) /* generic ELF */
82  {
83    LT = LT_ELF;
84    //omFree(newlib);
85    //newlib = omStrDup(libnamebuf);
86    goto lib_type_end;
87  }
88
89  if( (strncmp(buf, (const char *)mach_o, 4)==0) || (strncmp(buf, (const char *)mach_O, 4)==0)) /* generic Mach-O module */
90  {
91    LT = LT_MACH_O;
92    //omFree(newlib);
93    //newlib = omStrDup(libnamebuf);
94    goto lib_type_end;
95  }
96
97  if( (strncmp(buf, (const char *)mach_o64, 4)==0) || (strncmp(buf, (const char *)mach_O64, 4)==0)) /* generic Mach-O 64-bit module */
98  {
99    LT = LT_MACH_O;
100    //omFree(newlib);
101    //newlib = omStrDup(libnamebuf);
102    goto lib_type_end;
103  }
104
105  if( (strncmp(buf, (const char *)mach_FAT, 4)==0) || (strncmp(buf, (const char *)mach_fat, 4)==0)) /* generic Mach-O fat universal module */
106  {
107    LT = LT_MACH_O;
108    //omFree(newlib);
109    //newlib = omStrDup(libnamebuf);
110    goto lib_type_end;
111  }
112
113  if( (strncmp(buf, "\02\020\01\016\05\022@", 7)==0))
114  {
115    LT = LT_HPUX;
116    //omFree(newlib);
117    //newlib = omStrDup(libnamebuf);
118    goto lib_type_end;
119  }
120  if ((strncmp(buf,(const char *)utf16be,2)==0)
121  ||(strncmp(buf,(const char *)utf16le,2)==0))
122  {
123    WerrorS("UTF-16 not supported");
124    LT=LT_NOTFOUND;
125    goto lib_type_end;
126  }
127  if (strncmp(buf,(const char *)utf8ms,3)==0)
128  {
129    WarnS("UTF-8 detected - may not work");
130    LT=LT_SINGULAR;
131    goto lib_type_end;
132  }
133  if(isprint(buf[0]) || buf[0]=='\n')
134  { LT = LT_SINGULAR; goto lib_type_end; }
135
136  lib_type_end:
137  fclose(fp);
138  return LT;
139}
Note: See TracBrowser for help on using the repository browser.