Logo AND Algorithmique Numérique Distribuée

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