/* * Parse a file path and return a structure containing its * components. * * This function takes a file path (e.g. /var/tmp/myexample.txt) * and produces a structure containing points to the path elements. * * typedef struct _PATHINFO_STRUCT { * char* fullpath; * char* filename; * char* dirpath; * char* extension; * int isdir; * } PATHINFO_STRUCT; * * The PATHINFO_STRUCT structure is allocated from the heap, as are * the strings pointed two by its members. This memory must be * returned to the heap by calling pathinfo_release, passing a pointer * to the PATHINFO_STRUCT returned by pathinfo_parse. */ PATHINFO_STRUCT* pathinfo_parse_filepath(const char* pathname) { DQHEADER deque; PATHINFO_STRUCT* pathinfop; char* pathdelim = "/"; char* pathbuff; char* tokenp = NULL; char* token_aheadp = NULL; int isdir = 0; // Get a pathinfo structure pathinfop = calloc(1, sizeof(PATHINFO_STRUCT)); pathinfop->fullpath = strdup(pathname); // Initialize a plenty big deque to hold path components. dq_init(strlen(pathname) + 1, sizeof(char), &deque); // Break the file path into tokens. Copy to a working buffer // so we don't alter path with calls to strtok pathbuff = strdup(pathname); // Check for leading / if (pathbuff[0] == '/') { push_token(&deque, "/"); } isdir = pathinfo_is_dir(pathname); pathinfop->isdir = isdir; // Get first token and look ahead token tokenp = strtok(pathbuff, pathdelim); token_aheadp = strtok(NULL, pathdelim); while (tokenp != NULL) { if (token_aheadp != NULL) { // More tokens in path string push_token(&deque, tokenp); push_token(&deque, "/"); tokenp = token_aheadp; token_aheadp = strtok(NULL, pathdelim); } else { // tokenp is the last token in the sequence if (!isdir) { parse_filename(pathinfop, tokenp); } else { push_token(&deque, tokenp); } tokenp = NULL; } } set_dirpath(pathinfop, &deque); free(pathbuff); return pathinfop; }
int setup_one_watch (const char *dir) { int wd; wd = inotify_add_watch(ifd, dir, MASK); if (wd < 0) { if (errno == EACCES || errno == ENOENT) return -1; die_errno("inotify_add_watch"); } set_dirpath(wd, dir); #ifdef DEBUG fprintf(stderr, "%d: %s\n", wd, dir); #endif return 0; }