string.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "util.h"
  2. #include "string.h"
  3. #define K 1024LL
  4. /*
  5. * perf_atoll()
  6. * Parse (\d+)(b|B|kb|KB|mb|MB|gb|GB|tb|TB) (e.g. "256MB")
  7. * and return its numeric value
  8. */
  9. s64 perf_atoll(const char *str)
  10. {
  11. unsigned int i;
  12. s64 length = -1, unit = 1;
  13. if (!isdigit(str[0]))
  14. goto out_err;
  15. for (i = 1; i < strlen(str); i++) {
  16. switch (str[i]) {
  17. case 'B':
  18. case 'b':
  19. break;
  20. case 'K':
  21. if (str[i + 1] != 'B')
  22. goto out_err;
  23. else
  24. goto kilo;
  25. case 'k':
  26. if (str[i + 1] != 'b')
  27. goto out_err;
  28. kilo:
  29. unit = K;
  30. break;
  31. case 'M':
  32. if (str[i + 1] != 'B')
  33. goto out_err;
  34. else
  35. goto mega;
  36. case 'm':
  37. if (str[i + 1] != 'b')
  38. goto out_err;
  39. mega:
  40. unit = K * K;
  41. break;
  42. case 'G':
  43. if (str[i + 1] != 'B')
  44. goto out_err;
  45. else
  46. goto giga;
  47. case 'g':
  48. if (str[i + 1] != 'b')
  49. goto out_err;
  50. giga:
  51. unit = K * K * K;
  52. break;
  53. case 'T':
  54. if (str[i + 1] != 'B')
  55. goto out_err;
  56. else
  57. goto tera;
  58. case 't':
  59. if (str[i + 1] != 'b')
  60. goto out_err;
  61. tera:
  62. unit = K * K * K * K;
  63. break;
  64. case '\0': /* only specified figures */
  65. unit = 1;
  66. break;
  67. default:
  68. if (!isdigit(str[i]))
  69. goto out_err;
  70. break;
  71. }
  72. }
  73. length = atoll(str) * unit;
  74. goto out;
  75. out_err:
  76. length = -1;
  77. out:
  78. return length;
  79. }
  80. /*
  81. * Helper function for splitting a string into an argv-like array.
  82. * originally copied from lib/argv_split.c
  83. */
  84. static const char *skip_sep(const char *cp)
  85. {
  86. while (*cp && isspace(*cp))
  87. cp++;
  88. return cp;
  89. }
  90. static const char *skip_arg(const char *cp)
  91. {
  92. while (*cp && !isspace(*cp))
  93. cp++;
  94. return cp;
  95. }
  96. static int count_argc(const char *str)
  97. {
  98. int count = 0;
  99. while (*str) {
  100. str = skip_sep(str);
  101. if (*str) {
  102. count++;
  103. str = skip_arg(str);
  104. }
  105. }
  106. return count;
  107. }
  108. /**
  109. * argv_free - free an argv
  110. * @argv - the argument vector to be freed
  111. *
  112. * Frees an argv and the strings it points to.
  113. */
  114. void argv_free(char **argv)
  115. {
  116. char **p;
  117. for (p = argv; *p; p++)
  118. free(*p);
  119. free(argv);
  120. }
  121. /**
  122. * argv_split - split a string at whitespace, returning an argv
  123. * @str: the string to be split
  124. * @argcp: returned argument count
  125. *
  126. * Returns an array of pointers to strings which are split out from
  127. * @str. This is performed by strictly splitting on white-space; no
  128. * quote processing is performed. Multiple whitespace characters are
  129. * considered to be a single argument separator. The returned array
  130. * is always NULL-terminated. Returns NULL on memory allocation
  131. * failure.
  132. */
  133. char **argv_split(const char *str, int *argcp)
  134. {
  135. int argc = count_argc(str);
  136. char **argv = zalloc(sizeof(*argv) * (argc+1));
  137. char **argvp;
  138. if (argv == NULL)
  139. goto out;
  140. if (argcp)
  141. *argcp = argc;
  142. argvp = argv;
  143. while (*str) {
  144. str = skip_sep(str);
  145. if (*str) {
  146. const char *p = str;
  147. char *t;
  148. str = skip_arg(str);
  149. t = strndup(p, str-p);
  150. if (t == NULL)
  151. goto fail;
  152. *argvp++ = t;
  153. }
  154. }
  155. *argvp = NULL;
  156. out:
  157. return argv;
  158. fail:
  159. argv_free(argv);
  160. return NULL;
  161. }
  162. /* Character class matching */
  163. static bool __match_charclass(const char *pat, char c, const char **npat)
  164. {
  165. bool complement = false, ret = true;
  166. if (*pat == '!') {
  167. complement = true;
  168. pat++;
  169. }
  170. if (*pat++ == c) /* First character is special */
  171. goto end;
  172. while (*pat && *pat != ']') { /* Matching */
  173. if (*pat == '-' && *(pat + 1) != ']') { /* Range */
  174. if (*(pat - 1) <= c && c <= *(pat + 1))
  175. goto end;
  176. if (*(pat - 1) > *(pat + 1))
  177. goto error;
  178. pat += 2;
  179. } else if (*pat++ == c)
  180. goto end;
  181. }
  182. if (!*pat)
  183. goto error;
  184. ret = false;
  185. end:
  186. while (*pat && *pat != ']') /* Searching closing */
  187. pat++;
  188. if (!*pat)
  189. goto error;
  190. *npat = pat + 1;
  191. return complement ? !ret : ret;
  192. error:
  193. return false;
  194. }
  195. /* Glob/lazy pattern matching */
  196. static bool __match_glob(const char *str, const char *pat, bool ignore_space)
  197. {
  198. while (*str && *pat && *pat != '*') {
  199. if (ignore_space) {
  200. /* Ignore spaces for lazy matching */
  201. if (isspace(*str)) {
  202. str++;
  203. continue;
  204. }
  205. if (isspace(*pat)) {
  206. pat++;
  207. continue;
  208. }
  209. }
  210. if (*pat == '?') { /* Matches any single character */
  211. str++;
  212. pat++;
  213. continue;
  214. } else if (*pat == '[') /* Character classes/Ranges */
  215. if (__match_charclass(pat + 1, *str, &pat)) {
  216. str++;
  217. continue;
  218. } else
  219. return false;
  220. else if (*pat == '\\') /* Escaped char match as normal char */
  221. pat++;
  222. if (*str++ != *pat++)
  223. return false;
  224. }
  225. /* Check wild card */
  226. if (*pat == '*') {
  227. while (*pat == '*')
  228. pat++;
  229. if (!*pat) /* Tail wild card matches all */
  230. return true;
  231. while (*str)
  232. if (__match_glob(str++, pat, ignore_space))
  233. return true;
  234. }
  235. return !*str && !*pat;
  236. }
  237. /**
  238. * strglobmatch - glob expression pattern matching
  239. * @str: the target string to match
  240. * @pat: the pattern string to match
  241. *
  242. * This returns true if the @str matches @pat. @pat can includes wildcards
  243. * ('*','?') and character classes ([CHARS], complementation and ranges are
  244. * also supported). Also, this supports escape character ('\') to use special
  245. * characters as normal character.
  246. *
  247. * Note: if @pat syntax is broken, this always returns false.
  248. */
  249. bool strglobmatch(const char *str, const char *pat)
  250. {
  251. return __match_glob(str, pat, false);
  252. }
  253. /**
  254. * strlazymatch - matching pattern strings lazily with glob pattern
  255. * @str: the target string to match
  256. * @pat: the pattern string to match
  257. *
  258. * This is similar to strglobmatch, except this ignores spaces in
  259. * the target string.
  260. */
  261. bool strlazymatch(const char *str, const char *pat)
  262. {
  263. return __match_glob(str, pat, true);
  264. }