/* * search PATH to confirm the location and access mode of the given command * IN cwd - current working directory * IN cmd - command to execute * IN check_current_dir - if true, search cwd for the command * IN access_mode - required access rights of cmd * IN test_exec - if false, do not confirm access mode of cmd if full path * RET full path of cmd or NULL if not found */ char *search_path(char *cwd, char *cmd, bool check_current_dir, int access_mode, bool test_exec) { List l = NULL; ListIterator i = NULL; char *path, *fullpath = NULL; #if defined HAVE_BG /* BGQ's runjob command required a fully qualified path */ if (((cmd[0] == '.') || (cmd[0] == '/')) && (access(cmd, access_mode) == 0)) { if (cmd[0] == '.') xstrfmtcat(fullpath, "%s/", cwd); xstrcat(fullpath, cmd); goto done; } #else if ((cmd[0] == '.') || (cmd[0] == '/')) { if (test_exec && (access(cmd, access_mode) == 0)) { if (cmd[0] == '.') xstrfmtcat(fullpath, "%s/", cwd); xstrcat(fullpath, cmd); } goto done; } #endif l = _create_path_list(); if (l == NULL) return NULL; if (check_current_dir) list_prepend(l, xstrdup(cwd)); i = list_iterator_create(l); while ((path = list_next(i))) { xstrfmtcat(fullpath, "%s/%s", path, cmd); if (access(fullpath, access_mode) == 0) goto done; xfree(fullpath); } done: FREE_NULL_LIST(l); return fullpath; }
char * search_path(char *cwd, char *cmd, bool check_current_dir, int access_mode) { List l = NULL; ListIterator i = NULL; char *path, *fullpath = NULL; if ( (cmd[0] == '.' || cmd[0] == '/') && (access(cmd, access_mode) == 0 ) ) { if (cmd[0] == '.') xstrfmtcat(fullpath, "%s/", cwd); xstrcat(fullpath, cmd); goto done; } l = _create_path_list(); if (l == NULL) return NULL; if (check_current_dir) list_prepend(l, xstrdup(cwd)); i = list_iterator_create(l); while ((path = list_next(i))) { xstrfmtcat(fullpath, "%s/%s", path, cmd); if (access(fullpath, access_mode) == 0) goto done; xfree(fullpath); fullpath = NULL; } done: if (l) list_destroy(l); return fullpath; }