📄 smpd_session.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. */#include "smpd.h"#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#ifdef HAVE_SIGNAL_H#include <signal.h>#endif#ifdef HAVE_STDLIB_H#include <stdlib.h> /* getenv */#endif#ifdef HAVE_STRING_H#include <string.h> /* strrchr */#endif#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifndef HAVE_WINDOWS_Hstatic int exists(char *filename){ struct stat file_stat; if ((stat(filename, &file_stat) < 0) || !(S_ISREG(file_stat.st_mode))) { return 0; /* no such file, or not a regular file */ } return 1;}#endif#undef FCNAME#define FCNAME "smpd_get_full_path_name"SMPD_BOOL smpd_get_full_path_name(const char *exe, int maxlen, char *exe_path, char **namepart){#ifdef HAVE_WINDOWS_H DWORD dwResult; DWORD dwLength; int len; char buffer[SMPD_MAX_EXE_LENGTH]; char info_buffer[sizeof(REMOTE_NAME_INFO) + SMPD_MAX_EXE_LENGTH]; REMOTE_NAME_INFO *info = (REMOTE_NAME_INFO*)info_buffer; char *filename; char temp_name[SMPD_MAX_EXE_LENGTH]; char *temp_exe = NULL; smpd_enter_fn(FCNAME); /* handle the common case of unix programmers specifying executable like this: ./foo */ if (strlen(exe) > 2) { if (exe[0] == '.' && exe[1] == '/') { temp_exe = strdup(exe); temp_exe[1] = '\\'; exe = temp_exe; } smpd_dbg_printf("fixing up exe name: '%s' -> '%s'\n", exe, temp_exe); } /* make a full path out of the name provided */ len = GetFullPathName(exe, maxlen, exe_path, namepart); if (temp_exe != NULL) { free(temp_exe); temp_exe = NULL; } if (len == 0 || len > maxlen) { smpd_err_printf("buffer provided too short for path: %d provided, %d needed\n", maxlen, len); smpd_exit_fn(FCNAME); return SMPD_FALSE; } *(*namepart - 1) = '\0'; /* separate the path from the executable */ /* Verify file exists. If it doesn't search the path for exe */ if ((len = SearchPath(exe_path, *namepart, NULL, SMPD_MAX_EXE_LENGTH, buffer, &filename)) == 0) { if ((len = SearchPath(exe_path, *namepart, ".exe", SMPD_MAX_EXE_LENGTH, buffer, &filename)) == 0) { /* search the default path for an exact match */ if ((len = SearchPath(NULL, *namepart, NULL, SMPD_MAX_EXE_LENGTH, buffer, &filename)) == 0) { /* search the default path for a match + .exe */ if ((len = SearchPath(NULL, *namepart, ".exe", SMPD_MAX_EXE_LENGTH, buffer, &filename)) == 0) { smpd_dbg_printf("path not found. leaving as is in case the path exists on the remote machine.\n"); smpd_exit_fn(FCNAME); return SMPD_TRUE; } } if (len > SMPD_MAX_EXE_LENGTH || len > maxlen) { smpd_err_printf("buffer provided too short for path: %d provided, %d needed\n", maxlen, len); smpd_exit_fn(FCNAME); return SMPD_FALSE; } *(filename - 1) = '\0'; /* separate the file name */ /* copy the path */ strcpy(exe_path, buffer); *namepart = &exe_path[strlen(exe_path)+1]; /* copy the filename */ strcpy(*namepart, filename); } } if (len > maxlen) { smpd_err_printf("buffer provided too short for path: %d provided, %d needed\n", maxlen, len); smpd_exit_fn(FCNAME); return SMPD_FALSE; } /* save the filename */ strcpy(temp_name, *namepart); /* convert the path to its UNC equivalent to avoid need to map a drive */ dwLength = sizeof(REMOTE_NAME_INFO)+SMPD_MAX_EXE_LENGTH; info->lpConnectionName = NULL; info->lpRemainingPath = NULL; info->lpUniversalName = NULL; dwResult = WNetGetUniversalName(exe_path, REMOTE_NAME_INFO_LEVEL, info, &dwLength); if (dwResult == NO_ERROR) { if ((int)(strlen(info->lpUniversalName) + strlen(temp_name) + 2) > maxlen) { smpd_err_printf("buffer provided too short for path: %d provided, %d needed\n", maxlen, strlen(info->lpUniversalName) + strlen(temp_name) + 2); smpd_exit_fn(FCNAME); return SMPD_FALSE; } strcpy(exe_path, info->lpUniversalName); *namepart = &exe_path[strlen(exe_path)+1]; strcpy(*namepart, temp_name); } smpd_exit_fn(FCNAME); return SMPD_TRUE;#else char *path = NULL; char temp_str[SMPD_MAX_EXE_LENGTH] = "./"; smpd_enter_fn(FCNAME); getcwd(temp_str, SMPD_MAX_EXE_LENGTH); if (temp_str[strlen(temp_str)-1] != '/') strcat(temp_str, "/"); /* start with whatever they give you tacked on to the cwd */ snprintf(exe_path, maxlen, "%s%s", temp_str, exe); if (exists(exe_path)) { *namepart = strrchr(exe_path, '/'); **namepart = '\0'; /* separate the path from the executable */ *namepart = *namepart + 1; smpd_exit_fn(FCNAME); return SMPD_TRUE; } *namepart = strrchr(exe_path, '/'); **namepart = '\0'; /* separate the path from the executable */ *namepart = *namepart + 1; /* add searching of the path and verifying file exists */ path = getenv("PATH"); strcpy(temp_str, *namepart); if (smpd_search_path(path, temp_str, maxlen, exe_path)) { *namepart = strrchr(exe_path, '/'); **namepart = '\0'; *namepart = *namepart + 1; smpd_exit_fn(FCNAME); return SMPD_TRUE; } smpd_exit_fn(FCNAME); return SMPD_FALSE;#endif}#undef FCNAME#define FCNAME "smpd_search_path"SMPD_BOOL smpd_search_path(const char *smpd_path, const char *exe, int maxlen, char *str){#ifdef HAVE_WINDOWS_H char *filepart; char *path_spec, *smpd_path2 = NULL, exe_path[SMPD_MAX_EXE_LENGTH]; size_t len_pre, len_spec, len_post; HMODULE hModule; smpd_enter_fn(FCNAME); path_spec = strstr(smpd_path, SMPD_PATH_SPEC); if (path_spec != NULL) { if (smpd_process.pszExe[0] == '\0') { hModule = GetModuleHandle(NULL); if (!GetModuleFileName(hModule, smpd_process.pszExe, SMPD_MAX_EXE_LENGTH)) { strcpy(smpd_process.pszExe, ".\\smpd.exe"); } } smpd_path2 = strrchr(smpd_process.pszExe, '\\'); if (smpd_path2 == NULL) { exe_path[0] = '\0'; } else { memcpy(exe_path, smpd_process.pszExe, smpd_path2 - smpd_process.pszExe); exe_path[smpd_path2 - smpd_process.pszExe] = '\0'; } len_pre = path_spec - smpd_path; len_spec = strlen(exe_path); len_post = strlen(path_spec + strlen(SMPD_PATH_SPEC)); smpd_path2 = (char *)malloc((len_pre + len_spec + len_post + 1) * sizeof(char)); if (len_pre) { memcpy(smpd_path2, smpd_path, len_pre); } memcpy(smpd_path2 + len_pre, exe_path, len_spec); strcpy(smpd_path2 + len_pre + len_spec, path_spec + strlen(SMPD_PATH_SPEC)); smpd_dbg_printf("changed path(%s) to (%s)\n", smpd_path, smpd_path2); smpd_path = smpd_path2; } /* search for exactly what's specified */ if (SearchPath(smpd_path, exe, NULL, maxlen, str, &filepart) == 0) { /* search for file + .exe */ if (SearchPath(smpd_path, exe, ".exe", maxlen, str, &filepart) == 0) { /* search the default path */ if (SearchPath(NULL, exe, NULL, maxlen, str, &filepart) == 0) { /* search the default path + .exe */ if (SearchPath(NULL, exe, ".exe", maxlen, str, &filepart) == 0) { if (smpd_path2 != NULL) { free(smpd_path2); } smpd_exit_fn(FCNAME); return SMPD_FALSE; } } } } if (smpd_path2 != NULL) { free(smpd_path2); } smpd_exit_fn(FCNAME); return SMPD_TRUE;#else char test[SMPD_MAX_EXE_LENGTH]; char path[SMPD_MAX_PATH_LENGTH]; char *token; int n; smpd_enter_fn(FCNAME); if (smpd_path == NULL || exe == NULL || maxlen < 1 || str == NULL) { smpd_exit_fn(FCNAME); return SMPD_FALSE; } strncpy(path, smpd_path, SMPD_MAX_PATH_LENGTH); path[SMPD_MAX_PATH_LENGTH - 1] = '\0'; token = strtok(path, ";:"); while (token) { /* this does not catch the case where SMPD_MAX_EXE_LENGTH is not long enough and the file exists */ if (token[strlen(token)-1] != '/') n = snprintf(test, SMPD_MAX_EXE_LENGTH, "%s/%s", token, exe); else n = snprintf(test, SMPD_MAX_EXE_LENGTH, "%s%s", token, exe); test[SMPD_MAX_EXE_LENGTH-1] = '\0'; if (exists(test))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -