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