Adding usermode tree
[tpg/acess2.git] / Usermode / Applications / CLIShell_src / lib.c
1 /*\r
2  * AcessOS Shell Version 2\r
3  *\r
4  * See file COPYING for \r
5  */\r
6 #include "header.h"\r
7 \r
8 // === CODE ===\r
9 /**\r
10  * \fn int GeneratePath(char *file, char *base, char *Dest)\r
11  * \brief Generates a path given a base and a filename\r
12  * \param \r
13  */\r
14 int GeneratePath(char *file, char *base, char *Dest)\r
15 {\r
16         char    *pathComps[64];\r
17         char    *tmpStr;\r
18         int             iPos = 0;\r
19         int             iPos2 = 0;\r
20         \r
21         // Parse Base Path\r
22         if(file[0] != '/')\r
23         {\r
24                 pathComps[iPos++] = tmpStr = base+1;\r
25                 while(*tmpStr)\r
26                 {\r
27                         if(*tmpStr++ == '/')\r
28                         {\r
29                                 pathComps[iPos] = tmpStr;\r
30                                 iPos ++;\r
31                         }\r
32                 }\r
33         }\r
34         \r
35         //Print(" GeneratePath: Base Done\n");\r
36         \r
37         // Parse Argument Path\r
38         pathComps[iPos++] = tmpStr = file;\r
39         while(*tmpStr)\r
40         {\r
41                 if(*tmpStr++ == '/')\r
42                 {\r
43                         pathComps[iPos] = tmpStr;\r
44                         iPos ++;\r
45                 }\r
46         }\r
47         pathComps[iPos] = NULL;\r
48         \r
49         //Print(" GeneratePath: Path Done\n");\r
50         \r
51         // Cleanup\r
52         iPos2 = iPos = 0;\r
53         while(pathComps[iPos])\r
54         {\r
55                 tmpStr = pathComps[iPos];\r
56                 // Always Increment iPos\r
57                 iPos++;\r
58                 // ..\r
59                 if(tmpStr[0] == '.' && tmpStr[1] == '.' && (tmpStr[2] == '/' || tmpStr[2] == '\0') )\r
60                 {\r
61                         if(iPos2 != 0)\r
62                                 iPos2 --;\r
63                         continue;\r
64                 }\r
65                 // .\r
66                 if(tmpStr[0] == '.' && (tmpStr[1] == '/' || tmpStr[1] == '\0') )\r
67                 {\r
68                         continue;\r
69                 }\r
70                 // Empty\r
71                 if(tmpStr[0] == '/' || tmpStr[0] == '\0')\r
72                 {\r
73                         continue;\r
74                 }\r
75                 \r
76                 // Set New Position\r
77                 pathComps[iPos2] = tmpStr;\r
78                 iPos2++;\r
79         }\r
80         pathComps[iPos2] = NULL;\r
81         \r
82         // Build New Path\r
83         iPos2 = 1;      iPos = 0;\r
84         Dest[0] = '/';\r
85         while(pathComps[iPos])\r
86         {\r
87                 tmpStr = pathComps[iPos];\r
88                 while(*tmpStr && *tmpStr != '/')\r
89                 {\r
90                         Dest[iPos2++] = *tmpStr;\r
91                         tmpStr++;\r
92                 }\r
93                 Dest[iPos2++] = '/';\r
94                 iPos++;\r
95         }\r
96         if(iPos2 > 1)\r
97                 Dest[iPos2-1] = 0;\r
98         else\r
99                 Dest[iPos2] = 0;\r
100         \r
101         return iPos2;   //Length\r
102 }\r

UCC git Repository :: git.ucc.asn.au