feFopen.cc
Go to the documentation of this file.
1 #include "resourcesconfig.h"
2 #include "feResource.h"
3 #include "feFopen.h"
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <errno.h>
12 
13 #if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
14 #include <pwd.h>
15 #endif
16 
17 
18 
19 
20 extern "C" {
21 void (*WerrorS_callback)(const char *s) = NULL;
22 short errorreported=0;
23 void WerrorS(const char *s)
24 {
25  if (WerrorS_callback == NULL)
26  {
27  fwrite(" ? ",1,5,stderr);
28  fwrite((char *)s,1,strlen((char *)s),stderr);
29  fwrite("\n",1,1,stderr);
30  fflush(stderr);
31  }
32  else
33  {
35  }
36  errorreported = 1;
37 }
38 }
39 
40 /*****************************************************************
41  *
42  * File handling
43  *
44  *****************************************************************/
45 
46 FILE * feFopen(const char *path, const char *mode, char *where,
47  short useWerror, short path_only)
48 {
49  char longpath[MAXPATHLEN];
50  if (path[0]=='~')
51  {
52  if (path[1] == DIR_SEP)
53  {
54  const char* home = getenv("HOME");
55 #ifdef __CUGWIN__
56  if ((home==NULL)||(!access(home,X_OK)))
57  home = getenv("SINGHOME");
58 #endif
59  if (home != NULL)
60  {
61  strcpy(longpath, home);
62  strcat(longpath, &(path[1]));
63  path = longpath;
64  }
65  }
66 #if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
67  else
68  {
69  char* dir_sep;
70  struct passwd *pw_entry;
71  strcpy (longpath, path);
72  dir_sep = strchr(longpath, DIR_SEP);
73  if (dir_sep==NULL)
74  {
75  char buf[256];
76  strcpy(buf,"illegal ~ in filename >>");
77  strncat(buf,longpath,235);
78  strcat(buf,"<<");
79  WerrorS(buf);
80  return NULL;
81  }
82  *dir_sep = '\0';
83  pw_entry = getpwnam(&longpath[1]);
84  if (pw_entry != NULL)
85  {
86  strcpy(longpath, pw_entry->pw_dir);
87  dir_sep = strchr((char *)path, DIR_SEP);
88  strcat(longpath, dir_sep);
89  path = longpath;
90  }
91  }
92 #endif
93  }
94  FILE * f=NULL;
95  if (! path_only)
96  {
97  struct stat statbuf;
98  int res = -1;
99  do
100  {
101  res = stat(path,&statbuf);
102  } while((res < 0) and (errno == EINTR));
103  if ((res == 0)
104  && (S_ISREG(statbuf.st_mode)))
105  f = myfopen(path,mode);
106  }
107  if (where!=NULL) strcpy(where,path);
108  if ((*mode=='r') &&
109  (path[0]!=DIR_SEP) &&
110  ! (path[0] == '.' && path[1] == DIR_SEP) &&
111  (f==NULL))
112  {
113  char found = 0;
114  char* spath = feResource('s');
115  char *s;
116 
117  if (where==NULL) s=(char *)malloc(1024);
118  else s=where;
119 
120  if (spath!=NULL)
121  {
122  char *p,*q;
123  p = spath;
124  while( (q=strchr(p, fePathSep)) != NULL)
125  {
126  *q = '\0';
127  strcpy(s,p);
128  *q = fePathSep;
129  strcat(s, DIR_SEPP);
130  strcat(s, path);
131  if(!access(s, R_OK)) { found++; break; }
132  p = q+1;
133  }
134  if(!found)
135  {
136  strcpy(s,p);
137  strcat(s, DIR_SEPP);
138  strcat(s, path);
139  }
140  f=myfopen(s,mode);
141  if (f!=NULL)
142  {
143  if (where==NULL) free(s);
144  return f;
145  }
146  }
147  else
148  {
149  if (where!=NULL) strcpy(s/*where*/,path);
150  f=myfopen(path,mode);
151  }
152  if (where==NULL) free(s);
153  }
154  if ((f==NULL)&&(useWerror))
155  {
156  char buf[256];
157  strcpy(buf,"cannot open `");
158  strncat(buf,path,240);
159  strcat(buf,"`");
160  WerrorS(buf);
161  }
162  return f;
163 }
164 
165 // Make sure that mode contains binary option
166 FILE* myfopen(const char *path, const char *mode)
167 {
168 #if (defined(__CUGWIN__))
169  char mmode[4];
170  int i;
171  int done = 0;
172 
173  for (i=0;;i++)
174  {
175  mmode[i] = mode[i];
176  if (mode[i] == '\0') break;
177  if (mode[i] == 'w') done = 1;
178  if (mode[i] == 'a') done = 1;
179  if (mode[i] == 'b') done = 1;
180  }
181 
182  if (! done)
183  {
184  mmode[i] = 'b';
185  mmode[i+1] = '\0';
186  }
187  return fopen(path, mmode);
188 #else
189  return fopen(path, mode);
190 #endif
191 }
192 // replace "\r\n" by " \n" and "\r" by "\n"
193 
194 size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
195 {
196  size_t got = fread(ptr, size, nmemb, stream) * size;
197  size_t i;
198 
199  for (i=0; i<got; i++)
200  {
201  if ( ((char*) ptr)[i] == '\r')
202  {
203  if (i+1 < got && ((char*) ptr)[i+1] == '\n')
204  ((char*) ptr)[i] = ' ';
205  else
206  ((char*) ptr)[i] = '\n';
207  }
208  }
209  return got;
210 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
#define MAXPATHLEN
Definition: omRet2Info.c:22
return P p
Definition: myNF.cc:203
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:252
char * getenv()
void WerrorS(const char *s)
Definition: feFopen.cc:23
const char fePathSep
Definition: feResource.h:57
bool found
Definition: facFactorize.cc:56
poly res
Definition: myNF.cc:322
#define DIR_SEP
Definition: feResource.h:6
size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: feFopen.cc:194
FILE * myfopen(const char *path, const char *mode)
Definition: feFopen.cc:166
int status int void * buf
Definition: si_signals.h:59
void * malloc(size_t size)
Definition: omalloc.c:92
#define free
Definition: omAllocFunc.c:12
#define DIR_SEPP
Definition: feResource.h:7
FILE * feFopen(const char *path, const char *mode, char *where, short useWerror, short path_only)
Definition: feFopen.cc:46
FILE * f
Definition: checklibs.c:7
int i
Definition: cfEzgcd.cc:123
void(* WerrorS_callback)(const char *s)
Definition: feFopen.cc:21
short errorreported
Definition: feFopen.cc:22
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
#define NULL
Definition: omList.c:10