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