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