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