Error OS_Unix::open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path) { String path = p_path; if (FileAccess::exists(path) && path.is_rel_path()) { // dlopen expects a slash, in this case a leading ./ for it to be interpreted as a relative path, // otherwise it will end up searching various system directories for the lib instead and finally failing. path = "./" + path; } if (!FileAccess::exists(path)) { //this code exists so gdnative can load .so files from within the executable path path = get_executable_path().get_base_dir().plus_file(p_path.get_file()); } if (!FileAccess::exists(path)) { //this code exists so gdnative can load .so files from a standard unix location path = get_executable_path().get_base_dir().plus_file("../lib").plus_file(p_path.get_file()); } p_library_handle = dlopen(path.utf8().get_data(), RTLD_NOW); if (!p_library_handle) { ERR_EXPLAIN("Can't open dynamic library: " + p_path + ". Error: " + dlerror()); ERR_FAIL_V(ERR_CANT_OPEN); } return OK; }
void *fork_restart() { monitorlog(LOG_INFO, "enter fork_restart, and start sleep\n"); char *proc_path, *proc_dir, *proc_name; int len; pthread_t thread_id1, thread_id2, thread_id3, thread_id4; struct proc_info_s *proc_info; proc_info = alloca(sizeof(struct proc_info_s)); memset(proc_info, '\0', sizeof(struct proc_info_s) ); get_executable_path(proc_info); #if 1 pid_t childpid; childpid = fork(); if(childpid == 0 ) { // pthread_create(&thread_id1, NULL, (void *)proc_update, proc_info); pthread_create(&thread_id2, NULL, (void *)proc_restart, proc_info); pthread_create(&thread_id3, NULL, (void *)proc_monitor, proc_info); pthread_create(&thread_id4, NULL, (void *)proc_logcheck, NULL); // pthread_join(thread_id1, NULL); pthread_join(thread_id2, NULL); pthread_join(thread_id3, NULL); pthread_join(thread_id4, NULL); } #endif //proc_update(proc_info); //exit(1); }
char * msys2_get_relocated_path_list(char const * paths) { char win_part[MAX_PATH]; get_executable_path (NULL, &win_part[0], MAX_PATH); strip_n_suffix_folders (&win_part[0], 2); /* 2 because the file name is present. */ char **arr = NULL; size_t count = split_path_list(paths, ':', &arr); int result_size = 1 + (count - 1); /* count - 1 is for ; delim. */ size_t i; for (i = 0; i < count; ++i) { arr[i] = (char *) strip_n_prefix_folders (arr[i], 1); result_size += strlen (arr[i]) + strlen (win_part); } char * result = (char *) malloc (result_size); if (result == NULL) { return NULL; } result[0] = '\0'; for (i = 0; i < count; ++i) { strcat (result, win_part); strcat (result, arr[i]); if (i != count-1) { strcat (result, ";"); } } free ((void*)arr); return result; }
int path_finding(t_tree *current, char **all_path, char *name) { if (sgt_sh()->env == NULL || !current->args || !current->args[0]) return (g_return); if (current->args[0][0] != '.' && current->args[0][0] != '/') { all_path = ft_strsplit(sh_getenv(sgt_sh()->env, "PATH"), ':'); if (all_path == NULL) return (g_return); if ((name = get_executable_path(all_path, current->args[0], 0)) \ && (g_return = 1)) current->full_path = name; ft_strtable_clear(&all_path); } else if ((current->args[0][0] == '.' || current->args[0][0] == '/') && ft_strcmp(current->args[0], "..")) { if (check_access(current->args[0]) != 0 && (g_return = 1)) { if (current->args[0]) current->full_path = ft_strdup(current->args[0]); current->args = update_name(current->args, 0, 0); } } if (!ft_strcmp(current->args[0], "..")) return (-2); return (g_return); }
static struct config_paths_t determine_config_directory_paths(const char *argv0) { struct config_paths_t paths; bool done = false; std::string exec_path = get_executable_path(argv0); if (get_realpath(exec_path)) { debug(2, L"exec_path: '%s'", exec_path.c_str()); if (!done) { // The next check is that we are in a reloctable directory tree const char *installed_suffix = "/bin/fish"; const char *just_a_fish = "/fish"; const char *suffix = NULL; if (has_suffix(exec_path, installed_suffix, false)) { suffix = installed_suffix; } else if (has_suffix(exec_path, just_a_fish, false)) { debug(2, L"'fish' not in a 'bin/', trying paths relative to source tree"); suffix = just_a_fish; } if (suffix) { bool seems_installed = (suffix == installed_suffix); wcstring base_path = str2wcstring(exec_path); base_path.resize(base_path.size() - strlen(suffix)); paths.data = base_path + (seems_installed ? L"/share/fish" : L"/share"); paths.sysconf = base_path + (seems_installed ? L"/etc/fish" : L"/etc"); paths.doc = base_path + (seems_installed ? L"/share/doc/fish" : L"/user_doc/html"); paths.bin = base_path + (seems_installed ? L"/bin" : L""); // Check only that the data and sysconf directories exist. Handle the doc // directories separately. struct stat buf; if (0 == wstat(paths.data, &buf) && 0 == wstat(paths.sysconf, &buf)) { // The docs dir may not exist; in that case fall back to the compiled in path. if (0 != wstat(paths.doc, &buf)) { paths.doc = L"" DOCDIR; } done = true; } } } } if (!done) { // Fall back to what got compiled in. debug(2, L"Using compiled in paths:"); paths.data = L"" DATADIR "/fish"; paths.sysconf = L"" SYSCONFDIR "/fish"; paths.doc = L"" DOCDIR; paths.bin = L"" BINDIR; } debug(2, L"determine_config_directory_paths() results:\npaths.data: %ls\npaths.sysconf: " L"%ls\npaths.doc: %ls\npaths.bin: %ls", paths.data.c_str(), paths.sysconf.c_str(), paths.doc.c_str(), paths.bin.c_str()); return paths; }
int main () { char path[PATH_MAX]; get_executable_path (path, sizeof (path)); printf ("this program is in the directory %s\n", path); return 0; }
/* Do platform-specific initialization. */ void platform_init(int argc, char **argv) { /* Disable stdio output buffering. */ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); #ifdef get_executable_path get_executable_path(); #else strcpy(executable_path, argv[0]); #endif }
char const * msys2_get_relocated_single_path(char const * unix_path) { char * unix_part = (char *) strip_n_prefix_folders (unix_path, 1); char win_part[MAX_PATH]; get_executable_path (NULL, &win_part[0], MAX_PATH); strip_n_suffix_folders (&win_part[0], 2); /* 2 because the file name is present. */ char * new_path = (char *) malloc (strlen (unix_part) + strlen (win_part) + 1); strcpy(new_path, win_part); strcat(new_path, unix_part); return new_path; }
string get_backtrace() { char path[MAX_PATH_LEN] = {'\0'}; char processname[1024] = {'\0'}; get_executable_path(path, processname, sizeof(path)); string processpath = string(path) + string(processname); int nptrs; void *buffer[100]; char **strings; nptrs = backtrace(buffer, 100); strings = backtrace_symbols(buffer, nptrs); string result = get_backtrace_line(nptrs, buffer, processpath.c_str()); free(strings); return result; }
char * single_path_relocation(const char *from, const char *to) { #if defined(__MINGW32__) char exe_path[PATH_MAX]; get_executable_path (NULL, &exe_path[0], sizeof(exe_path)/sizeof(exe_path[0])); if (strrchr (exe_path, '/') != NULL) { strrchr (exe_path, '/')[1] = '\0'; } char * rel_to_datadir = get_relative_path (from, to); strcat (exe_path, rel_to_datadir); simplify_path (&exe_path[0]); return malloc_copy_string(exe_path); #else return malloc_copy_string(to); #endif }
fs::path get_gadgetron_home() { const char *home = std::getenv("GADGETRON_HOME"); if (home != nullptr) { return fs::path(home); } fs::path executable_path = get_executable_path(); GDEBUG_STREAM("Executable path: " << executable_path << std::endl); fs::path gadgetron_home = executable_path .parent_path() .parent_path(); GDEBUG_STREAM("Gadgetron home: " << gadgetron_home << std::endl); return gadgetron_home; }
void exec_utility (command_t c) { // First, find the proper binary. char *exec_bin = get_executable_path (c->u.word[1]); if (exec_bin == NULL) show_error (c->line_number, "Could not find binary to execute", c->u.word[0]); // Next, open any file redirects and replace STDIN and STDOUT if(c->input != NULL) { char *input_path = get_redirect_file_path (c->input); int fd_in = open (input_path, O_RDONLY); free (input_path); if(fd_in == -1) show_error (c->line_number, "Error opening input file", c->input); dup2 (fd_in, STDIN_FILENO); close (fd_in); } if(c->output != NULL) { char *output_path = get_redirect_file_path (c->output); int fd_out = open (c->output, O_WRONLY | O_CREAT | O_TRUNC, // Data from the file will be truncated S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // By default bash in posix mode will create files as rw-r--r-- free (output_path); if(fd_out == -1) show_error (c->line_number, "Error opening output file", c->output); dup2 (fd_out, STDOUT_FILENO); close (fd_out); } // Execute! execvp (exec_bin, c->u.word + 1); // If we got here, there's a problem show_error (c->line_number, "Exec error", NULL); }
const char* get_executable_directory() { static char path[FOUNDATION_MAX_PATH_LENGTH + 1]; static bool path_initialized = false; if (!path_initialized) { filesystem::path executable_path(get_executable_path()); assert(executable_path.has_filename()); executable_path.remove_filename(); assert(executable_path.string().size() <= FOUNDATION_MAX_PATH_LENGTH); strncpy(path, executable_path.string().c_str(), sizeof(path) - 1); path[sizeof(path) - 1] = '\0'; path_initialized = true; } return path; }
/* Make sure that all stdio output of the processes is buffered up. */ int process_start(char* name, process_info_t* p) { FILE* stdout_file = tmpfile(); if (!stdout_file) { perror("tmpfile"); return -1; } p->terminated = 0; p->status = 0; get_executable_path(); pid_t pid = vfork(); if (pid < 0) { perror("vfork"); return -1; } if (pid == 0) { /* child */ dup2(fileno(stdout_file), STDOUT_FILENO); dup2(fileno(stdout_file), STDERR_FILENO); char* args[3] = { executable_path, name, NULL }; execvp(executable_path, args); perror("execvp()"); _exit(127); } /* parent */ p->pid = pid; p->name = strdup(name); p->stdout_file = stdout_file; return 0; }
int main (int argc, char *argv[]) { #ifdef GUI void *status; char path[256]; pthread_t thr_menu; #endif /* * Program setup * Handle user interrupt and alarm * Get program options * Open report file for logging if needed * Initialize UI */ signal(SIGINT, handle_break); signal(SIGALRM, handle_alarm); alarm(1); get_configuration(&opts); if (parse_options(argc, argv, &opts)) { return 1; } if (opts.save_options) { save_configuration(&opts); } if (opts.text_out) { report = fopen(opts.filename, "a"); if(report == NULL) { report = stdout; } } init_idle(); init_winmgmt(&opts); window_state_init(¤t); #ifdef GUI get_executable_path(path, sizeof(path)); init_indicator(argc, argv, path, &opts); if (opts.menu_items != NULL) { add_menu_items(opts.menu_items); } fflush(NULL); pthread_create(&thr_menu, NULL, run_indicator, ¤t.force); #endif if (opts.use_database) { open_database(opts.dbname); } while(poll_continue || current.force) { sleep(1); /* Just wait for program close */ } /* * Clean up */ alarm(1); /* Last hurrah */ sleep(1); #ifdef GUI pthread_join(thr_menu, &status); #endif if (report != stdout && opts.text_out) { fclose(report); } if (opts.use_database) { close_database(); } return 0; }
int main(int argc, char *argv[],char *envp[]) { rd_kafka_t *rks[1024] = { 0 }; int rkcount = 0; char value[1024] = { 0 }; char brokers[1024] = "localhost:9092"; char *broker = NULL; char topic[1024] = "topic"; int sendcnt = 0; int partitions = 4; int opt; int len = 0; char *opbuf = NULL; char config_file[1024] = ""; char path[PATH_MAX] = {0}; char processname[1024] = {0}; get_executable_path(path,processname,sizeof(processname)); snprintf(config_file, sizeof(config_file), "/etc/sendkafka/%s.conf", processname); if (read_config("brokers", value, sizeof(value), config_file) > 0) { strcpy(brokers, value); } if (read_config("topic", value, sizeof(value),config_file) > 0) { strcpy(topic, value); } if (read_config ("partitions", value, sizeof(value), config_file) > 0) { partitions = atoi(value); if (partitions <= 0 || partitions > 256) { partitions = 4; } } if (read_config ("data_path", value, sizeof(value), config_file) > 0) { strcpy(g_queue_data_filepath, value); } if (read_config ("error_path", value, sizeof(value), config_file) > 0) { strcpy(g_error_logpath, value); } if (read_config ("logsavelocal_tag", value, sizeof(value), config_file) > 0) { g_logsavelocal_tag = atoi(value); } if (read_config ("lognum_max", value, sizeof(value), config_file) > 0) { g_logfilenum_max = atoi(value); } if (read_config ("monitor_period", value, sizeof(value), config_file) > 0) { g_monitor_period = atoi(value); } if (read_config ("logsize_max", value, sizeof(value), config_file) > 0) { g_logfilesize_max = atoi(value); } if (read_config ("queue_sizepath", value, sizeof(value), config_file) > 0) { strcpy(g_monitor_qusizelogpath, value); } while ((opt = getopt(argc, argv, "hb:c:d:p:t:o:m:n:l:x:")) != -1) { switch (opt) { case 'b': strncpy(brokers, optarg, sizeof(brokers)); brokers[sizeof(brokers) - 1] = '\0'; break; case 'c': if (read_config("brokers", value, sizeof(value), optarg) > 0) { strcpy(brokers, value); } if (read_config("topic", value, sizeof(value), optarg) > 0) { strcpy(topic, value); } if (read_config ("partitions", value, sizeof(value), optarg) > 0) { partitions = atoi(value); if (partitions <= 0 || partitions > 256) { partitions = 4; } } if (read_config ("data_path", value, sizeof(value), optarg) > 0) { strcpy(g_queue_data_filepath, value); } if (read_config ("queue_sizepath", value, sizeof(value), optarg) > 0) { strcpy(g_monitor_qusizelogpath, value); } if (read_config ("error_path", value, sizeof(value), optarg) > 0) { strcpy(g_error_logpath, value); } if (read_config ("savelocal_tag", value, sizeof(value), optarg) > 0) { g_logsavelocal_tag = atoi(value); } if (read_config ("monitor_period", value, sizeof(value), optarg) > 0) { g_monitor_period = atoi(value); } if (read_config ("lognum_max", value, sizeof(value), optarg) > 0) { g_logfilenum_max = atoi(value); } if (read_config ("logsize_max", value, sizeof(value), optarg) > 0) { g_logfilesize_max = atoi(value); } break; case 'o': if (NULL != optarg) { g_logsavelocal_tag = atoi(optarg); } break; case 't': if (NULL != optarg) { strncpy(topic, optarg, sizeof(topic)); topic[sizeof(topic) - 1] = '\0'; } break; case 'p': if (NULL != optarg) { partitions = atoi(optarg); if (partitions <= 0 || partitions > 256) { partitions = 4; } } break; case 'm': if (NULL != optarg) { g_logfilesize_max = atoi(optarg); } break; case 'l': if (NULL != optarg) { strcpy(g_error_logpath, optarg); } break; case 'd': if (NULL != optarg) { strcpy(g_queue_data_filepath, optarg); } break; case 'x': if (NULL != optarg) { strcpy(g_monitor_qusizelogpath, optarg); } break; case 'n': if (NULL != optarg) { g_logfilenum_max = atoi(optarg); } break; case 'r': if (NULL != optarg) { g_monitor_period = atoi(optarg); } break; case 'h': default: usage(argv[0]); break; } } if(g_logsavelocal_tag == 0){ rd_kafka_set_logger(save_liberr_tolocal); } else{ rd_kafka_set_logger(rd_kafka_log_syslog); } signal(SIGINT, stop); signal(SIGTERM, stop); // see: https://github.com/edenhill/librdkafka/issues/2 signal(SIGPIPE, SIG_IGN); signal(SIGHUP, stop); /* Producer */ char buf[4096]; //int sendcnt = 0; int i = 0; /* Create Kafka handle */ for (broker = strtok(brokers, ","), rkcount = 0; broker && rkcount < sizeof(rks); broker = strtok(NULL, ","), ++rkcount) { rks[rkcount] = rd_kafka_new(RD_KAFKA_PRODUCER, broker, NULL); if (!rks[rkcount]) { for (i = 0; i < rkcount; i++) { rd_kafka_destroy(rks[i]); rks[i] = NULL; } strcpy(buf, getcurrenttime()); buf[strlen(buf) - 1] = '\0'; strcpy(buf, "kafka_new producer is fail..."); perror(buf); strcpy(buf, "kafka_new producer is fail..."); save_error(g_logsavelocal_tag, LOG_CRIT, buf); exit(9); } } FILE *fp = NULL; opbuf = NULL; if (access(g_queue_data_filepath, F_OK) == 0) { fp = fopen(g_queue_data_filepath, "r"); if (fp == NULL) { char buf[100] = { 0 }; sprintf(buf, "%d line open %s file fail...", __LINE__ - 4,g_queue_data_filepath); perror(buf); save_error(g_logsavelocal_tag, LOG_CRIT, buf); exit(8); } while (fgets(buf, sizeof(buf), fp)) { ++sendcnt; opbuf = strdup(buf); len = strlen(opbuf); producer(rks, topic, partitions, RD_KAFKA_OP_F_FREE, opbuf, len, rkcount); } if (get_file_size(g_queue_data_filepath) > 0) { unlink(g_queue_data_filepath); } } if(NULL!=fp) { fclose(fp); } char *eptr = NULL; while (g_run_tag) { eptr = fgets(buf, sizeof(buf), stdin); if (EINTR == errno || NULL == eptr) { g_run_tag = 0; break; } ++sendcnt; opbuf = strdup(buf); len = strlen(opbuf); producer(rks, topic, partitions, RD_KAFKA_OP_F_FREE, opbuf, len, rkcount); if ((sendcnt % 100000) == 0) { char timebuf[50] = { 0 }; strcpy(timebuf, getcurrenttime()); timebuf[strlen(timebuf) - 1] = '\0'; fprintf(stderr, "%s sendkafka[%d]: Sent %i messages to topic %s\n", timebuf, getpid(), sendcnt, topic); char *buf = calloc(1, strlen(topic) + 128); sprintf(buf, "sendkafka[%d]: Sent %i messages to topic %s\n", getpid(), sendcnt, topic); save_error(g_logsavelocal_tag, LOG_INFO, buf); free(buf); buf = NULL; } } printf("sendcnt num %d\n", sendcnt); save_queuedata_tofile(rks, rkcount); /* Destroy the handle */ for (i = 0; i < rkcount; i++) { rd_kafka_destroy(rks[i]); } return 0; }
std::string get_resource_path() { return get_executable_path(); }
void sln_tests_setup() { char *p; get_executable_path(); p = strrchr(executable_path, '/'); memcpy(testdir_path, executable_path, p - executable_path); }
int main(int argc, char *argv[]) { #define BINDIR "/mingw64/bin" #define DATADIR "/mingw64/share" char exe_path[PATH_MAX]; get_executable_path (argv[0], &exe_path[0], sizeof (exe_path) / sizeof (exe_path[0])); printf ("executable path is %s\n", exe_path); char * rel_to_datadir = get_relative_path (BINDIR, DATADIR); if (strrchr (exe_path, '/') != NULL) { strrchr (exe_path, '/')[1] = '\0'; } strcat (exe_path, rel_to_datadir); simplify_path (&exe_path[0]); printf("real path of DATADIR is %s\n", exe_path); if (argc >= 2) { get_relative_path_debug (argv[argc-2], argv[argc-1], 0); } get_relative_path_debug (NULL, NULL, "./"); get_relative_path_debug ("/mingw64/bin", "/mingw64/etc/pkcs11/pkcs11.conf", "../etc/pkcs11/pkcs11.conf"); get_relative_path_debug ("/a/b/c/d", "/a/b/c", ".."); get_relative_path_debug ("/a/b/c/d/", "/a/b/c/", "../"); get_relative_path_debug ("/", "/", "/"); get_relative_path_debug ("/a/testone/c/d", "/a/testtwo/c", "../../../testtwo/c"); get_relative_path_debug ("/a/testone/c/d/", "/a/testtwo/c/", "../../../testtwo/c/"); get_relative_path_debug ("/home/part2/part3/part4", "/work/proj1/proj2", "../../../../work/proj1/proj2"); simplify_path_debug ("a/b/..", "a"); simplify_path_debug ("a/b/c/../../", "a/"); simplify_path_debug ("a/../a/..", ""); simplify_path_debug ("../a/../a/", "../a/"); simplify_path_debug ("./././", "./"); simplify_path_debug ("/test/", "/test/"); simplify_path_debug (".", "."); simplify_path_debug ("..", ".."); simplify_path_debug ("../", "../"); simplify_path_debug ("././.", "."); simplify_path_debug ("../..", "../.."); simplify_path_debug ("/", "/"); simplify_path_debug ("./test/", "./test/"); simplify_path_debug ("./test", "./test"); simplify_path_debug ("/test", "/test"); simplify_path_debug ("../test", "../test"); simplify_path_debug ("../../test", "../../test"); simplify_path_debug ("../test/..", ".."); simplify_path_debug (".././../", "../../"); sanitise_path_debug ("C:\\windows\\path", "C:/windows/path"); sanitise_path_debug ("", ""); sanitise_path_debug ("\\\\", "/"); char const * win_path = X509_get_default_private_dir (); printf ("%s -> %s\n", X509_PRIVATE_DIR, win_path); char * trusts = msys2_get_relocated_path_list (TRUST_PATHS); printf ("%s -> %s\n", TRUST_PATHS, trusts); free ((void*)trusts); char * single = msys2_get_relocated_path_list (SINGLE_PATH_LIST); printf ("%s -> %s\n", SINGLE_PATH_LIST, single); free ((void*)single); char *multi = get_relocated_path_list(BINDIR, TRUST_PATHS); printf ("Source pathlist: %s \n", TRUST_PATHS); printf ("Real pathlist: %s\n", multi); free ((void*)multi); return 0; }
/* * 실행 path 로 각 config path 들을 유추해보고, * 이름으로 유추할 수 없을 때는 */ static struct config_paths_t determine_config_directory_paths(const char *argv0) { struct config_paths_t paths; bool done = false; std::string exec_path = get_executable_path(argv0); if (get_realpath(exec_path)) { #if __APPLE__ /* On OS X, maybe we're an app bundle, and should use the bundle's files. Since we don't link CF, use this lame approach to test it: see if the resolved path ends with /Contents/MacOS/fish, case insensitive since HFS+ usually is. */ if (! done) { const char *suffix = "/Contents/MacOS/fish"; const size_t suffixlen = strlen(suffix); if (has_suffix(exec_path, suffix, true)) { /* Looks like we're a bundle. Cut the string at the / prefixing /Contents... and then the rest */ wcstring wide_resolved_path = str2wcstring(exec_path); wide_resolved_path.resize(exec_path.size() - suffixlen); wide_resolved_path.append(L"/Contents/Resources/"); /* Append share, etc, doc */ paths.data = wide_resolved_path + L"share/fish"; paths.sysconf = wide_resolved_path + L"etc/fish"; paths.doc = wide_resolved_path + L"doc/fish"; /* But the bin_dir is the resolved_path, minus fish (aka the MacOS directory) */ paths.bin = str2wcstring(exec_path); paths.bin.resize(paths.bin.size() - strlen("/fish")); done = true; } } #endif if (! done) { /* The next check is that we are in a reloctable directory tree like this: bin/fish etc/fish share/fish Check it! */ const char *suffix = "/bin/fish"; /* * exec_path 가 suffix 로 끝나면 config path 를 * 이름만으로 유추해서 설정 가능 */ if (has_suffix(exec_path, suffix, false)) { /* * config path 는 wcstring 으로 이루어져있고, * 그 base path 는 /usr/local 과 같이 위의 * exec_path 에서 추출한 것이다. */ wcstring base_path = str2wcstring(exec_path); base_path.resize(base_path.size() - strlen(suffix)); paths.data = base_path + L"/share/fish"; paths.sysconf = base_path + L"/etc/fish"; paths.doc = base_path + L"/share/doc/fish"; paths.bin = base_path + L"/bin"; /* Check only that the data and sysconf directories exist. Handle the doc directories separately */ struct stat buf; if (0 == wstat(paths.data, &buf) && 0 == wstat(paths.sysconf, &buf)) { /* The docs dir may not exist; in that case fall back to the compiled in path */ if (0 != wstat(paths.doc, &buf)) { paths.doc = L"" DOCDIR; } done = true; } } } } /* * 이름에서 유추할 수 없는 경우 Makefile 에서 미리 결정된 * path 를 사용한다. */ if (! done) { /* Fall back to what got compiled in. */ paths.data = L"" DATADIR "/fish"; paths.sysconf = L"" SYSCONFDIR "/fish"; paths.doc = L"" DOCDIR; paths.bin = L"" BINDIR; done = true; } return paths; }
/// The status builtin. Gives various status information on fish. int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv) { wchar_t *cmd = argv[0]; int argc = builtin_count_args(argv); status_cmd_opts_t opts; int optind; int retval = parse_cmd_opts(opts, &optind, argc, argv, parser, streams); if (retval != STATUS_CMD_OK) return retval; if (opts.print_help) { builtin_print_help(parser, streams, cmd, streams.out); return STATUS_CMD_OK; } // If a status command hasn't already been specified via a flag check the first word. // Note that this can be simplified after we eliminate allowing subcommands as flags. if (optind < argc) { status_cmd_t subcmd = str_to_enum(argv[optind], status_enum_map, status_enum_map_len); if (subcmd != STATUS_UNDEF) { if (!set_status_cmd(cmd, opts, subcmd, streams)) { return STATUS_CMD_ERROR; } optind++; } else { streams.err.append_format(BUILTIN_ERR_INVALID_SUBCMD, cmd, argv[1]); return STATUS_INVALID_ARGS; } } // Every argument that we haven't consumed already is an argument for a subcommand. const wcstring_list_t args(argv + optind, argv + argc); switch (opts.status_cmd) { case STATUS_UNDEF: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) if (is_login) { streams.out.append_format(_(L"This is a login shell\n")); } else { streams.out.append_format(_(L"This is not a login shell\n")); } streams.out.append_format( _(L"Job control: %ls\n"), job_control_mode == JOB_CONTROL_INTERACTIVE ? _(L"Only on interactive jobs") : (job_control_mode == JOB_CONTROL_NONE ? _(L"Never") : _(L"Always"))); streams.out.append(parser.stack_trace()); break; } case STATUS_SET_JOB_CONTROL: { if (opts.new_job_control_mode != -1) { // Flag form was used. CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) } else { if (args.size() != 1) { const wchar_t *subcmd_str = enum_to_str(opts.status_cmd, status_enum_map); streams.err.append_format(BUILTIN_ERR_ARG_COUNT2, cmd, subcmd_str, 1, args.size()); return STATUS_INVALID_ARGS; } opts.new_job_control_mode = job_control_str_to_mode(args[0].c_str(), cmd, streams); if (opts.new_job_control_mode == -1) { return STATUS_CMD_ERROR; } } job_control_mode = opts.new_job_control_mode; break; } case STATUS_FEATURES: { print_features(streams); break; } case STATUS_TEST_FEATURE: { if (args.size() != 1) { const wchar_t *subcmd_str = enum_to_str(opts.status_cmd, status_enum_map); streams.err.append_format(BUILTIN_ERR_ARG_COUNT2, cmd, subcmd_str, 1, args.size()); return STATUS_INVALID_ARGS; } const auto *metadata = features_t::metadata_for(args.front().c_str()); if (!metadata) { retval = TEST_FEATURE_NOT_RECOGNIZED; } else { retval = feature_test(metadata->flag) ? TEST_FEATURE_ON : TEST_FEATURE_OFF; } break; } case STATUS_FILENAME: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) const wchar_t *fn = parser.current_filename(); if (!fn) fn = _(L"Standard input"); streams.out.append_format(L"%ls\n", fn); break; } case STATUS_FUNCTION: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) const wchar_t *fn = parser.get_function_name(opts.level); if (!fn) fn = _(L"Not a function"); streams.out.append_format(L"%ls\n", fn); break; } case STATUS_LINE_NUMBER: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) // TBD is how to interpret the level argument when fetching the line number. // See issue #4161. // streams.out.append_format(L"%d\n", parser.get_lineno(opts.level)); streams.out.append_format(L"%d\n", parser.get_lineno()); break; } case STATUS_IS_INTERACTIVE: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = !is_interactive_session; break; } case STATUS_IS_COMMAND_SUB: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = !is_subshell; break; } case STATUS_IS_BLOCK: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = !is_block; break; } case STATUS_IS_BREAKPOINT: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = !is_breakpoint; break; } case STATUS_IS_LOGIN: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = !is_login; break; } case STATUS_IS_FULL_JOB_CTRL: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = job_control_mode != JOB_CONTROL_ALL; break; } case STATUS_IS_INTERACTIVE_JOB_CTRL: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = job_control_mode != JOB_CONTROL_INTERACTIVE; break; } case STATUS_IS_NO_JOB_CTRL: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) retval = job_control_mode != JOB_CONTROL_NONE; break; } case STATUS_STACK_TRACE: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) streams.out.append(parser.stack_trace()); break; } case STATUS_CURRENT_CMD: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) // HACK: Go via the deprecated variable to get the command. const auto var = env_get(L"_"); if (!var.missing_or_empty()) { streams.out.append(var->as_string()); streams.out.push_back(L'\n'); } else { streams.out.append(program_name); streams.out.push_back(L'\n'); } break; } case STATUS_FISH_PATH: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) streams.out.append(str2wcstring(get_executable_path("fish"))); streams.out.push_back(L'\n'); break; } } return retval; }
void execute_simple_command (command_t c, bool pipe_output) { // This is a "blank command" which was probably all newlines as a result of comments being read // No need to do anything further if(c->u.word[0] == NULL) return; // If the command is the keyword `exec`, run the exec utility, which replaces the current process // Control is NOT returned if(strcmp (c->u.word[0], "exec") == 0) exec_utility(c); // First, find the proper binary. char *exec_bin = get_executable_path (c->u.word[0]); if (exec_bin == NULL) show_error (c->line_number, "Could not find binary to execute", c->u.word[0]); // If we found it, let's execute it! // First, create a pipe to handle input/output to the command int pipefd[2], pid; pipe (pipefd); if(c->output == NULL && pipe_output) // Signifies where to read the pipe from c->fd_writing_to = pipefd[PIPE_READ]; else c->fd_writing_to = -1; if ((pid = fork ()) < 0) show_error (c->line_number, "Could not fork.", NULL); if (pid == 0) // Child process { close (pipefd[PIPE_READ]); // Child will never read from this newly opened pipe // File redirects trump pipes, thus we ignore specified pipes if redirects are present if(c->input != NULL) { char *input_path = get_redirect_file_path (c->input); int fd_in = open (input_path, O_RDONLY); free (input_path); if(fd_in == -1) show_error (c->line_number, "Error opening input file", c->input); // dup2 will close STDIN_FILENO for us dup2 (fd_in, STDIN_FILENO); close (fd_in); } else if (c->fd_read_from > -1) { dup2 (c->fd_read_from, STDIN_FILENO); close (c->fd_read_from); } else { // Leave STDIN_FILENO open! } // File redirects trump pipes, thus we ignore specified pipes if redirects are present if(c->output != NULL) { char *output_path = get_redirect_file_path (c->output); int fd_out = open (output_path, O_WRONLY | O_CREAT | O_TRUNC, // Data from the file will be truncated S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // By default bash in posix mode will create files as rw-r--r-- free (output_path); if(fd_out == -1) show_error (c->line_number, "Error opening output file", c->output); dup2 (fd_out, STDOUT_FILENO); close (fd_out); close (pipefd[PIPE_WRITE]); // We already have an output location, we don't need this pipe } else if(pipe_output) { dup2 (pipefd[PIPE_WRITE], STDOUT_FILENO); close (pipefd[PIPE_WRITE]); } else { // Leave STDOUT_FILENO open! close (pipefd[PIPE_WRITE]); } // Execute! execvp (exec_bin, c->u.word); // If we got here, there's a problem show_error (c->line_number, "Execution error", NULL); } else // Parent process { c->pid = pid; // Set the child's PID c->running = true; close (pipefd[PIPE_WRITE]); if(!pipe_output) close (pipefd[PIPE_READ]); } }
int main(int argc, char *argv[]) { char **args = alloca(sizeof(char*) * (argc+12)); int i, j; char execpath[PATH_MAX+1]; char sdkpath[PATH_MAX+1]; char codesign_allocate[64]; char osvermin[64]; char *compiler; char *target; char *sdk; char *cpu; char *osmin; target_info(argv, &target, &compiler); if (!get_executable_path(execpath, sizeof(execpath))) abort(); snprintf(sdkpath, sizeof(sdkpath), "%s/../SDK", execpath); snprintf(codesign_allocate, sizeof(codesign_allocate), "%s-codesign_allocate", target); setenv("CODESIGN_ALLOCATE", codesign_allocate, 1); setenv("IOS_FAKE_CODE_SIGN", "1", 1); env(&sdk, "IOS_SDK_SYSROOT", sdkpath); env(&cpu, "IOS_TARGET_CPU", TARGET_CPU); env(&osmin, "IPHONEOS_DEPLOYMENT_TARGET", OS_VER_MIN); unsetenv("IPHONEOS_DEPLOYMENT_TARGET"); snprintf(osvermin, sizeof(osvermin), "-miphoneos-version-min=%s", osmin); for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-arch")) { cpu = NULL; break; } } i = 0; args[i++] = compiler; args[i++] = "-target"; args[i++] = target; args[i++] = "-isysroot"; args[i++] = sdk; if (cpu) { args[i++] = "-arch"; args[i++] = cpu; } args[i++] = osvermin; args[i++] = "-mlinker-version=253.3"; for (j = 1; j < argc; ++i, ++j) args[i] = argv[j]; args[i] = NULL; printf("GAR: Path: %s \n" "Compiler: %s\n", execpath, compiler); setenv("COMPILER_PATH", execpath, 1); int k; for (k = 0; k < i; k++) printf("Compiler option: %s\n", args[k]); printf("End of Compiler args\n"); execvp(compiler, args); fprintf(stderr, "cannot invoke compiler!\n"); return 1; }
char * get_relocated_path_list(char const * from, char const * to_path_list) { char exe_path[MAX_PATH]; char * temp; get_executable_path (NULL, &exe_path[0], sizeof (exe_path) / sizeof (exe_path[0])); if ((temp = strrchr (exe_path, '/')) != NULL) { temp[1] = '\0'; } char **arr = NULL; /* Ask Alexey why he added this. Are we not 100% sure that we're dealing with unix paths here? */ char split_char = ':'; if (strchr (to_path_list, ';')) { split_char = ';'; } size_t count = split_path_list (to_path_list, split_char, &arr); int result_size = 1 + (count - 1); /* count - 1 is for ; delim. */ size_t exe_path_size = strlen (exe_path); size_t i; /* Space required is: count * (exe_path_size + strlen (rel_to_datadir)) rel_to_datadir upper bound is: (count * strlen (from)) + (3 * num_slashes (from)) + strlen(arr[i]) + 1. .. pathalogically num_slashes (from) is strlen (from) (from = ////////) */ size_t space_required = (count * (exe_path_size + 4 * strlen (from))) + count - 1; for (i = 0; i < count; ++i) { space_required += strlen (arr[i]); } char * scratch = (char *) alloca (space_required); if (scratch == NULL) return NULL; for (i = 0; i < count; ++i) { char * rel_to_datadir = get_relative_path (from, arr[i]); scratch[0] = '\0'; arr[i] = scratch; strcat (scratch, exe_path); strcat (scratch, rel_to_datadir); simplify_path (arr[i]); size_t arr_i_size = strlen (arr[i]); result_size += arr_i_size; scratch = arr[i] + arr_i_size + 1; } char * result = (char *) malloc (result_size); if (result == NULL) { return NULL; } result[0] = '\0'; for (i = 0; i < count; ++i) { strcat (result, arr[i]); if (i != count-1) { #if defined(_WIN32) strcat (result, ";"); #else strcat (result, ":"); #endif } } free ((void*)arr); return result; }
static struct config_paths_t determine_config_directory_paths(const char *argv0) { struct config_paths_t paths; bool done = false; std::string exec_path = get_executable_path(argv0); if (get_realpath(exec_path)) { #if __APPLE__ /* On OS X, maybe we're an app bundle, and should use the bundle's files. Since we don't link CF, use this lame approach to test it: see if the resolved path ends with /Contents/MacOS/fish, case insensitive since HFS+ usually is. */ if (! done) { const char *suffix = "/Contents/MacOS/fish"; const size_t suffixlen = strlen(suffix); if (has_suffix(exec_path, suffix, true)) { /* Looks like we're a bundle. Cut the string at the / prefixing /Contents... and then the rest */ wcstring wide_resolved_path = str2wcstring(exec_path); wide_resolved_path.resize(exec_path.size() - suffixlen); wide_resolved_path.append(L"/Contents/Resources/"); /* Append share, etc, doc */ paths.data = wide_resolved_path + L"share/fish"; paths.sysconf = wide_resolved_path + L"etc/fish"; paths.doc = wide_resolved_path + L"doc/fish"; /* But the bin_dir is the resolved_path, minus fish (aka the MacOS directory) */ paths.bin = str2wcstring(exec_path); paths.bin.resize(paths.bin.size() - strlen("/fish")); done = true; } } #endif if (! done) { /* The next check is that we are in a reloctable directory tree like this: bin/fish etc/fish share/fish Check it! */ const char *suffix = "/bin/fish"; if (has_suffix(exec_path, suffix, false)) { wcstring base_path = str2wcstring(exec_path); base_path.resize(base_path.size() - strlen(suffix)); paths.data = base_path + L"/share/fish"; paths.sysconf = base_path + L"/etc/fish"; paths.doc = base_path + L"/share/doc/fish"; paths.bin = base_path + L"/bin"; struct stat buf; if (0 == wstat(paths.data, &buf) && 0 == wstat(paths.sysconf, &buf)) { done = true; } } } } if (! done) { /* Fall back to what got compiled in. */ paths.data = L"" DATADIR "/fish"; paths.sysconf = L"" SYSCONFDIR "/fish"; paths.doc = L"" DATADIR "/doc/fish"; paths.bin = L"" PREFIX "/bin"; done = true; } return paths; }
static struct config_paths_t determine_config_directory_paths(const char *argv0) { struct config_paths_t paths; bool done = false; std::string exec_path = get_executable_path(argv0); if (get_realpath(exec_path)) { debug(2, L"exec_path: '%s', argv[0]: '%s'", exec_path.c_str(), argv0); // TODO: we should determine program_name from argv0 somewhere in this file #ifdef CMAKE_BINARY_DIR // Detect if we're running right out of the CMAKE build directory if (string_prefixes_string(CMAKE_BINARY_DIR, exec_path.c_str())) { debug(2, "Running out of build directory, using paths relative to CMAKE_SOURCE_DIR:\n %s", CMAKE_SOURCE_DIR); done = true; paths.data = wcstring{L"" CMAKE_SOURCE_DIR} + L"/share"; paths.sysconf = wcstring{L"" CMAKE_SOURCE_DIR} + L"/etc"; paths.doc = wcstring{L"" CMAKE_SOURCE_DIR} + L"/user_doc/html"; paths.bin = wcstring{L"" CMAKE_BINARY_DIR}; } #endif if (!done) { // The next check is that we are in a reloctable directory tree const char *installed_suffix = "/bin/fish"; const char *just_a_fish = "/fish"; const char *suffix = NULL; if (has_suffix(exec_path, installed_suffix, false)) { suffix = installed_suffix; } else if (has_suffix(exec_path, just_a_fish, false)) { debug(2, L"'fish' not in a 'bin/', trying paths relative to source tree"); suffix = just_a_fish; } if (suffix) { bool seems_installed = (suffix == installed_suffix); wcstring base_path = str2wcstring(exec_path); base_path.resize(base_path.size() - std::strlen(suffix)); paths.data = base_path + (seems_installed ? L"/share/fish" : L"/share"); paths.sysconf = base_path + (seems_installed ? L"/etc/fish" : L"/etc"); paths.doc = base_path + (seems_installed ? L"/share/doc/fish" : L"/user_doc/html"); paths.bin = base_path + (seems_installed ? L"/bin" : L""); // Check only that the data and sysconf directories exist. Handle the doc // directories separately. struct stat buf; if (0 == wstat(paths.data, &buf) && 0 == wstat(paths.sysconf, &buf)) { // The docs dir may not exist; in that case fall back to the compiled in path. if (0 != wstat(paths.doc, &buf)) { paths.doc = L"" DOCDIR; } done = true; } } } } if (!done) { // Fall back to what got compiled in. debug(2, L"Using compiled in paths:"); paths.data = L"" DATADIR "/fish"; paths.sysconf = L"" SYSCONFDIR "/fish"; paths.doc = L"" DOCDIR; paths.bin = L"" BINDIR; } debug(2, L"determine_config_directory_paths() results:\npaths.data: %ls\npaths.sysconf: " L"%ls\npaths.doc: %ls\npaths.bin: %ls", paths.data.c_str(), paths.sysconf.c_str(), paths.doc.c_str(), paths.bin.c_str()); return paths; }