Commit c99ebb18 c99ebb1864d4448854be78d0872addb9f346cc6e by Sergey Poznyakoff

(mu_getcwd): New function. Return the name

 of current working directory. The return value is malloc'ed
(mu_get_full_path): New function. Translate cwd-relative
 pathname to its absolute equivalent. Both moved from
 mbx_default.c
1 parent dd881c96
...@@ -265,6 +265,73 @@ mu_get_homedir (void) ...@@ -265,6 +265,73 @@ mu_get_homedir (void)
265 return homedir; 265 return homedir;
266 } 266 }
267 267
268 char *
269 mu_getcwd ()
270 {
271 char *ret;
272 unsigned path_max;
273 char buf[128];
274
275 errno = 0;
276 ret = getcwd (buf, sizeof (buf));
277 if (ret != NULL)
278 return strdup (buf);
279
280 if (errno != ERANGE)
281 return NULL;
282
283 path_max = 128;
284 path_max += 2; /* The getcwd docs say to do this. */
285
286 for (;;)
287 {
288 char *cwd = (char *) malloc (path_max);
289
290 errno = 0;
291 ret = getcwd (cwd, path_max);
292 if (ret != NULL)
293 return ret;
294 if (errno != ERANGE)
295 {
296 int save_errno = errno;
297 free (cwd);
298 errno = save_errno;
299 return NULL;
300 }
301
302 free (cwd);
303
304 path_max += path_max / 16;
305 path_max += 32;
306 }
307 /* oops? */
308 return NULL;
309 }
310
311 char *
312 mu_get_full_path (const char *file)
313 {
314 char *p = NULL;
315
316 if (!file)
317 p = mu_getcwd ();
318 else if (*file != '/')
319 {
320 char *cwd = mu_getcwd ();
321 if (cwd)
322 {
323 p = calloc (strlen (cwd) + 1 + strlen (file) + 1, 1);
324 if (p)
325 sprintf (p, "%s/%s", cwd, file);
326 free (cwd);
327 }
328 }
329
330 if (!p)
331 p = strdup (file);
332 return p;
333 }
334
268 /* NOTE: Allocates Memory. */ 335 /* NOTE: Allocates Memory. */
269 /* Expand: ~ --> /home/user and to ~guest --> /home/guest. */ 336 /* Expand: ~ --> /home/user and to ~guest --> /home/guest. */
270 char * 337 char *
......