static void attach_process_add_line (AttachProcess *ap, GtkTreeStore *store, gchar *line) { gchar *pid, *user, *start, *command, *tmp; // guint i, level; GtkTreeIter *iter; pid = skip_spaces (line); // skip leading spaces user = skip_token_and_spaces (pid); // skip PID start = skip_token_and_spaces (user); // skip USER tmp = skip_token (start); // skip START (do not skip spaces) command = calc_depth_and_get_iter (ap, store, &iter, tmp); if (ap->hide_paths) { command = skip_path (command); } if (ap->hide_params) { remove_params(command); } gtk_tree_store_set (store, iter, PID_COLUMN, pid, USER_COLUMN, user, START_COLUMN, start, COMMAND_COLUMN, command, -1); }
/* COMMAND "getUser": Extract the user identity from the URL * SIGNATURE: getUser(str) : str; */ str URLgetUser(str *retval, url *val) { const char *s; const char *p; const char *u; if (val == NULL || *val == NULL) throw(ILLARG, "url.getUser", "url missing"); if ((s = skip_scheme(*val)) == NULL || (p = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(p, NULL, NULL)) == NULL) throw(ILLARG, "url.getUser", "bad url"); if (p == s || *p != '/' || p[1] != '~') { *retval = GDKstrdup(str_nil); } else { size_t l; u = p + 2; for (p = u; p < s && *p != '/'; p++) ; l = p - u; if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, u, l); (*retval)[l] = 0; } } if (*retval == NULL) throw(MAL, "url.getUser", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getQuery": Extract the query part from the URL * SIGNATURE: getQuery(str) : str; */ str URLgetQuery(str *retval, url *val) { const char *s; const char *q; if (val == NULL || *val == NULL) throw(ILLARG, "url.getQuery", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (q = skip_path(s, NULL, NULL)) == NULL || (s = skip_search(q)) == NULL) throw(ILLARG, "url.getQuery", "bad url"); if (*q == '?') { size_t l; q++; l = s - q; if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, q, l); (*retval)[l] = 0; } } else { *retval = GDKstrdup(str_nil); } if (*retval == NULL) throw(MAL, "url.getQuery", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getFile": Extract the last file name of the URL * SIGNATURE: getFile(str) : str; */ str URLgetFile(str *retval, url *val) { const char *s; const char *b = NULL; if (val == NULL || *val == NULL) throw(ILLARG, "url.getFile", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(s, &b, NULL)) == NULL) throw(ILLARG, "url.getFile", "bad url"); if (b == NULL) { *retval = GDKstrdup(str_nil); } else { size_t l; l = s - b; if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, b, l); (*retval)[l] = 0; } } if (*retval == NULL) throw(MAL, "url.getFile", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getExtension": Extract the file extension of the URL * SIGNATURE: getExtension(str) : str; */ str URLgetExtension(str *retval, url *val) { const char *s; const char *e = NULL; if (val == NULL || *val == NULL) throw(ILLARG, "url.getExtension", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(s, NULL, &e)) == NULL) throw(ILLARG, "url.getExtension", "bad url"); if (e == NULL) { *retval = GDKstrdup(str_nil); } else { size_t l = s - e; assert(*e == '.'); if ((*retval = GDKmalloc(l)) != NULL) { strncpy(*retval, e + 1, l - 1); (*retval)[l - 1] = 0; } } if (*retval == NULL) throw(MAL, "url.getExtension", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getAnchor": Extract an anchor (reference) from the URL * SIGNATURE: getAnchor(url) : str; */ str URLgetAnchor(str *retval, url *val) { const char *s; if (val == NULL || *val == NULL) throw(ILLARG, "url.getAnchor", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(s, NULL, NULL)) == NULL || (s = skip_search(s)) == NULL) throw(ILLARG, "url.getAnchor", "bad url"); if (*s == '#') s++; else s = str_nil; if ((*retval = GDKstrdup(s)) == NULL) throw(MAL, "url.getAnchor", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getContext": Extract the path context from the URL * SIGNATURE: getContext(str) : str; */ str URLgetContext(str *retval, url *val) { const char *s; const char *p; if (val == NULL || *val == NULL) throw(ILLARG, "url.getContext", "url missing"); if ((s = skip_scheme(*val)) == NULL || (p = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(p, NULL, NULL)) == NULL) throw(ILLARG, "url.getContext", "bad url"); if (p == s) { *retval = GDKstrdup(str_nil); } else if ((*retval = GDKmalloc(s - p + 1)) != NULL) { strncpy(*retval, p, s - p); (*retval)[s - p] = 0; } if (*retval == NULL) throw(MAL, "url.getContext", "Allocation failed"); return MAL_SUCCEED; }
static bool updateDirectory(struct directory *directory, const struct stat *st) { DIR *dir; struct dirent *ent; char *path_fs, *exclude_path_fs; GSList *exclude_list; assert(S_ISDIR(st->st_mode)); directory_set_stat(directory, st); path_fs = map_directory_fs(directory); if (path_fs == NULL) return false; dir = opendir(path_fs); if (!dir) { g_warning("Failed to open directory %s: %s", path_fs, g_strerror(errno)); g_free(path_fs); return false; } exclude_path_fs = g_build_filename(path_fs, ".mpdignore", NULL); exclude_list = exclude_list_load(exclude_path_fs); g_free(exclude_path_fs); g_free(path_fs); if (exclude_list != NULL) remove_excluded_from_directory(directory, exclude_list); removeDeletedFromDirectory(directory); while ((ent = readdir(dir))) { char *utf8; struct stat st2; if (skip_path(ent->d_name) || exclude_list_check(exclude_list, ent->d_name)) continue; utf8 = fs_charset_to_utf8(ent->d_name); if (utf8 == NULL) continue; if (skip_symlink(directory, utf8)) { delete_name_in(directory, utf8); g_free(utf8); continue; } if (stat_directory_child(directory, utf8, &st2) == 0) updateInDirectory(directory, utf8, &st2); else delete_name_in(directory, utf8); g_free(utf8); } exclude_list_free(exclude_list); closedir(dir); directory->mtime = st->st_mtime; return true; }
static void recursive_watch_subdirectories(struct watch_directory *directory, const char *path_fs, unsigned depth) { GError *error = NULL; DIR *dir; struct dirent *ent; assert(directory != NULL); assert(depth <= inotify_max_depth); assert(path_fs != NULL); ++depth; if (depth > inotify_max_depth) return; dir = opendir(path_fs); if (dir == NULL) { g_warning("Failed to open directory %s: %s", path_fs, g_strerror(errno)); return; } while ((ent = readdir(dir))) { char *child_path_fs; struct stat st; int ret; struct watch_directory *child; if (skip_path(ent->d_name)) continue; child_path_fs = g_strconcat(path_fs, "/", ent->d_name, NULL); ret = stat(child_path_fs, &st); if (ret < 0) { g_warning("Failed to stat %s: %s", child_path_fs, g_strerror(errno)); g_free(child_path_fs); continue; } if (!S_ISDIR(st.st_mode)) { g_free(child_path_fs); continue; } ret = mpd_inotify_source_add(inotify_source, child_path_fs, IN_MASK, &error); if (ret < 0) { g_warning("Failed to register %s: %s", child_path_fs, error->message); g_error_free(error); error = NULL; g_free(child_path_fs); continue; } child = tree_find_watch_directory(ret); if (child != NULL) { /* already being watched */ g_free(child_path_fs); continue; } child = g_slice_new(struct watch_directory); child->parent = directory; child->name = g_strdup(ent->d_name); child->descriptor = ret; child->children = NULL; directory->children = g_list_prepend(directory->children, child); tree_add_watch_directory(child); recursive_watch_subdirectories(child, child_path_fs, depth); g_free(child_path_fs); } closedir(dir); }