1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include "fileutils.h"
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <dirent.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
/* return 0 on success, perr, or -1 (internal failure) value on failure */
#ifndef WIN32
int mkdir_recursive(const char *targetdir){
char parentdir[1024];
char *lastslash = NULL;
int err = 0;
strcpy(parentdir, targetdir);
lastslash = strrchr(parentdir, '/');
if (lastslash && lastslash != parentdir) {
/* target directory not at root or is relative path, but contains a '/'
check if we need to recursively create parent dirs */
DIR *dir = opendir(parentdir);
*lastslash = 0;
if (!dir) {
int err = mkdir_recursive(parentdir);
if (err) return err;
}
else closedir(dir);
}
/* at this point, the parent directory exists */
err = mkdir(targetdir, 0777);
if (err) {
if (err == -1) {
if (errno == EEXIST) {
return 0;
}
else {
return errno;
}
}
else return err;
}
return 0;
}
#endif
/* read in a loop until done or fread returns zero (EOF) (caller then needs to use feof() or ferror() )
return number of bytes read
*/
size_t fread_loop(void *bufp, size_t elemSize, size_t elems2Read, FILE *theFile) {
size_t totalRead = fread(bufp, elemSize, elems2Read, theFile);
bufp = (void*)((char*)bufp + totalRead);
while (elems2Read > totalRead) {
size_t numread = fread(bufp, elemSize, elems2Read - totalRead, theFile);
if (!numread) { /* EOF */
return totalRead;
}
if (numread < 0) {
fprintf(stderr, "Warning: bad read in fread_loop, continuing...\n");
}
bufp = (void*)((char*)bufp + numread);
totalRead += numread;
}
return totalRead;
}
|