dfs_util.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. +------------------------------------------------------------------------------
  3. | Project : Device Filesystem
  4. +------------------------------------------------------------------------------
  5. | Copyright 2004, 2005 www.fayfayspace.org.
  6. | All rights reserved.
  7. |------------------------------------------------------------------------------
  8. | File : dfs_utl.c, some misc functions of Device FileSystem
  9. |------------------------------------------------------------------------------
  10. | Chang Logs:
  11. | Date Author Notes
  12. | 2005-01-26 ffxz The first version
  13. +------------------------------------------------------------------------------
  14. */
  15. #include <string.h>
  16. #include <dfs_fs.h>
  17. #include <dfs_util.h>
  18. /*
  19. +------------------------------------------------------------------------------
  20. | Function : dir_name
  21. +------------------------------------------------------------------------------
  22. | Description : Gets the directory name in specified path string
  23. |
  24. | Parameters : path, the specified path string
  25. | path_name, return the path name in this parameter
  26. | len, the length of path name
  27. | Returns : the status of path_name:
  28. | 0 , successful
  29. | -1, failed
  30. | For Example :
  31. | path, dir_name
  32. | /, /
  33. | /usr, /
  34. | /usr/lib, /usr
  35. | /usr/lib/, /usr
  36. +------------------------------------------------------------------------------
  37. */
  38. int dir_name(const char* path, char* dirname, int len)
  39. {
  40. int pos;
  41. if ( path[0] == '/' && path[1] == '\0' )
  42. {
  43. *dirname++ = '/';
  44. *dirname = '\0';
  45. return 0;
  46. }
  47. pos = strlen(path);
  48. while ( *(path + pos - 1) =='/' ) pos --;
  49. while ( pos > 0 && *(path + pos - 1) != '/' ) pos --;
  50. while ( pos > 0 && *(path + pos - 1) == '/' ) pos --;
  51. if ( pos > len ) return -1;
  52. if ( pos != 0)
  53. {
  54. memcpy(dirname, path, pos);
  55. *(dirname+pos) = '\0';
  56. }
  57. else
  58. {
  59. *dirname++ = '/';
  60. *dirname = '\0';
  61. }
  62. return 0;
  63. }
  64. /*
  65. +------------------------------------------------------------------------------
  66. | Function : file_name
  67. +------------------------------------------------------------------------------
  68. | Description : Gets the file name in specified path string
  69. |
  70. | Parameters : path, the specified path string
  71. | filename, return the path name in this parameter
  72. | len, the length of file name
  73. | Returns : the status of file_name:
  74. | 0 , successful
  75. | -1, failed
  76. | For Example :
  77. | path, filename
  78. | /, RT_NULL
  79. | /usr, usr
  80. | /usr/lib, lib
  81. | /usr/lib/, lib
  82. +------------------------------------------------------------------------------
  83. */
  84. int file_name(const char* path, char* filename, int len)
  85. {
  86. int pos, size;
  87. if ( path[0] == '/' && path[1] == '\0' )
  88. {
  89. *filename = '\0';
  90. return 0;
  91. }
  92. pos = strlen(path);
  93. while ( *(path + pos -1)=='/' ) pos --;
  94. size = pos;
  95. while ( *(path + pos -1) != '/' && pos > 0 ) pos --;
  96. if ( size - pos > len ) return -1;
  97. else size = size - pos ;
  98. memcpy(filename, path + pos, size);
  99. *(filename+size) = '\0';
  100. return 0;
  101. }
  102. /*
  103. +------------------------------------------------------------------------------
  104. | Function : next_path_name
  105. +------------------------------------------------------------------------------
  106. | Description : Gets the next directory name from specified path string
  107. |
  108. | Parameters : path, the specified path string
  109. | pos, the specified position of path string
  110. | next, return the next path in this parameter
  111. | Returns : the position of next directory item,
  112. | -1, if pos reach in the end of the specified path string
  113. | For Example :
  114. +------------------------------------------------------------------------------
  115. */
  116. int next_dir_name(const char* path, int pos, char* next)
  117. {
  118. const char* q;
  119. if ( pos > strlen(path) || pos < 0|| path == RT_NULL ) return -1;
  120. q = path + pos;
  121. /* check for firt '/' */
  122. while ( *q == '/' ) q++;
  123. if ( *q == '\0' )
  124. {
  125. *next = '\0';
  126. return -1;
  127. }
  128. while ( *q != '/' && *q != '\0' )
  129. {
  130. *next++ = *q++;
  131. }
  132. *next = '\0';
  133. return q - path + 1;
  134. }
  135. /*
  136. +------------------------------------------------------------------------------
  137. | Function : build_fullpath
  138. +------------------------------------------------------------------------------
  139. | Description : make up the full path from directory and filename
  140. |
  141. | Parameters : path, the specified path string
  142. | filename, return the path name in this parameter
  143. | fullpath, the returned full path name
  144. | Returns : null
  145. +------------------------------------------------------------------------------
  146. */
  147. void build_fullpath(const char *directory, const char *filename, char *fullpath)
  148. {
  149. char elem[DFS_PATH_MAX + 1];
  150. int pos, start, len;
  151. len = 0;
  152. /* check parameters */
  153. RT_ASSERT(directory != RT_NULL);
  154. RT_ASSERT(filename != RT_NULL);
  155. RT_ASSERT(fullpath != RT_NULL);
  156. /* copy full of directory */
  157. strncpy(fullpath, directory, DFS_PATH_MAX + 1);
  158. for (pos = 0; filename[pos] != '\0'; )
  159. {
  160. /* strip '//' */
  161. while (filename[pos] == '/') pos++;
  162. /* get the filename element, save to elem array */
  163. for (start = pos; filename[pos] != '/' && filename[pos] != '\0'; pos++)
  164. len = pos - start + 1;
  165. strncpy(elem, filename + start, DFS_PATH_MAX + 1);
  166. /* clear the end of elem */
  167. elem[(len < DFS_PATH_MAX + 1? len : DFS_PATH_MAX + 1)] = '\0';
  168. /* strip '..' */
  169. if (elem[0] == '.' && elem[1] == '.')
  170. {
  171. if (strlen(fullpath) == 0) strcpy(fullpath, ""); /* empty filename */
  172. else if (fullpath[0] == '/' && fullpath[1] !='\0')
  173. {
  174. int i = strlen(fullpath);
  175. while (fullpath[i - 1] == '/') i--;
  176. while (i > 0 && fullpath[i - 1] != '/') i--;
  177. fullpath[i] = '\0';
  178. }
  179. }
  180. /* not '.', copy as normally */
  181. else if (elem[0] != '.')
  182. {
  183. len = strlen(fullpath);
  184. if (len == 0) strncpy(fullpath, elem, DFS_PATH_MAX + 1);
  185. else if (fullpath[len - 1] == '/')
  186. {
  187. if (elem[0] == '/') strncat(fullpath, elem + 1, DFS_PATH_MAX + 1);
  188. else strncat(fullpath, elem, DFS_PATH_MAX + 1);
  189. }
  190. else
  191. {
  192. if (elem[0] == '/') strcat(fullpath, elem);
  193. else
  194. {
  195. strncat(fullpath, "/", DFS_PATH_MAX + 1);
  196. strncat(fullpath + 1, elem, DFS_PATH_MAX + 1);
  197. }
  198. }
  199. }
  200. }
  201. if ( fullpath[0] != '/' && fullpath[(len = strlen(fullpath)) - 1] == '/')
  202. {
  203. fullpath[len - 1] = '\0';
  204. }
  205. }
  206. int str_is_prefix(const char* prefix, const char* str)
  207. {
  208. while ((*prefix) && (*str) && (*prefix == *str))
  209. {
  210. prefix ++;
  211. str ++;
  212. }
  213. if (*prefix == 0) return 0;
  214. return -1;
  215. }
  216. #if !defined(RT_USING_MINILIBC) && !defined(RT_USING_NEWLIB)
  217. #if !defined(__ICCARM__)
  218. char *strrchr(const char *t, int c)
  219. {
  220. register char ch;
  221. register const char *l=0;
  222. ch = c;
  223. for (;;)
  224. {
  225. if ((*t == ch)) l=t;
  226. if ((!*t)) return (char*)l; ++t;
  227. }
  228. }
  229. #endif
  230. #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION / 10000 < 35)
  231. int strncasecmp ( const char* s1, const char* s2, size_t len )
  232. {
  233. register unsigned int x2;
  234. register unsigned int x1;
  235. register const char* end = s1 + len;
  236. while (1)
  237. {
  238. if ((s1 >= end) )
  239. return 0;
  240. x2 = *s2 - 'A'; if ((x2 < 26u)) x2 += 32;
  241. x1 = *s1 - 'A'; if ((x1 < 26u)) x1 += 32;
  242. s1++; s2++;
  243. if ((x2 != x1))
  244. break;
  245. if ((x1 == (unsigned int)-'A'))
  246. break;
  247. }
  248. return x1 - x2;
  249. }
  250. #endif /* end of __ARMCC_VERSION */
  251. #endif