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