1 | /*
|
---|
2 | * Miscellaneous routines.
|
---|
3 | */
|
---|
4 |
|
---|
5 | #include "sysincludes.h"
|
---|
6 | #include "msdos.h"
|
---|
7 | #include "stream.h"
|
---|
8 | #include "vfat.h"
|
---|
9 | #include "mtools.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | void printOom(void)
|
---|
13 | {
|
---|
14 | fprintf(stderr, "Out of memory error");
|
---|
15 | }
|
---|
16 |
|
---|
17 | char *get_homedir(void)
|
---|
18 | {
|
---|
19 | struct passwd *pw;
|
---|
20 | uid_t uid;
|
---|
21 | char *homedir;
|
---|
22 | char *username;
|
---|
23 |
|
---|
24 | homedir = getenv ("HOME");
|
---|
25 | /*
|
---|
26 | * first we call getlogin.
|
---|
27 | * There might be several accounts sharing one uid
|
---|
28 | */
|
---|
29 | if ( homedir )
|
---|
30 | return homedir;
|
---|
31 |
|
---|
32 | pw = 0;
|
---|
33 |
|
---|
34 | username = getenv("LOGNAME");
|
---|
35 | if ( !username )
|
---|
36 | username = getlogin();
|
---|
37 | if ( username )
|
---|
38 | pw = getpwnam( username);
|
---|
39 |
|
---|
40 | if ( pw == 0 ){
|
---|
41 | /* if we can't getlogin, look up the pwent by uid */
|
---|
42 | uid = geteuid();
|
---|
43 | pw = getpwuid(uid);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /* we might still get no entry */
|
---|
47 | if ( pw )
|
---|
48 | return pw->pw_dir;
|
---|
49 | return 0;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | static void get_mcwd_file_name(char *file)
|
---|
54 | {
|
---|
55 | char *mcwd_path;
|
---|
56 | char *homedir;
|
---|
57 |
|
---|
58 | mcwd_path = getenv("MCWD");
|
---|
59 | if (mcwd_path == NULL || *mcwd_path == '\0'){
|
---|
60 | homedir= get_homedir();
|
---|
61 | if(!homedir)
|
---|
62 | homedir="/tmp";
|
---|
63 | strncpy(file, homedir, MAXPATHLEN-6);
|
---|
64 | file[MAXPATHLEN-6]='\0';
|
---|
65 | strcat( file, "/.mcwd");
|
---|
66 | } else {
|
---|
67 | strncpy(file, mcwd_path, MAXPATHLEN);
|
---|
68 | file[MAXPATHLEN]='\0';
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | void unlink_mcwd()
|
---|
73 | {
|
---|
74 | char file[MAXPATHLEN+1];
|
---|
75 | get_mcwd_file_name(file);
|
---|
76 | unlink(file);
|
---|
77 | }
|
---|
78 |
|
---|
79 | FILE *open_mcwd(const char *mode)
|
---|
80 | {
|
---|
81 | struct stat sbuf;
|
---|
82 | char file[MAXPATHLEN+1];
|
---|
83 | time_t now;
|
---|
84 |
|
---|
85 | get_mcwd_file_name(file);
|
---|
86 | if (*mode == 'r'){
|
---|
87 | if (stat(file, &sbuf) < 0)
|
---|
88 | return NULL;
|
---|
89 | /*
|
---|
90 | * Ignore the info, if the file is more than 6 hours old
|
---|
91 | */
|
---|
92 | getTimeNow(&now);
|
---|
93 | if (now - sbuf.st_mtime > 6 * 60 * 60) {
|
---|
94 | fprintf(stderr,
|
---|
95 | "Warning: \"%s\" is out of date, removing it\n",
|
---|
96 | file);
|
---|
97 | unlink(file);
|
---|
98 | return NULL;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | return fopen(file, mode);
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | /* Fix the info in the MCWD file to be a proper directory name.
|
---|
107 | * Always has a leading separator. Never has a trailing separator
|
---|
108 | * (unless it is the path itself). */
|
---|
109 |
|
---|
110 | const char *fix_mcwd(char *ans)
|
---|
111 | {
|
---|
112 | FILE *fp;
|
---|
113 | char *s;
|
---|
114 | char buf[MAX_PATH];
|
---|
115 |
|
---|
116 | fp = open_mcwd("r");
|
---|
117 | if(!fp){
|
---|
118 | strcpy(ans, "A:/");
|
---|
119 | return ans;
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (!fgets(buf, MAX_PATH, fp))
|
---|
123 | return("A:/");
|
---|
124 |
|
---|
125 | buf[strlen(buf) -1] = '\0';
|
---|
126 | fclose(fp);
|
---|
127 | /* drive letter present? */
|
---|
128 | s = skip_drive(buf);
|
---|
129 | if (s > buf) {
|
---|
130 | strncpy(ans, buf, s - buf);
|
---|
131 | ans[s - buf] = '\0';
|
---|
132 | } else
|
---|
133 | strcpy(ans, "A:");
|
---|
134 | /* add a leading separator */
|
---|
135 | if (*s != '/' && *s != '\\') {
|
---|
136 | strcat(ans, "/");
|
---|
137 | strcat(ans, s);
|
---|
138 | } else
|
---|
139 | strcat(ans, s);
|
---|
140 |
|
---|
141 | #if 0
|
---|
142 | /* translate to upper case */
|
---|
143 | for (s = ans; *s; ++s) {
|
---|
144 | *s = toupper(*s);
|
---|
145 | if (*s == '\\')
|
---|
146 | *s = '/';
|
---|
147 | }
|
---|
148 | #endif
|
---|
149 | /* if only drive, colon, & separator */
|
---|
150 | if (strlen(ans) == 3)
|
---|
151 | return(ans);
|
---|
152 | /* zap the trailing separator */
|
---|
153 | if (*--s == '/')
|
---|
154 | *s = '\0';
|
---|
155 | return ans;
|
---|
156 | }
|
---|
157 |
|
---|
158 | void *safe_malloc(size_t size)
|
---|
159 | {
|
---|
160 | void *p;
|
---|
161 |
|
---|
162 | p = malloc(size);
|
---|
163 | if(!p){
|
---|
164 | printOom();
|
---|
165 | exit(1);
|
---|
166 | }
|
---|
167 | return p;
|
---|
168 | }
|
---|
169 |
|
---|
170 | void print_sector(char *message, unsigned char *data, int size)
|
---|
171 | {
|
---|
172 | int col;
|
---|
173 | int row;
|
---|
174 |
|
---|
175 | printf("%s:\n", message);
|
---|
176 |
|
---|
177 | for(row = 0; row * 16 < size; row++){
|
---|
178 | printf("%03x ", row * 16);
|
---|
179 | for(col = 0; col < 16; col++)
|
---|
180 | printf("%02x ", data [row*16+col]);
|
---|
181 | for(col = 0; col < 16; col++) {
|
---|
182 | if(isprint(data [row*16+col]))
|
---|
183 | printf("%c", data [row*16+col]);
|
---|
184 | else
|
---|
185 | printf(".");
|
---|
186 | }
|
---|
187 | printf("\n");
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | time_t getTimeNow(time_t *now)
|
---|
193 | {
|
---|
194 | static int haveTime = 0;
|
---|
195 | static time_t sharedNow;
|
---|
196 |
|
---|
197 | if(!haveTime) {
|
---|
198 | time(&sharedNow);
|
---|
199 | haveTime = 1;
|
---|
200 | }
|
---|
201 | if(now)
|
---|
202 | *now = sharedNow;
|
---|
203 | return sharedNow;
|
---|
204 | }
|
---|
205 |
|
---|
206 | char *skip_drive(const char *filename)
|
---|
207 | {
|
---|
208 | char *p;
|
---|
209 |
|
---|
210 | /* Skip drive name. Return pointer just after the `:', or a pointer
|
---|
211 | * to the start of the file name if there is is no drive name.
|
---|
212 | */
|
---|
213 | p = strchr(filename, ':');
|
---|
214 | return (p == NULL || p == filename) ? (char *) filename : p + 1;
|
---|
215 | }
|
---|
216 |
|
---|
217 | char *get_drive(const char *filename, const char *def)
|
---|
218 | {
|
---|
219 | const char *path;
|
---|
220 | char *drive;
|
---|
221 | const char *rest;
|
---|
222 | size_t len;
|
---|
223 |
|
---|
224 | /* Return the drive name part of a full filename. */
|
---|
225 |
|
---|
226 | path = filename;
|
---|
227 | rest = skip_drive(path);
|
---|
228 | if (rest == path) {
|
---|
229 | if (def == NULL) def = "A:";
|
---|
230 | path = def;
|
---|
231 | rest = skip_drive(path);
|
---|
232 | if (rest == path) {
|
---|
233 | path = "A:";
|
---|
234 | rest = path+2;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | len = rest - path;
|
---|
238 | drive = safe_malloc(len * sizeof(drive[0]));
|
---|
239 | len--;
|
---|
240 | memcpy(drive, path, len);
|
---|
241 | drive[len] = 0;
|
---|
242 | if (len == 1) drive[0] = toupper(drive[0]);
|
---|
243 | return drive;
|
---|
244 | }
|
---|
245 |
|
---|
246 | #if 0
|
---|
247 |
|
---|
248 | #undef free
|
---|
249 | #undef malloc
|
---|
250 |
|
---|
251 | static int total=0;
|
---|
252 |
|
---|
253 | void myfree(void *ptr)
|
---|
254 | {
|
---|
255 | int *size = ((int *) ptr)-1;
|
---|
256 | total -= *size;
|
---|
257 | fprintf(stderr, "freeing %d bytes at %p total alloced=%d\n",
|
---|
258 | *size, ptr, total);
|
---|
259 | free(size);
|
---|
260 | }
|
---|
261 |
|
---|
262 | void *mymalloc(size_t size)
|
---|
263 | {
|
---|
264 | int *ptr;
|
---|
265 | ptr = (int *)malloc(size+sizeof(int));
|
---|
266 | if(!ptr)
|
---|
267 | return 0;
|
---|
268 | *ptr = size;
|
---|
269 | ptr++;
|
---|
270 | total += size;
|
---|
271 | fprintf(stderr, "allocating %d bytes at %p total allocated=%d\n",
|
---|
272 | size, ptr, total);
|
---|
273 | return (void *) ptr;
|
---|
274 | }
|
---|
275 |
|
---|
276 | void *mycalloc(size_t nmemb, size_t size)
|
---|
277 | {
|
---|
278 | void *ptr = mymalloc(nmemb * size);
|
---|
279 | if(!ptr)
|
---|
280 | return 0;
|
---|
281 | memset(ptr, 0, size);
|
---|
282 | return ptr;
|
---|
283 | }
|
---|
284 |
|
---|
285 | void *myrealloc(void *ptr, size_t size)
|
---|
286 | {
|
---|
287 | int oldsize = ((int *)ptr) [-1];
|
---|
288 | void *new = mymalloc(size);
|
---|
289 | if(!new)
|
---|
290 | return 0;
|
---|
291 | memcpy(new, ptr, oldsize);
|
---|
292 | myfree(ptr);
|
---|
293 | return new;
|
---|
294 | }
|
---|
295 |
|
---|
296 | char *mystrdup(char *src)
|
---|
297 | {
|
---|
298 | char *dest;
|
---|
299 | dest = mymalloc(strlen(src)+1);
|
---|
300 | if(!dest)
|
---|
301 | return 0;
|
---|
302 | strcpy(dest, src);
|
---|
303 | return dest;
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | #endif
|
---|