Macros | Functions | Variables
mod_lib.cc File Reference
#include <kernel/mod2.h>
#include <resources/feFopen.h>
#include <reporter/reporter.h>
#include <polys/mod_raw.h>
#include <Singular/mod_lib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include <errno.h>

Go to the source code of this file.

Macros

#define SI_BUILTIN_LIBSTR(name)   (char*) #name ".so",
 
#define BYTES_TO_CHECK   7
 

Functions

lib_types type_of_LIB (const char *newlib, char *libnamebuf)
 

Variables

const char *const si_builtin_libs [] = { SI_FOREACH_BUILTIN(SI_BUILTIN_LIBSTR) NULL }
 

Macro Definition Documentation

◆ BYTES_TO_CHECK

#define BYTES_TO_CHECK   7

Definition at line 22 of file mod_lib.cc.

◆ SI_BUILTIN_LIBSTR

#define SI_BUILTIN_LIBSTR (   name)    (char*) #name ".so",

Definition at line 16 of file mod_lib.cc.

Function Documentation

◆ type_of_LIB()

lib_types type_of_LIB ( const char *  newlib,
char *  libnamebuf 
)

Definition at line 24 of file mod_lib.cc.

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

Variable Documentation

◆ si_builtin_libs

const char* const si_builtin_libs[] = { SI_FOREACH_BUILTIN(SI_BUILTIN_LIBSTR) NULL }

Definition at line 18 of file mod_lib.cc.