My Project  UNKNOWN_GIT_VERSION
Functions | Variables
feFopen.h File Reference
#include <stdio.h>

Go to the source code of this file.

Functions

FILE * feFopen (const char *path, const char *mode, char *where=0, short useWerror=0, short path_only=0)
 
FILE * myfopen (const char *path, const char *mode)
 
size_t myfread (void *ptr, size_t size, size_t nmemb, FILE *stream)
 
void WerrorS (const char *s)
 

Variables

short errorreported
 
void(* WerrorS_callback )(const char *s)
 
void(* PrintS_callback )(const char *s)
 

Function Documentation

◆ feFopen()

FILE* feFopen ( const char *  path,
const char *  mode,
char *  where = 0,
short  useWerror = 0,
short  path_only = 0 
)

Definition at line 46 of file feFopen.cc.

49 {
50  char longpath[MAXPATHLEN];
51  if (path[0]=='~')
52  {
53  if (path[1] == DIR_SEP)
54  {
55  const char* home = getenv("HOME");
56 #ifdef __CUGWIN__
57  if ((home==NULL)||(!access(home,X_OK)))
58  home = getenv("SINGHOME");
59 #endif
60  if (home != NULL)
61  {
62  strcpy(longpath, home);
63  strcat(longpath, &(path[1]));
64  path = longpath;
65  }
66  }
67 #if defined(HAVE_PWD_H) && defined(HAVE_GETPWNAM)
68  else
69  {
70  char* dir_sep;
71  struct passwd *pw_entry;
72  strcpy (longpath, path);
73  dir_sep = strchr(longpath, DIR_SEP);
74  if (dir_sep==NULL)
75  {
76  char buf[256];
77  strcpy(buf,"illegal ~ in filename >>");
78  strncat(buf,longpath,235);
79  strcat(buf,"<<");
80  WerrorS(buf);
81  return NULL;
82  }
83  *dir_sep = '\0';
84  pw_entry = getpwnam(&longpath[1]);
85  if (pw_entry != NULL)
86  {
87  strcpy(longpath, pw_entry->pw_dir);
88  dir_sep = strchr((char *)path, DIR_SEP);
89  strcat(longpath, dir_sep);
90  path = longpath;
91  }
92  }
93 #endif
94  }
95  FILE * f=NULL;
96  if (! path_only)
97  {
98  struct stat statbuf;
99  int res = -1;
100  do
101  {
102  res = stat(path,&statbuf);
103  } while((res < 0) and (errno == EINTR));
104  if ((res == 0)
105  && (S_ISREG(statbuf.st_mode)))
106  f = myfopen(path,mode);
107  }
108  if (where!=NULL) strcpy(where,path);
109  if ((*mode=='r') &&
110  (path[0]!=DIR_SEP) &&
111  ! (path[0] == '.' && path[1] == DIR_SEP) &&
112  (f==NULL))
113  {
114  char found = 0;
115  char* spath = feResource('s');
116  char *s;
117 
118  if (where==NULL) s=(char *)malloc(1024);
119  else s=where;
120 
121  if (spath!=NULL)
122  {
123  char *p,*q;
124  p = spath;
125  while( (q=strchr(p, fePathSep)) != NULL)
126  {
127  *q = '\0';
128  strcpy(s,p);
129  *q = fePathSep;
130  strcat(s, DIR_SEPP);
131  strcat(s, path);
132  if(!access(s, R_OK)) { found++; break; }
133  p = q+1;
134  }
135  if(!found)
136  {
137  strcpy(s,p);
138  strcat(s, DIR_SEPP);
139  strcat(s, path);
140  }
141  f=myfopen(s,mode);
142  if (f!=NULL)
143  {
144  if (where==NULL) free(s);
145  return f;
146  }
147  }
148  else
149  {
150  if (where!=NULL) strcpy(s/*where*/,path);
151  f=myfopen(path,mode);
152  }
153  if (where==NULL) free(s);
154  }
155  if ((f==NULL)&&(useWerror))
156  {
157  char buf[256];
158  strcpy(buf,"cannot open `");
159  strncat(buf,path,240);
160  strcat(buf,"`");
161  WerrorS(buf);
162  }
163  return f;

◆ myfopen()

FILE* myfopen ( const char *  path,
const char *  mode 
)

Definition at line 166 of file feFopen.cc.

168 {
169 #if (defined(__CUGWIN__))
170  char mmode[4];
171  int i;
172  int done = 0;
173 
174  for (i=0;;i++)
175  {
176  mmode[i] = mode[i];
177  if (mode[i] == '\0') break;
178  if (mode[i] == 'w') done = 1;
179  if (mode[i] == 'a') done = 1;
180  if (mode[i] == 'b') done = 1;
181  }
182 
183  if (! done)
184  {
185  mmode[i] = 'b';
186  mmode[i+1] = '\0';
187  }
188  return fopen(path, mmode);
189 #else
190  return fopen(path, mode);
191 #endif

◆ myfread()

size_t myfread ( void *  ptr,
size_t  size,
size_t  nmemb,
FILE *  stream 
)

Definition at line 194 of file feFopen.cc.

196 {
197  size_t got = fread(ptr, size, nmemb, stream) * size;
198  size_t i;
199 
200  for (i=0; i<got; i++)
201  {
202  if ( ((char*) ptr)[i] == '\r')
203  {
204  if (i+1 < got && ((char*) ptr)[i+1] == '\n')
205  ((char*) ptr)[i] = ' ';
206  else
207  ((char*) ptr)[i] = '\n';
208  }
209  }
210  return got;

◆ WerrorS()

void WerrorS ( const char *  s)

Definition at line 24 of file feFopen.cc.

25 {
26  if (WerrorS_callback == NULL)
27  {
28  fwrite(" ? ",1,5,stderr);
29  fwrite((char *)s,1,strlen((char *)s),stderr);
30  fwrite("\n",1,1,stderr);
31  fflush(stderr);
32  }
33  else
34  {
36  }
37  errorreported = 1;
38 }

Variable Documentation

◆ errorreported

short errorreported

Definition at line 23 of file feFopen.cc.

◆ PrintS_callback

void(* PrintS_callback) (const char *s)

Definition at line 22 of file feFopen.cc.

◆ WerrorS_callback

void(* WerrorS_callback) (const char *s)

Definition at line 21 of file feFopen.cc.

getenv
char * getenv()
f
FILE * f
Definition: checklibs.c:9
fePathSep
const char fePathSep
Definition: feResource.h:56
errorreported
short errorreported
Definition: feFopen.cc:23
found
bool found
Definition: facFactorize.cc:56
i
int i
Definition: cfEzgcd.cc:125
res
CanonicalForm res
Definition: facAbsFact.cc:64
buf
int status int void * buf
Definition: si_signals.h:58
DIR_SEPP
#define DIR_SEPP
Definition: feResource.h:7
malloc
void * malloc(size_t size)
Definition: omalloc.c:91
size
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
free
#define free
Definition: omAllocFunc.c:11
feResource
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:254
DIR_SEP
#define DIR_SEP
Definition: feResource.h:6
myfopen
FILE * myfopen(const char *path, const char *mode)
Definition: feFopen.cc:166
WerrorS
void WerrorS(const char *s)
Definition: feFopen.cc:24
WerrorS_callback
void(* WerrorS_callback)(const char *s)
Definition: feFopen.cc:21
NULL
#define NULL
Definition: omList.c:9
MAXPATHLEN
#define MAXPATHLEN
Definition: omRet2Info.c:21
p
int p
Definition: cfModGcd.cc:4019
s
const CanonicalForm int s
Definition: facAbsFact.cc:55