Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
54d1a2466e6e16a790c029fff783654d8e838b04
[simgrid.git] / tools / tesh2 / src / getpath.c
1 #include <com.h>\r
2 \r
3 /*#include <stdlib.h>\r
4 #include <string.h>\r
5 \r
6 */\r
7 \r
8 #include <sys/types.h>\r
9 #include <sys/stat.h>\r
10 \r
11 #ifndef WIN32\r
12 #include <pwd.h>\r
13 #else\r
14 #endif\r
15 \r
16 #include <errno.h>\r
17 #include <getpath.h>\r
18 \r
19 #ifndef PATH_MAX\r
20 #define PATH_MAX 255\r
21 #endif\r
22 \r
23 #ifndef WIN32\r
24 int\r
25 getpath(const char* file, char** path)\r
26 {\r
27         char buffer1[PATH_MAX + 1] = {0};\r
28         char buffer2[PATH_MAX + 1] = {0};\r
29         char buffer3[PATH_MAX + 1] = {0};       \r
30         char *p1,*p2;\r
31         size_t len = strlen(file);\r
32         char* last_delimiter = NULL;\r
33         struct stat buffer = {0};               \r
34         \r
35         strncpy(buffer1, file, len);    \r
36         \r
37         /* remove the /////// */\r
38         while((p1 = strstr(buffer1, "//"))) \r
39         {\r
40                 if(p1[2]) \r
41                         strcpy(p1, p1 + 1); \r
42                 else \r
43                         p1[1] = '\0';\r
44         }\r
45         \r
46         if(*buffer1 == '~') \r
47         {\r
48                 for(p2 = buffer2, p1 = buffer1 + 1; *p1 && (*p1 != '/'); *p2++ = *p1++);\r
49                         *p2 = '\0';                             \r
50                 \r
51                 if(buffer2[0] == '\0') \r
52                 {\r
53                         char* home = getenv("HOME");\r
54         \r
55                         if(home) \r
56                         {\r
57                                 strcpy(buffer2, home);          \r
58                         } \r
59                         else \r
60                         {\r
61                                 struct passwd* pw = getpwuid(getuid());  \r
62                                 \r
63                                 if(!pw) \r
64                                 {\r
65                                         *path = NULL;\r
66                                         return -1;      \r
67                                 }\r
68                                 \r
69                                 strcpy(buffer2,pw->pw_dir);     \r
70                         }\r
71                         \r
72                         strcat(buffer2, p1);\r
73                 } \r
74                 \r
75         }\r
76         else if (buffer1[0] != '/') \r
77         {\r
78         \r
79                 getcwd(buffer2, PATH_MAX + 1);  \r
80                 \r
81                 if(buffer1[0] == '.' && buffer1[1] == '/') \r
82                 {       /* replace */\r
83                         strcat(buffer2, &buffer1[1]);           \r
84                 } \r
85                 else \r
86                 {       \r
87                         strcat(buffer2, "/");                   \r
88                         strcat(buffer2, buffer1);               \r
89                 }\r
90         } \r
91         else \r
92         {       \r
93                 strcpy(buffer2, buffer1);                       /* copy */\r
94         }\r
95                 \r
96         /*\r
97          * check for /..\r
98          */\r
99         while((p1 = strstr( buffer2, "/.." ))) \r
100         {\r
101                 for( p2 = p1; --p2 > buffer2 && *p2 != '/'; );\r
102                 \r
103                 if (*(p1 + 3)) \r
104                         memmove(p2, p1+3, strlen(p1+3)+1);\r
105                 else \r
106                         *p2 = '\0';\r
107         }\r
108         \r
109         /*\r
110          * try to find links, and resolve them.\r
111          */\r
112         p1 = strtok( buffer2, "/" );\r
113         \r
114         *buffer3 = '\0';\r
115         \r
116         while(p1) \r
117         {\r
118                 strcat( buffer3, "/" );\r
119                 strcat( buffer3, p1 );\r
120                 \r
121                 len = readlink(buffer3, buffer1, PATH_MAX);\r
122                 \r
123                 if (len != -1) \r
124                 {\r
125                         *(buffer1 + len) = '\0';\r
126                         strcpy(buffer3, buffer1 );\r
127                 }\r
128                 \r
129                 p1 = strtok( NULL, "/" );\r
130         }\r
131         \r
132         if(stat(buffer3, &buffer) || !S_ISREG(buffer.st_mode))\r
133         {\r
134                 *path = NULL;\r
135                 errno = ENOENT;\r
136                 return -1;\r
137         }                                                               \r
138                 \r
139         last_delimiter = strrchr(buffer3, '/');\r
140         \r
141         len = strlen(buffer3);\r
142         \r
143         if(last_delimiter)\r
144                 len -=strlen(last_delimiter);                    \r
145         \r
146         *path = (char*) calloc(len + 1, sizeof(char));\r
147         \r
148         if(!(*path))\r
149         {\r
150                 *path = NULL;\r
151                 return -1;\r
152         }\r
153                 \r
154         strncpy(*path, buffer3, len);\r
155 \r
156         return len;\r
157 }\r
158 \r
159 int\r
160 translatepath(const char* totranslate, char** translated)\r
161 {\r
162         char buffer1[PATH_MAX + 1] = {0};               \r
163         char buffer2[PATH_MAX + 1] = {0};\r
164         char buffer3[PATH_MAX + 1] = {0};       \r
165         char *p1,*p2;\r
166         size_t len;     \r
167         struct stat buffer = {0};\r
168         \r
169         len = strlen(totranslate);                                              \r
170         \r
171         strncpy(buffer1, totranslate, len);     \r
172         \r
173         if(!strcmp(buffer1,"."))\r
174         {\r
175                 *translated = getcwd(NULL,0);\r
176                 return strlen(*translated);\r
177         }\r
178         else if(!strcmp(buffer1, "/.."))\r
179         {\r
180                 *translated = strdup("/");\r
181                 return strlen(*translated);\r
182         }\r
183         \r
184         while((p1 = strstr(buffer1, "//"))) \r
185                 if(p1[2]) \r
186                         strcpy(p1, p1 + 1); \r
187                 else \r
188                         p1[1] = '\0';\r
189         \r
190         if (buffer1[0] == '~') \r
191         {\r
192                 for (p2 = buffer2, p1 = buffer1 + 1; *p1 && (*p1 != '/'); *(p2++) = *(p1++));\r
193                 \r
194                 *p2 = '\0';                             \r
195                 \r
196                 if(buffer2[0] == '\0') \r
197                 {\r
198                         char* home = getenv("HOME");\r
199         \r
200                         if(home) \r
201                         {\r
202                                 strcpy(buffer2, home);\r
203                         } \r
204                         else \r
205                         {\r
206                                 struct passwd* pw = getpwuid(getuid());  /* get entry */\r
207                                 \r
208                                 if(!pw)\r
209                                 {\r
210                                         *translated = NULL; \r
211                                         return -1;\r
212                                 }\r
213                                 \r
214                                 strcpy(buffer2,pw->pw_dir);     \r
215                         }\r
216                         \r
217                         strcat(buffer2, p1);\r
218                 } \r
219         }\r
220         else if (*buffer1 != '/') \r
221         {\r
222         \r
223                 getcwd(buffer2, PATH_MAX + 1);\r
224                 \r
225                 if (*buffer1 == '.' && *(buffer1 + 1) == '/') \r
226                 {\r
227                         strcat(buffer2, buffer1+1);             \r
228                 } \r
229                 else \r
230                 {       \r
231                         strcat(buffer2, "/");                   \r
232                         strcat(buffer2, buffer1);               \r
233                 }\r
234         } \r
235         else \r
236         {       \r
237                 strcpy(buffer2, buffer1);                       \r
238         }\r
239         \r
240         /*\r
241          * check for /..\r
242          */\r
243         while((p1 = strstr( buffer2, "/.." ))) \r
244         {\r
245                 for( p2 = p1; --p2 > buffer2 && *p2 != '/'; );\r
246                 \r
247                 if (*(p1 + 3)) \r
248                         memmove(p2, p1+3, strlen(p1+3)+1);\r
249                 else \r
250                         *p2 = '\0';\r
251         }\r
252         \r
253         /*\r
254          * try to find links.\r
255          */\r
256         p1 = strtok( buffer2, "/" );\r
257         \r
258         \r
259         *buffer3 = '\0';\r
260         \r
261         while(p1) \r
262         {\r
263                 strcat( buffer3, "/" );\r
264                 strcat( buffer3, p1 );\r
265                 \r
266                 len = readlink(buffer3, buffer1, PATH_MAX);\r
267                 \r
268                 if (len != -1) \r
269                 {\r
270                         *(buffer1 + len) = '\0';\r
271                         strcpy(buffer3, buffer1 );\r
272                 }\r
273                 \r
274                 p1 = strtok( NULL, "/" );\r
275         }\r
276         \r
277         if (!(*buffer3)) \r
278                 strcpy(buffer3, "/" );          \r
279         \r
280         len = strlen(buffer3);\r
281         \r
282         if(stat(buffer3, &buffer) || !S_ISDIR(buffer.st_mode))\r
283         {\r
284                 *translated = NULL;\r
285                 errno = ENOTDIR;\r
286                 return -1;\r
287         }\r
288                                  \r
289         \r
290         *translated = (char*) calloc(len + 1, sizeof(char));\r
291         \r
292         if(!(*translated))\r
293         {\r
294                 *translated = NULL;\r
295                 return -1;\r
296         }\r
297         \r
298         strncpy(*translated, buffer3, len);\r
299         \r
300         return len;\r
301 }\r
302 #else\r
303 int\r
304 getpath(const char* file, char** path)\r
305 {\r
306         DWORD len;\r
307         char* part = NULL;\r
308         char buffer[PATH_MAX + 1] = {0}; \r
309         struct stat info = {0};\r
310         \r
311         len = GetFullPathName(file, PATH_MAX, buffer, &part );\r
312         \r
313         if(!len)\r
314         {\r
315                 *path = NULL;\r
316                 return -1;\r
317         }\r
318 \r
319         \r
320         if(stat(buffer, &info) || !S_ISREG(info.st_mode))\r
321         {\r
322                 *path = NULL;\r
323                 errno = ENOENT;\r
324                 return -1;\r
325         }               \r
326         \r
327         *path = (char*)calloc(strlen(buffer) - strlen(part), sizeof(char));\r
328 \r
329 \r
330         *path = strncpy(*path, buffer, strlen(buffer) - strlen(part) - 1);\r
331 \r
332         return (int)(strlen(buffer) - strlen(part) -1);\r
333 }\r
334 \r
335 int\r
336 translatepath(const char* totranslate, char** translated)\r
337 {\r
338         char buffer1[PATH_MAX + 1] = {0};               \r
339         char buffer2[PATH_MAX + 1] = {0};\r
340         char *p1;\r
341         int i, j, len;\r
342         \r
343         struct stat stat_buf = {0};\r
344         \r
345         /* return the current directory */\r
346         if(!strcmp(totranslate,".") || !strcmp(totranslate,"./"))\r
347         {\r
348                 *translated = getcwd(NULL,0);\r
349                 return (int)strlen(*translated);\r
350         }\r
351         /* return the previous directory */\r
352         else if(!strcmp(totranslate,"..") || !strcmp(totranslate,"../"))\r
353         {\r
354                 getcwd(buffer1, PATH_MAX + 1);\r
355                 p1 = strrchr(buffer1, '\\');\r
356                 *translated = (char*) calloc((p1 - buffer1) + 1, sizeof(char));\r
357                 strncpy(*translated, buffer1, (p1 - buffer1));\r
358                 \r
359                 return (int)strlen(*translated);\r
360         }\r
361         /* return the root directory */\r
362         else if(!strcmp(totranslate, "/"))\r
363         {\r
364                 *translated = getcwd(NULL,0);\r
365                 (*translated)[2] = '\0';\r
366                 return (int)strlen(*translated);\r
367         }\r
368         /* it's a relative directory name build the full directory name */\r
369         else if(!strchr(totranslate, '/') && !strchr(totranslate, '\\') && !stat(totranslate, &stat_buf) && S_ISDIR(stat_buf.st_mode))\r
370         {\r
371                 getcwd(buffer1, PATH_MAX + 1);\r
372                 strcat(buffer1,"\\");\r
373                 strcat(buffer1,totranslate);\r
374                 \r
375                 *translated = (char*) calloc(strlen(buffer1) + 1, sizeof(char));\r
376                 strcpy(*translated, buffer1);\r
377 \r
378                 return (int)strlen(*translated);\r
379         }\r
380         \r
381         len = (int)strlen(totranslate);                                         \r
382         \r
383         strncpy(buffer1, totranslate, len);     \r
384         \r
385         if(buffer1[strlen(buffer1) - 1] == '/' || buffer1[strlen(buffer1) - 1] == '\\')\r
386                 buffer1[strlen(buffer1) - 1] = '\0';\r
387         \r
388         while((p1 = strstr(buffer1, "//"))) \r
389                 if(p1[2]) \r
390                         strcpy(p1, p1 + 1); \r
391                 else \r
392                         p1[1] = '\0';\r
393         \r
394         for(i = 0, j = 0; buffer1[i] !='\0'; i++)\r
395         {\r
396                 if(buffer1[i] == '/')\r
397                 {\r
398                         j++;\r
399                         \r
400                         if(j > 1)\r
401                                 break;\r
402                 }\r
403         }\r
404         \r
405         if(j == 1 && buffer1[i - 1] == '/')\r
406         {\r
407                 /* perhaps it's a relative directory : `dir/' */\r
408                 strncpy(buffer2, buffer1, strlen(buffer1) - 1);\r
409                 \r
410                 if(!stat(buffer2, &stat_buf) && S_ISDIR(stat_buf.st_mode))\r
411                 {\r
412                         getcwd(buffer1, PATH_MAX + 1);\r
413                         strcat(buffer1,"\\");\r
414                         strcat(buffer1,buffer2);\r
415                         \r
416                         *translated = (char*) calloc(strlen(buffer1) + 1, sizeof(char));\r
417                         strcpy(*translated, buffer1);\r
418 \r
419                         return (int)strlen(*translated);\r
420                 }\r
421                 else\r
422                         memset(buffer2, 0, PATH_MAX + 1);\r
423                 \r
424         }\r
425         \r
426         if(buffer1[0] == '~') \r
427         {\r
428                 /* TODO */\r
429                 *translated = NULL;\r
430                 errno = ENOSYS;\r
431                 return -1;\r
432         }\r
433         else if (*buffer1 == '.') \r
434         {\r
435                 _fullpath(buffer2, buffer1, sizeof(buffer1));   \r
436         } \r
437         else \r
438                 strcpy(buffer2, buffer1);\r
439         \r
440         if(stat(buffer2, &stat_buf) || !S_ISDIR(stat_buf.st_mode))\r
441         {\r
442                 *translated = NULL;\r
443                 errno = ENOTDIR;\r
444                 return -1;\r
445         }\r
446                                  \r
447         len = (int)strlen(buffer2);\r
448         \r
449         *translated = (char*) calloc(len + 1, sizeof(char));\r
450         \r
451         if(!(*translated))\r
452         {\r
453                 *translated = NULL;\r
454                 return -1;\r
455         }\r
456 \r
457         return len;\r
458 }\r
459 #endif\r
460 \r
461 \r