My Project
Loading...
Searching...
No Matches
Functions | Variables
feFopen.cc File Reference
#include "singular_resourcesconfig.h"
#include "feResource.h"
#include "feFopen.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

Go to the source code of this file.

Functions

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

Variables

VAR void(* WerrorS_callback )(const char *s) = NULL
 
VAR void(* PrintS_callback )(const char *s) = NULL
 
VAR short errorreported =0
 

Function Documentation

◆ feFopen()

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

Definition at line 47 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;
164}
int p
Definition: cfModGcd.cc:4078
FILE * f
Definition: checklibs.c:9
const CanonicalForm int s
Definition: facAbsFact.cc:51
CanonicalForm res
Definition: facAbsFact.cc:60
bool found
Definition: facFactorize.cc:55
void WerrorS(const char *s)
Definition: feFopen.cc:24
FILE * myfopen(const char *path, const char *mode)
Definition: feFopen.cc:167
static char * feResource(feResourceConfig config, int warn)
Definition: feResource.cc:236
const char fePathSep
Definition: feResource.h:58
#define DIR_SEPP
Definition: feResource.h:7
#define DIR_SEP
Definition: feResource.h:6
char * getenv()
#define free
Definition: omAllocFunc.c:14
#define malloc
Definition: omAllocFunc.c:12
#define NULL
Definition: omList.c:12
#define MAXPATHLEN
Definition: omRet2Info.c:22
int status int void * buf
Definition: si_signals.h:59

◆ myfopen()

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

Definition at line 167 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
192}
int i
Definition: cfEzgcd.cc:132

◆ myfread()

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

Definition at line 195 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;
211}
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600

◆ WerrorS()

void WerrorS ( const char *  s)

Definition at line 24 of file feFopen.cc.

25{
26 errorreported = 1;
28 {
29 fwrite(" ? ",1,5,stderr);
30 fwrite((char *)s,1,strlen((char *)s),stderr);
31 fwrite("\n",1,1,stderr);
32 fflush(stderr);
33 }
34 else
35 {
37 }
38}
VAR void(* WerrorS_callback)(const char *s)
Definition: feFopen.cc:21
VAR short errorreported
Definition: feFopen.cc:23

Variable Documentation

◆ errorreported

VAR short errorreported =0

Definition at line 23 of file feFopen.cc.

◆ PrintS_callback

VAR void(* PrintS_callback) (const char *s) ( const char *  s) = NULL

Definition at line 22 of file feFopen.cc.

◆ WerrorS_callback

VAR void(* WerrorS_callback) (const char *s) ( const char *  s) = NULL

Definition at line 21 of file feFopen.cc.