Changeset 3612 in MondoRescue for branches/3.2/mondo/src/lib/mr_file.c


Ignore:
Timestamp:
Nov 18, 2016, 5:31:40 PM (7 years ago)
Author:
Bruno Cornec
Message:

Addition of a new mr_getcwd function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mondo/src/lib/mr_file.c

    r3509 r3612  
    1313#include <sys/types.h>
    1414#include <sys/stat.h>
     15#include <stdlib.h>
     16#include <errno.h>
     17#include <unistd.h>
    1518
    1619#include "mr_err.h"
     
    6871    }
    6972}
     73
     74/* Version of getcwd using dynamically allocated memory. Cf: man 3p getcwd */
     75char *mr_getcwd_int(int line, char *file) {
     76
     77long path_max;
     78size_t size;
     79char *buf;
     80char *ptr;
     81
     82path_max = pathconf(".", _PC_PATH_MAX);
     83if (path_max == -1) {
     84    size = (size_t)512;
     85} else if (path_max > 10240) {
     86    size = (size_t)10240;
     87} else {
     88    size = (size_t)path_max;
     89}
     90
     91for (buf = ptr = NULL; ptr == NULL; size *= 2) {
     92    if ((buf = realloc(buf, size)) == NULL) {
     93        mr_msg_int(1,line,file,"mr_getcwd","Unable to realloc memory");
     94        mr_exit(-1,"Exiting");
     95    }
     96
     97    ptr = getcwd(buf, size);
     98    if (ptr == NULL && errno != ERANGE) {
     99        mr_msg_int(1,line,file,"mr_getcwd","Unable to get current working directory");
     100        mr_exit(-1,"Exiting");
     101    }
     102return(buf);
     103}
     104}
Note: See TracChangeset for help on using the changeset viewer.