From 7bdc45248f10911a1b4478cac934042e4c3a8eb8 Mon Sep 17 00:00:00 2001 From: cherierm Date: Wed, 4 Jun 2008 17:45:41 +0000 Subject: [PATCH] source code used to check if a specified command exists (if the command checking is enabled) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5535 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- tools/tesh2/include/explode.h | 19 +++++++ tools/tesh2/include/is_cmd.h | 18 +++++++ tools/tesh2/src/explode.c | 45 +++++++++++++++++ tools/tesh2/src/is_cmd.c | 94 +++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 tools/tesh2/include/explode.h create mode 100644 tools/tesh2/include/is_cmd.h create mode 100644 tools/tesh2/src/explode.c create mode 100644 tools/tesh2/src/is_cmd.c diff --git a/tools/tesh2/include/explode.h b/tools/tesh2/include/explode.h new file mode 100644 index 0000000000..84c571a6d7 --- /dev/null +++ b/tools/tesh2/include/explode.h @@ -0,0 +1,19 @@ +#ifndef __EXPLODE_H +#define __EXPLODE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +char** +explode(char separator, const char *string); + + +#ifdef __cplusplus +} +#endif + +#endif /* !__EXPLODE_H */ + diff --git a/tools/tesh2/include/is_cmd.h b/tools/tesh2/include/is_cmd.h new file mode 100644 index 0000000000..4c6d743b74 --- /dev/null +++ b/tools/tesh2/include/is_cmd.h @@ -0,0 +1,18 @@ +#ifndef __IS_CMD_H +#define __IS_CMD_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int +is_cmd(char** path, char** builtin, const char* p); + +#ifdef __cplusplus +} +#endif + +#endif /* !__IS_CMD_H */ + diff --git a/tools/tesh2/src/explode.c b/tools/tesh2/src/explode.c new file mode 100644 index 0000000000..fd4a49e99d --- /dev/null +++ b/tools/tesh2/src/explode.c @@ -0,0 +1,45 @@ +#include + +char** +explode(char separator, const char *string) +{ + int pos = 0; + int i, len = 1; + int number = 2; + char **table; + const char* p = string; + + for (i = 0; p[i] != '\0'; i++) + if (p[i] == separator) + number++; + + + table = (char**)calloc(number, sizeof(char*)); + + i = 0; + + while (*p++ != '\0') + { + if(*p == separator) + { + table[i] = (char*)calloc(len + 1, sizeof(char)); + strncpy(table[i], string + pos , len); + pos += len + 1; + len = 0; + i++; + } + else + len++; + } + + if(len > 1) + { + table[i] = (char*)calloc(len + 1, sizeof(char)); + strncpy(table[i], string + pos , len); + } + + + table[++i] = NULL; + + return table; +} diff --git a/tools/tesh2/src/is_cmd.c b/tools/tesh2/src/is_cmd.c new file mode 100644 index 0000000000..fd46515fbb --- /dev/null +++ b/tools/tesh2/src/is_cmd.c @@ -0,0 +1,94 @@ +#include + +#include + +#ifdef WIN32 +static int is_w32_binary(const char* cmd) +{ + DWORD binary_type; + + GetBinaryType(cmd, &binary_type); + + if(SCS_32BIT_BINARY == binary_type || SCS_64BIT_BINARY == binary_type || SCS_64BIT_BINARY == binary_type) + return 1; + + return 0; +} +#endif + +int +is_cmd(char** path, char** builtin, const char* p) +{ + size_t i = 0; + size_t j = 0; + int yes = 0; + + struct stat stat_buff = {0}; + char command[PATH_MAX + 1] = {0}; + char buff[PATH_MAX + 1] = {0}; + + size_t len = strlen(p); + + if(!p) + return EINVAL; + + while(i < len) + { + if(p[i] != ' ' && p[i] != '\t' && p[i] != '>') + command[j++] = p[i]; + else + break; + + i++; + } + + /* check first if it's a shell buitin */ + + if(builtin) + { + for(i = 0; builtin[i] != NULL; i++) + { + if(!strcmp(builtin[i], command)) + return 0; + } + } + + if(stat(command, &stat_buff) || !S_ISREG(stat_buff.st_mode)) + { + if(path) + { + for (i = 0; path[i] != NULL; i++) + { + sprintf(buff,"%s/%s",path[i], command); + + if(!stat(buff, &stat_buff) && S_ISREG(stat_buff.st_mode)) + { + #ifdef WIN32 + if(is_w32_binary(buff)) + yes = 1; + break; + #else + if(!access(buff, X_OK)) + { + yes = 1; + break; + } + #endif + } + } + } + } + else + { + #ifdef WIN32 + if(is_w32_binary(command)) + yes = 1; + #else + if(!access(command, X_OK)) + yes = 1; + #endif + } + + return yes ? 0 : ECMDNOTFOUND; +} + -- 2.20.1