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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "imapfilter.h"
#include "pathnames.h"
extern environment env;
/*
* Create imapfilter's home directory.
*/
int
create_homedir(void)
{
int n;
char b;
char *hd;
n = snprintf(&b, 1, "%s/%s", env.home, PATHNAME_HOME);
if (env.pathmax != -1 && n > env.pathmax)
fatal(ERROR_PATHNAME,
"pathname limit %ld exceeded: %d\n", env.pathmax, n);
hd = (char *)xmalloc((n + 1) * sizeof(char));
snprintf(hd, n + 1, "%s/%s", env.home, PATHNAME_HOME);
if (!exists_dir(hd)) {
if (mkdir(hd, S_IRUSR | S_IWUSR | S_IXUSR))
error("could not create directory %s; %s\n", hd,
strerror(errno));
}
xfree(hd);
return 0;
}
/*
* Check if a file exists.
*/
int
exists_file(char *fname)
{
struct stat fs;
if (access(fname, F_OK))
return 0;
stat(fname, &fs);
if (!S_ISREG(fs.st_mode)) {
error("file %s not a regular file\n", fname);
return -1;
}
return 1;
}
/*
* Check if a directory exists.
*/
int
exists_dir(char *dname)
{
struct stat ds;
if (access(dname, F_OK))
return 0;
stat(dname, &ds);
if (!S_ISDIR(ds.st_mode)) {
error("file %s not a directory\n", dname);
return -1;
}
return 1;
}
/*
* Create a file with the specified permissions.
*/
int
create_file(char *fname, mode_t mode)
{
int fd;
fd = 0;
if (!exists_file(fname)) {
fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
if (fd == -1) {
error("could not create file %s; %s\n", fname,
strerror(errno));
return -1;
}
close(fd);
}
return 0;
}
/*
* Get the system's maximum number of bytes in a pathname.
*/
int
get_pathmax(void)
{
int n;
errno = 0;
n = pathconf("/", _PC_PATH_MAX);
if (n == -1 && errno != 0) {
error("getting PATH_MAX limit; %s\n", strerror(errno));
return -1;
}
env.pathmax = n;
return 0;
}
|