Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert svn logs into ChangeLog (up to r7858 for now)
[simgrid.git] / tools / tesh2 / src / is_cmd.c
1 #include <is_cmd.h>
2
3 #include <explode.h>
4
5 int is_cmd(char **path, char **builtin, const char *p)
6 {
7   size_t i = 0;
8   size_t j = 0;
9   int yes = 0;
10
11   struct stat stat_buff = { 0 };
12   char command[PATH_MAX + 1] = { 0 };
13   char buff[PATH_MAX + 1] = { 0 };
14   size_t len;
15
16   if (!p)
17     return EINVAL;
18
19   len = strlen(p);
20   while (i < len) {
21     if (p[i] != ' ' && p[i] != '\t' && p[i] != '>')
22       command[j++] = p[i];
23     else
24       break;
25
26     i++;
27   }
28
29
30   /* check first if it's a shell buitin */
31
32   if (builtin) {
33     for (i = 0; builtin[i] != NULL; i++) {
34       if (!strcmp(builtin[i], command))
35         return 0;
36     }
37   }
38
39   if (stat(command, &stat_buff) || !S_ISREG(stat_buff.st_mode)) {
40     if (path) {
41       for (i = 0; path[i] != NULL; i++) {
42
43         sprintf(buff, "%s/%s", path[i], command);
44
45         if (!stat(buff, &stat_buff) && S_ISREG(stat_buff.st_mode)) {
46
47           if (!access(buff, X_OK)) {
48             yes = 1;
49             break;
50           }
51         }
52       }
53     }
54   } else {
55
56     if (!access(command, X_OK))
57       yes = 1;
58   }
59
60   return yes ? 0 : ECMDNOTFOUND;
61 }