Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
source code used to check if a specified command exists (if the command checking...
[simgrid.git] / tools / tesh2 / src / is_cmd.c
1 #include <is_cmd.h>
2
3 #include <explode.h>
4
5 #ifdef WIN32
6 static int is_w32_binary(const char* cmd)
7 {
8         DWORD binary_type;
9
10         GetBinaryType(cmd, &binary_type);
11
12         if(SCS_32BIT_BINARY == binary_type || SCS_64BIT_BINARY == binary_type || SCS_64BIT_BINARY == binary_type)
13                 return 1;
14
15         return 0;
16 }
17 #endif
18
19 int
20 is_cmd(char** path, char** builtin, const char* p)
21 {
22         size_t i = 0;
23         size_t j = 0;
24         int yes = 0;
25
26         struct stat stat_buff = {0};
27         char command[PATH_MAX + 1] = {0};
28         char buff[PATH_MAX + 1] = {0};
29         
30         size_t len = strlen(p);
31         
32         if(!p)
33                 return EINVAL;
34         
35         while(i < len)
36         {
37                 if(p[i] != ' ' && p[i] != '\t' && p[i] != '>')
38                         command[j++] = p[i];
39                 else
40                         break;
41                         
42                 i++;
43         }
44         
45         /* check first if it's a shell buitin */
46         
47         if(builtin)
48         {
49                 for(i = 0; builtin[i] != NULL; i++)
50                 {
51                         if(!strcmp(builtin[i], command))
52                                 return 0;
53                 }
54         }
55         
56         if(stat(command, &stat_buff) || !S_ISREG(stat_buff.st_mode))
57         {
58                 if(path)
59                 {
60                         for (i = 0; path[i] != NULL; i++)
61                         {
62                                 sprintf(buff,"%s/%s",path[i], command);
63                                 
64                                 if(!stat(buff, &stat_buff) && S_ISREG(stat_buff.st_mode))
65                                 {
66                                         #ifdef WIN32
67                                         if(is_w32_binary(buff))
68                                                 yes = 1;
69                                                 break;
70                                         #else
71                                         if(!access(buff, X_OK))
72                                         {
73                                                 yes = 1;
74                                                 break;
75                                         }
76                                         #endif
77                                 }
78                         }
79                 }
80         }
81         else
82         {
83                 #ifdef WIN32
84                 if(is_w32_binary(command))
85                         yes = 1;                        
86                 #else
87                 if(!access(command, X_OK))
88                         yes = 1;
89                 #endif
90         }
91                 
92         return yes ? 0 : ECMDNOTFOUND;  
93 }
94