Changeset 3612 for branches


Ignore:
Timestamp:
Nov 18, 2016, 5:31:40 PM (2 years ago)
Author:
bruno
Message:

Addition of a new mr_getcwd function

Location:
branches/3.2/mondo/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mondo/src/include/mr_file.h

    r2208 r3612  
    2121#define mr_fclose(x)            mr_fclose_int((FILE **)&x, __LINE__,__FILE__)
    2222#define mr_mkdir(x,y)           mr_mkdir_int((const char *)x,(mode_t)y,__LINE__,__FILE__)
     23#define mr_getcwd()         mr_getcwd_int(__LINE__,__FILE__)
    2324
    2425extern FILE *mr_fopen_int(const char *path, const char *mode,int line, char *file);
     
    2627extern void mr_fclose_int(FILE **fd, int line, char *file);
    2728extern void mr_mkdir_int(const char *pathname, mode_t mode, int line, char *file);
     29extern char *mr_mkdir_int(int line, char *file);
    2830
    2931#endif                          /* MR_FILE_H */
  • 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.