static int mkpath( const char *path, mode_t mode ) { char opath[PATH_MAX]; char *p; size_t len; int ret; if (path == NULL) return 0; strncpy( opath, path, sizeof(opath) ); len = strlen(opath); if (opath[len - 1] == '/') opath[len - 1] = '\0'; for (p=&opath[1]; p[0]!='\0'; p++) { if (p[0] == '/') { p[0] = '\0'; if (!nfile_dirExists(opath)) { ret = mkdir( opath, mode ); if (ret) return ret; } p[0] = '/'; } } if (!nfile_dirExists(opath)) { /* if path is not terminated with / */ ret = mkdir( opath, mode ); if (ret) return ret; } return 0; }
/** * @brief Creates a directory if it doesn't exist. * * Uses relative paths to basePath. * * @param path Path to create directory if it doesn't exist. * @return 0 on success. */ int nfile_dirMakeExist( const char* path, ... ) { char file[PATH_MAX]; va_list ap; if (path == NULL) return -1; else { /* get the message */ va_start(ap, path); vsnprintf(file, PATH_MAX, path, ap); va_end(ap); } /* Check if it exists. */ if (nfile_dirExists(file)) return 0; #if HAS_POSIX if (mkdir(file, S_IRWXU | S_IRWXG | S_IRWXO) < 0) { WARN("Dir '%s' does not exist and unable to create: %s", file, strerror(errno)); return -1; } #elif HAS_WIN32 if (!CreateDirectory(file, NULL)) { WARN("Dir '%s' does not exist and unable to create: %s", file, strerror(errno)); return -1; } #else #error "Feature needs implementation on this Operating System for Naev to work." #endif return 0; }
static int mkpath( const char *path ) #else #error "Feature needs implementation on this Operating System for Naev to work." #endif { char opath[PATH_MAX]; char *p; size_t len; int ret; if (path == NULL) return 0; strncpy( opath, path, sizeof(opath) ); opath[ PATH_MAX-1 ] = '\0'; len = strlen(opath); if (opath[len - 1] == '/') opath[len - 1] = '\0'; for (p=&opath[1]; p[0]!='\0'; p++) { if (p[0] == '/') { p[0] = '\0'; if (!nfile_dirExists(opath)) { ret = MKDIR; if (ret) return ret; } p[0] = '/'; } } if (!nfile_dirExists(opath)) { /* if path is not terminated with / */ ret = MKDIR; if (ret) return ret; } return 0; }