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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
/*
* static char *rcsid_misc_c =
* "$Id: misc.c,v 1.2 1999/07/12 06:08:03 cvs Exp $";
*/
/* Contains misc useful functions that may be useful to various parts
* of code, but are not especially tied to it.
*/
#include "client.h"
#include <sys/stat.h>
/*
* Verifies that the directory exists, creates it if necessary
* Returns -1 on failure
*/
int make_path_to_dir (char *directory)
{
char buf[MAX_BUF], *cp = buf;
struct stat statbuf;
if (!directory || !*directory)
return -1;
strcpy (buf, directory);
while ((cp = strchr (cp + 1, (int) '/'))) {
*cp = '\0';
if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) {
if (mkdir (buf, 0777)) {
perror ("Couldn't make path to file");
return -1;
}
} else
*cp = '/';
}
/* Need to make the final component */
if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) {
if (mkdir (buf, 0777)) {
perror ("Couldn't make path to file");
return -1;
}
}
return 0;
}
/*
* If any directories in the given path doesn't exist, they are created.
*/
int make_path_to_file (char *filename)
{
char buf[MAX_BUF], *cp = buf;
struct stat statbuf;
if (!filename || !*filename)
return -1;
strcpy (buf, filename);
while ((cp = strchr (cp + 1, (int) '/'))) {
*cp = '\0';
if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) {
if (mkdir (buf, 0777)) {
perror ("Couldn't make path to file");
return -1;
}
}
*cp = '/';
}
return 0;
}
/*
* A replacement of strdup(), since it's not defined at some
* unix variants.
*/
char *strdup_local(char *str) {
char *c=(char *)malloc(sizeof(char)*strlen(str)+1);
strcpy(c,str);
return c;
}
|