"Fossies" - the Fresh Open Source Software Archive

Member "imapfilter-2.8.2/src/file.c" (26 Dec 2023, 3077 Bytes) of package /linux/privat/imapfilter-2.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "file.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.8.0_vs_2.8.1.

    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <unistd.h>
    4 #include <string.h>
    5 #include <errno.h>
    6 #include <limits.h>
    7 #include <sys/types.h>
    8 #include <sys/stat.h>
    9 #include <fcntl.h>
   10 
   11 #include "imapfilter.h"
   12 #include "pathnames.h"
   13 
   14 
   15 extern options opts;
   16 extern environment env;
   17 
   18 
   19 /*
   20  * Create imapfilter's home directory.
   21  */
   22 void
   23 create_homedir(void)
   24 {
   25     int n;
   26     char *h, *i;
   27 
   28     h = getenv("HOME");
   29     i = getenv("IMAPFILTER_HOME");
   30 
   31     if (i == NULL)
   32         n = strlen(h ? h : "") + strlen(h ? "/" : "") +
   33             strlen(".imapfilter");
   34     else
   35         n = strlen(i);
   36 
   37     if (env.pathmax != -1 && n > env.pathmax)
   38         fatal(ERROR_PATHNAME,
   39             "pathname limit %ld exceeded: %d\n", env.pathmax, n);
   40 
   41     env.home = (char *)xmalloc((n + 1) * sizeof(char));
   42     if (i == NULL)
   43         snprintf(env.home, n + 1, "%s%s%s", h ? h : "", h ? "/" : "",
   44             ".imapfilter");
   45     else
   46         snprintf(env.home, n + 1, "%s", i);
   47 
   48     if (!exists_dir(env.home)) {
   49         if (mkdir(env.home, S_IRUSR | S_IWUSR | S_IXUSR))
   50             error("could not create directory %s; %s\n", env.home,
   51                 strerror(errno));
   52     }
   53 }
   54 
   55 
   56 /*
   57  * Check if a file exists.
   58  */
   59 int
   60 exists_file(char *fname)
   61 {
   62     struct stat fs;
   63 
   64     if (access(fname, F_OK))
   65         return 0;
   66 
   67     stat(fname, &fs);
   68     if (!S_ISREG(fs.st_mode)) {
   69         return 0;
   70     }
   71 
   72     return 1;
   73 }
   74 
   75 
   76 /*
   77  * Check if a directory exists.
   78  */
   79 int
   80 exists_dir(char *dname)
   81 {
   82     struct stat ds;
   83 
   84     if (access(dname, F_OK))
   85         return 0;
   86 
   87     stat(dname, &ds);
   88     if (!S_ISDIR(ds.st_mode)) {
   89         return 0;
   90     }
   91 
   92     return 1;
   93 }
   94 
   95 
   96 /*
   97  * Create a file with the specified permissions.
   98  */
   99 int
  100 create_file(char *fname, mode_t mode)
  101 {
  102     int fd;
  103 
  104     fd = 0;
  105 
  106     if (!exists_file(fname)) {
  107         fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  108         if (fd == -1) {
  109             error("could not create file %s; %s\n", fname,
  110                 strerror(errno));
  111             return -1;
  112         }
  113         close(fd);
  114     }
  115 
  116     return 0;
  117 }
  118 
  119 
  120 /*
  121  * Get the system's maximum number of bytes in a pathname.
  122  */
  123 int
  124 get_pathmax(void)
  125 {
  126     int n;
  127 
  128     errno = 0;
  129 
  130     n = pathconf("/", _PC_PATH_MAX);
  131 
  132     if (n == -1 && errno != 0) {
  133         error("getting PATH_MAX limit; %s\n", strerror(errno));
  134         return -1;
  135     }
  136     env.pathmax = n;
  137 
  138     return 0;
  139 }
  140 
  141 
  142 /*
  143  * Get the path of a file inside the configuration directory.
  144  */
  145 char *
  146 get_filepath(char *fname)
  147 {
  148     int n;
  149     char *fp;
  150 
  151     n = strlen(env.home) + strlen("/") + strlen(fname);
  152     if (env.pathmax != -1 && n > env.pathmax)
  153         fatal(ERROR_PATHNAME,
  154             "pathname limit %ld exceeded: %d\n", env.pathmax, n);
  155 
  156     fp = (char *)xmalloc((n + 1) * sizeof(char));
  157     snprintf(fp, n + 1, "%s/%s", env.home, fname);
  158 
  159     return fp;
  160 }
  161 
  162 
  163 /*
  164  * Write PID to user specified file.
  165  */
  166 void
  167 write_pidfile(void)
  168 {
  169     FILE *fp;
  170 
  171     if (opts.pidfile == NULL)
  172         return;
  173 
  174     if ((fp = fopen(opts.pidfile, "w")) == NULL) {
  175         error("could not write PID to file %s; %s\n", opts.pidfile, strerror(errno));
  176         return;
  177     }
  178 
  179     fprintf(fp, "%d\n", getpid());
  180     fclose(fp);
  181 }
  182 
  183 
  184 /*
  185  * Delete user specified file, which holds the PID written earlier.
  186  */
  187 void
  188 remove_pidfile(void)
  189 {
  190 
  191     if (opts.pidfile == NULL)
  192         return;
  193 
  194     if (unlink(opts.pidfile) == -1)
  195         error("could not delete PID file %s; %s\n", opts.pidfile, strerror(errno));
  196 }