My Project
Loading...
Searching...
No Matches
feFopen.cc
Go to the documentation of this file.
1#include "singular_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
20extern "C" {
21VAR void (*WerrorS_callback)(const char *s) = NULL;
22VAR void (*PrintS_callback)(const char *s) = NULL;
24void WerrorS(const char *s)
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}
39}
40
41/*****************************************************************
42 *
43 * File handling
44 *
45 *****************************************************************/
46
47FILE * feFopen(const char *path, const char *mode, char *where,
48 short useWerror, short path_only)
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}
165
166// Make sure that mode contains binary option
167FILE* myfopen(const char *path, const char *mode)
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}
193// replace "\r\n" by " \n" and "\r" by "\n"
194
195size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
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
int i
Definition: cfEzgcd.cc:132
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
VAR void(* WerrorS_callback)(const char *s)
Definition: feFopen.cc:21
FILE * feFopen(const char *path, const char *mode, char *where, short useWerror, short path_only)
Definition: feFopen.cc:47
VAR short errorreported
Definition: feFopen.cc:23
void WerrorS(const char *s)
Definition: feFopen.cc:24
FILE * myfopen(const char *path, const char *mode)
Definition: feFopen.cc:167
VAR void(* PrintS_callback)(const char *s)
Definition: feFopen.cc:22
size_t myfread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: feFopen.cc:195
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 VAR
Definition: globaldefs.h:5
#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