"Fossies" - the Fresh Open Source Software Archive

Member "inotify-tools-3.20.11.0/src/common.c" (13 Nov 2020, 6196 Bytes) of package /linux/privat/inotify-tools-3.20.11.0.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 "common.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.20.2.2_vs_3.20.11.0.

    1 #include "common.h"
    2 #include "../config.h"
    3 
    4 #include <sys/stat.h>
    5 #include <sys/types.h>
    6 
    7 #include <errno.h>
    8 #include <limits.h>
    9 #include <stdio.h>
   10 #include <stdlib.h>
   11 #include <string.h>
   12 #include <unistd.h>
   13 
   14 #include <inotifytools/inotifytools.h>
   15 
   16 #define MAXLEN 4096
   17 #define LIST_CHUNK 1024
   18 
   19 #define resize_if_necessary(count, len, ptr)                                   \
   20     if (count >= len - 1) {                                                    \
   21         len += LIST_CHUNK;                                                     \
   22         ptr = (char const **)realloc(ptr, sizeof(char *) * len);               \
   23     }
   24 
   25 void print_event_descriptions() {
   26     printf("\taccess\t\tfile or directory contents were read\n");
   27     printf("\tmodify\t\tfile or directory contents were written\n");
   28     printf("\tattrib\t\tfile or directory attributes changed\n");
   29     printf("\tclose_write\tfile or directory closed, after being opened in\n"
   30            "\t           \twriteable mode\n");
   31     printf("\tclose_nowrite\tfile or directory closed, after being opened in\n"
   32            "\t           \tread-only mode\n");
   33     printf("\tclose\t\tfile or directory closed, regardless of read/write "
   34            "mode\n");
   35     printf("\topen\t\tfile or directory opened\n");
   36     printf("\tmoved_to\tfile or directory moved to watched directory\n");
   37     printf("\tmoved_from\tfile or directory moved from watched directory\n");
   38     printf("\tmove\t\tfile or directory moved to or from watched directory\n");
   39     printf("\tmove_self\t\tA watched file or directory was moved.\n");
   40     printf("\tcreate\t\tfile or directory created within watched directory\n");
   41     printf("\tdelete\t\tfile or directory deleted within watched directory\n");
   42     printf("\tdelete_self\tfile or directory was deleted\n");
   43     printf("\tunmount\t\tfile system containing file or directory unmounted\n");
   44 }
   45 
   46 int isdir(char const *path) {
   47     static struct stat64 my_stat;
   48 
   49     if (-1 == lstat64(path, &my_stat)) {
   50         if (errno == ENOENT)
   51             return 0;
   52         fprintf(stderr, "Stat failed on %s: %s\n", path, strerror(errno));
   53         return 0;
   54     }
   55 
   56     return S_ISDIR(my_stat.st_mode) && !S_ISLNK(my_stat.st_mode);
   57 }
   58 
   59 FileList construct_path_list(int argc, char **argv, char const *filename) {
   60     FileList list;
   61     list.watch_files = 0;
   62     list.exclude_files = 0;
   63     FILE *file = 0;
   64 
   65     if (filename) {
   66         if (!strcmp(filename, "-")) {
   67             file = stdin;
   68         } else {
   69             file = fopen(filename, "r");
   70         }
   71     }
   72 
   73     int watch_len = LIST_CHUNK;
   74     int exclude_len = LIST_CHUNK;
   75     int watch_count = 0;
   76     int exclude_count = 0;
   77     list.watch_files = (char const **)malloc(sizeof(char *) * watch_len);
   78     list.exclude_files = (char const **)malloc(sizeof(char *) * exclude_len);
   79 
   80     char name[MAXLEN];
   81     while (file && fgets(name, MAXLEN, file)) {
   82         if (name[strlen(name) - 1] == '\n')
   83             name[strlen(name) - 1] = 0;
   84         if (strlen(name) == 0)
   85             continue;
   86         if ('@' == name[0] && strlen(name) == 1)
   87             continue;
   88         if ('@' == name[0]) {
   89             resize_if_necessary(exclude_count, exclude_len, list.exclude_files);
   90             list.exclude_files[exclude_count++] = strdup(&name[1]);
   91         } else {
   92             resize_if_necessary(watch_count, watch_len, list.watch_files);
   93             list.watch_files[watch_count++] = strdup(name);
   94         }
   95     }
   96     if (file && file != stdin)
   97         fclose(file);
   98 
   99     for (int i = 0; i < argc; ++i) {
  100         if (strlen(argv[i]) == 0)
  101             continue;
  102         if ('@' == argv[i][0] && strlen(argv[i]) == 1)
  103             continue;
  104         if ('@' == argv[i][0]) {
  105             resize_if_necessary(exclude_count, exclude_len, list.exclude_files);
  106             list.exclude_files[exclude_count++] = &argv[i][1];
  107         } else {
  108             resize_if_necessary(watch_count, watch_len, list.watch_files);
  109             list.watch_files[watch_count++] = argv[i];
  110         }
  111     }
  112     list.exclude_files[exclude_count] = 0;
  113     list.watch_files[watch_count] = 0;
  114     return list;
  115 }
  116 
  117 void _niceassert(long cond, int line, char const *file, char const *condstr,
  118                  char const *mesg) {
  119     if (cond)
  120         return;
  121 
  122     if (mesg) {
  123         fprintf(stderr, "%s:%d assertion ( %s ) failed: %s\n", file, line,
  124                 condstr, mesg);
  125     } else {
  126         fprintf(stderr, "%s:%d assertion ( %s ) failed.\n", file, line,
  127                 condstr);
  128     }
  129 }
  130 
  131 void warn_inotify_init_error() {
  132     int error = inotifytools_error();
  133     fprintf(stderr, "Couldn't initialize inotify: %s\n", strerror(error));
  134     if (error == EMFILE) {
  135         fprintf(stderr, "Try increasing the value of "
  136                         "/proc/sys/fs/inotify/max_user_instances\n");
  137     }
  138 }
  139 
  140 bool is_timeout_option_valid(long int *timeout, char *o) {
  141     if ((o == NULL) || (*o == '\0')) {
  142         fprintf(stderr, "The provided value is not a valid timeout value.\n"
  143                         "Please specify a long int value.\n");
  144         return false;
  145     }
  146 
  147     char *timeout_end = NULL;
  148     errno = 0;
  149     *timeout = strtol(o, &timeout_end, 10);
  150 
  151     const int err = errno;
  152     if (err != 0) {
  153         if (err == ERANGE) {
  154             // Figure out on which side it overflows.
  155             if (*timeout == LONG_MAX) {
  156                 fprintf(stderr, "The timeout value you provided is "
  157                                 "not in the representable range "
  158                                 "(higher than LONG_MAX).\n");
  159             } else {
  160                 fprintf(stderr, "The timeout value you provided is "
  161                                 "not in the representable range "
  162                                 "(lower than LONG_MIN).\n");
  163             }
  164 
  165         } else {
  166             fprintf(stderr, "Something went wrong with the timeout "
  167                             "value you provided.\n");
  168             fprintf(stderr, "%s\n", strerror(err));
  169         }
  170         return false;
  171     }
  172 
  173     if (*timeout_end != '\0') {
  174         fprintf(stderr, "'%s' is not a valid timeout value.\n"
  175                         "Please specify a long int value.\n",
  176                 o);
  177         return false;
  178     }
  179 
  180     return true;
  181 }