strutil.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. #include "precompile.h"
  2. #include <float.h>
  3. #include <errno.h>
  4. #include <ctype.h>
  5. #include <stdarg.h>
  6. #include <malloc.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include "strutil.h"
  10. #include <winpr/wtypes.h>
  11. #include <winpr/string.h>
  12. #pragma warning(disable : 4311)
  13. TOOLKIT_API const char *memstr(const char *buf, int n, const char *str)
  14. {
  15. const char *p, *e;
  16. size_t len;
  17. assert(buf);
  18. assert(str);
  19. len = strlen(str);
  20. for (p = buf, e = buf+n-len; p <= e; ++p) {
  21. if (memcmp(p, str, len) == 0)
  22. return p;
  23. }
  24. return NULL;
  25. }
  26. TOOLKIT_API char *memstr1(char *buf, int n, const char *str)
  27. {
  28. char *p, *e;
  29. size_t len;
  30. assert(buf);
  31. assert(str);
  32. len = strlen(str);
  33. for (p = buf, e = buf+n-len; p <= e; ++p) {
  34. if (memcmp(p, str, len) == 0)
  35. return p;
  36. }
  37. return NULL;
  38. }
  39. TOOLKIT_API const char *memrstr(const char *buf, int n, const char *str)
  40. {
  41. const char *p, *e;
  42. size_t len;
  43. assert(buf);
  44. assert(str);
  45. len = strlen(str);
  46. for (p = buf, e = buf+n-len; e >= p; e--) {
  47. if (memcmp(e, str, len) == 0)
  48. return e;
  49. }
  50. return NULL;
  51. }
  52. TOOLKIT_API const char *memistr(const char *buf, int n, const char *str)
  53. {
  54. const char *p, *e;
  55. size_t len;
  56. assert(buf);
  57. assert(str);
  58. len = strlen(str);
  59. for (p = buf, e = buf+n-len; p <= e; ++p) {
  60. if (_memicmp(p, str, len) == 0)
  61. return p;
  62. }
  63. return NULL;
  64. }
  65. TOOLKIT_API const char *memristr(const char *buf, int n, const char *str)
  66. {
  67. const char *p, *e;
  68. size_t len;
  69. assert(buf);
  70. assert(str);
  71. len = strlen(str);
  72. for (p = buf, e = buf+n-len; e >= p; e--) {
  73. if (_memicmp(e, str, len) == 0)
  74. return e;
  75. }
  76. return NULL;
  77. }
  78. /* Naive implementation of memmem() */
  79. TOOLKIT_API void *memmem(const void *haystack, size_t haystacklen,
  80. const void *needle, size_t needlelen)
  81. {
  82. size_t i;
  83. char const *hs = haystack;
  84. if (needlelen == 0)
  85. return (void *)haystack;
  86. if (needlelen > haystacklen || haystack == NULL || needle == NULL)
  87. return NULL;
  88. for (i = 0; i <= haystacklen - needlelen; i++) {
  89. if (memcmp(hs + i, needle, needlelen) == 0)
  90. return (void *)(hs + i);
  91. }
  92. return NULL;
  93. }
  94. /* Naive implementation of strcasestr() */
  95. TOOLKIT_API char *strcasestr(const char *haystack,
  96. const char *needle)
  97. {
  98. unsigned char lcn, ucn;
  99. unsigned i;
  100. if (haystack == NULL || needle == NULL)
  101. return NULL;
  102. lcn = ucn = needle[0];
  103. if (isupper(lcn))
  104. lcn = tolower(lcn);
  105. else if (islower(ucn))
  106. ucn = toupper(ucn);
  107. if (lcn == 0)
  108. return (char *)haystack;
  109. while (haystack[0] != 0) {
  110. if (lcn == haystack[0] || ucn == haystack[0]) {
  111. for (i = 1; ; i++) {
  112. char n = needle[i], h = haystack[i];
  113. if (n == 0)
  114. return (char *)haystack;
  115. if (h == 0)
  116. return NULL;
  117. if (isupper(n)) n = tolower(n);
  118. if (isupper(h)) h = tolower(h);
  119. if (n != h)
  120. break;
  121. }
  122. }
  123. haystack++;
  124. }
  125. return NULL; /* Not found */
  126. }
  127. TOOLKIT_API char *strltrim(char *s, const char *trims)
  128. {
  129. char *k = s;
  130. char *p;
  131. while (strchr(trims, *k))
  132. ++k;
  133. if (k != s)
  134. for (p = s; *p++ = *k++;);
  135. return s;
  136. }
  137. TOOLKIT_API char *strrtrim(char *s, const char *trims)
  138. {
  139. size_t n = strlen(s);
  140. while (n && strchr(trims, s[n-1]))
  141. n--;
  142. s[n] = 0;
  143. return s;
  144. }
  145. TOOLKIT_API char *strtrim(char *s, const char *trims)
  146. {
  147. return strltrim(strrtrim(s, trims), trims);
  148. }
  149. TOOLKIT_API char *strnormws(char *s)
  150. {
  151. char *in = s, *out = s;
  152. int ch;
  153. while (isspace((ch = *in++)))
  154. ;
  155. if (ch != '\0')
  156. for (;;) {
  157. *out++ = ch;
  158. ch = *in++;
  159. if (ch == '\0')
  160. break;
  161. if (isspace(ch)) {
  162. while(isspace((ch = *in++)))
  163. ;
  164. if (ch == '\0')
  165. break;
  166. *out++ = ' ';
  167. }
  168. }
  169. *out = '\0';
  170. return s;
  171. }
  172. #define ONES_WORD 0x01010101
  173. #define EIGHTS_WORD 0x80808080
  174. #define word_has_nullbyte(w) (((w) - 0x01010101) & ~(w) & EIGHTS_WORD)
  175. TOOLKIT_API char *stpcpy(char *dst, const char *src)
  176. {
  177. const int *p = (const int *)src;
  178. int *q = (int*)dst;
  179. const char *sp;
  180. char *sq;
  181. if (!((int)p&(sizeof(int)-1)) && !((int)q&(sizeof(int)-1))) {
  182. int x = *p;
  183. while (!word_has_nullbyte(x)) {
  184. *q++ = x;
  185. x = *++p;
  186. }
  187. }
  188. sp = (const char*)p;
  189. sq = (char*)q;
  190. while (*sq = *sp++)
  191. sq++;
  192. return sq;
  193. }
  194. TOOLKIT_API char * strreplace(const char *src, const char *old, const char *news)
  195. {
  196. # define SUBS_TOP 32
  197. const char *subs[SUBS_TOP], *p, *sub_start, *src_without_old;
  198. char *dest, *ret;
  199. size_t old_len, new_len, dest_size, tmp;
  200. int i;
  201. old_len = strlen(old);
  202. new_len = strlen(news);
  203. /* Find substrings and compute size of result */
  204. dest_size = 1;
  205. src_without_old = src;
  206. for (i = 0, p = src ;; p = sub_start + old_len) {
  207. sub_start = strstr(p, old);
  208. if (i < SUBS_TOP)
  209. subs[i++] = sub_start;
  210. if (sub_start == NULL)
  211. break;
  212. dest_size += new_len;
  213. if (dest_size < new_len) {
  214. return NULL;
  215. }
  216. src_without_old += old_len;
  217. }
  218. tmp = (p - src_without_old) + strlen(p);
  219. dest_size += tmp;
  220. if (dest_size < tmp) {
  221. return NULL;
  222. }
  223. /* Make result string */
  224. ret = malloc(dest_size);
  225. if (ret) {
  226. dest = ret;
  227. for (i = 0;; ) {
  228. p = (i < SUBS_TOP ? subs[i++] : strstr(src, old));
  229. if (p == NULL)
  230. break;
  231. tmp = p - src;
  232. memcpy(dest, src, tmp);
  233. src += tmp + old_len;
  234. dest = stpcpy(dest + tmp, news);
  235. }
  236. strcpy(dest, src);
  237. }
  238. return ret;
  239. }
  240. TOOLKIT_API char **strsplit(const char *s, const char *delim)
  241. {
  242. const char *sp;
  243. char *p, **ret, **rp;
  244. size_t i;
  245. /* skip to first substring */
  246. s += strspn(s, delim);
  247. /* allocate array */
  248. for (i = 1, sp = s; *sp != '\0'; i++) {
  249. sp += strcspn(sp, delim);
  250. sp += strspn(sp, delim);
  251. }
  252. ret = rp = malloc(i * sizeof(char *));
  253. if (ret != NULL) {
  254. while (*s != '\0') {
  255. /* found new substring */
  256. i = strcspn(s, delim);
  257. *rp++ = p = malloc(i + 1);
  258. if (p == NULL) {
  259. strfreev(ret);
  260. return NULL;
  261. }
  262. memcpy(p, s, i);
  263. p[i] = '\0';
  264. s += i;
  265. s += strspn(s, delim);
  266. }
  267. *rp = NULL;
  268. }
  269. return ret;
  270. }
  271. TOOLKIT_API void strfreev(char **strings)
  272. {
  273. if (strings) {
  274. char **p;
  275. for (p = strings; *p != NULL; ++p)
  276. free(*p);
  277. free(strings);
  278. }
  279. }
  280. TOOLKIT_API char *strsep(char **stringp, const char * delim)
  281. {
  282. char *str, *end;
  283. str = *stringp + strspn(*stringp, delim);
  284. if (*str == '\0') {
  285. end = str;
  286. str = NULL;
  287. } else {
  288. end = str + strcspn(str, delim);
  289. if (*end != '\0')
  290. *end++ = '\0';
  291. }
  292. *stringp = end;
  293. return str;
  294. }
  295. TOOLKIT_API char *strtok_r(char *str, const char * delim, char **tracker)
  296. {
  297. if (str != NULL)
  298. *tracker = str;
  299. return strsep(tracker, delim);
  300. }
  301. TOOLKIT_API char *strsub(char *dest,char *src,size_t offset,size_t len)
  302. {
  303. *dest = '\0';
  304. if (!memchr(src, '\0', offset))
  305. strncat(dest, src + offset, len);
  306. return dest;
  307. }
  308. TOOLKIT_API char *strleft(char *dest, char *src, size_t len)
  309. {
  310. *dest = '\0';
  311. strncat(dest, src, len);
  312. return dest;
  313. }
  314. TOOLKIT_API char *strright(char *dest, char *src, size_t len)
  315. {
  316. size_t src_len;
  317. src_len = strlen(src);
  318. if(src_len > len)
  319. src += src_len - len;
  320. return strcpy(dest, src);
  321. }
  322. static char * TOOLKIT_CC __strallocv(int stack, const char *arg1, va_list arg_list)
  323. {
  324. size_t len;
  325. const char *arg;
  326. char *ret, *end;
  327. int bad;
  328. va_list ap;
  329. /* compute length of result string */
  330. len = 1;
  331. bad = 0;
  332. //TODO: need to test its validity
  333. #ifdef _WIN32
  334. /*gcc compile error: assignment to expression with array type*/
  335. ap = arg_list;
  336. #else
  337. va_copy(ap, arg_list);
  338. #endif
  339. for (arg = arg1; arg != (char *)0; arg = va_arg(ap, char *)) {
  340. size_t arglen = strlen(arg);
  341. len += arglen;
  342. if(len < arglen) {
  343. /* string length too large for size_t */
  344. bad = 1;
  345. break;
  346. }
  347. }
  348. if(bad) {
  349. return NULL;
  350. }
  351. /* create result string */
  352. if (!stack)
  353. ret = malloc(len);
  354. else
  355. ret = _alloca(len);
  356. if(ret != NULL) {
  357. end = ret;
  358. *end = '\0';
  359. #ifdef _WIN32
  360. /*gcc compile error: assignment to expression with array type*/
  361. ap = arg_list;
  362. #else
  363. va_copy(ap, arg_list);
  364. #endif
  365. for (arg = arg1; arg != (char *)0; arg = va_arg(ap, char *))
  366. end = stpcpy(end, arg);
  367. }
  368. return ret;
  369. }
  370. TOOLKIT_API char * TOOLKIT_CC stralloc(const char *arg1, ...)
  371. {
  372. char *r;
  373. va_list ap;
  374. va_start(ap, arg1);
  375. r = __strallocv(0, arg1, ap);
  376. va_end(ap);
  377. return r;
  378. }
  379. TOOLKIT_API char * TOOLKIT_CC stralloca(const char *arg1, ...)
  380. {
  381. char *r;
  382. va_list ap;
  383. va_start(ap, arg1);
  384. r = __strallocv(1, arg1, ap);
  385. va_end(ap);
  386. return r;
  387. }
  388. TOOLKIT_API void *memdup(const void *buf, int len)
  389. {
  390. void *ret;
  391. if (buf) {
  392. ret = malloc(len);
  393. if (ret) {
  394. memcpy(ret, buf, len);
  395. }
  396. } else {
  397. ret = NULL;
  398. }
  399. return ret;
  400. }
  401. TOOLKIT_API void *memdupa(const void *buf, int len)
  402. {
  403. void *ret;
  404. if (buf) {
  405. ret = _alloca(len);
  406. if (ret) {
  407. memcpy(ret, buf, len);
  408. }
  409. } else {
  410. ret = NULL;
  411. }
  412. return ret;
  413. }
  414. TOOLKIT_API char *strdupa(const char * s)
  415. {
  416. size_t size;
  417. char *p;
  418. assert(s);
  419. size = strlen(s) + 1;
  420. if (size == 0)
  421. p = NULL;
  422. else if ((p = _alloca(size)) != NULL)
  423. memcpy(p, s, size);
  424. return p;
  425. }
  426. #ifdef _WIN32
  427. TOOLKIT_API char *strndup(const char *str, int n)
  428. {
  429. char *new_str;
  430. if (str) {
  431. new_str = (char*)malloc(n+1);
  432. strncpy(new_str, str, n);
  433. new_str[n] = '\0';
  434. } else {
  435. new_str = NULL;
  436. }
  437. return new_str;
  438. }
  439. #endif //_WIN32
  440. TOOLKIT_API char* strnfill(int length, int fill_char)
  441. {
  442. char *str;
  443. str = (char*)malloc(length + 1);
  444. memset (str, fill_char, length);
  445. str[length] = '\0';
  446. return str;
  447. }
  448. TOOLKIT_API int strcmp0 (const char *str1,const char *str2)
  449. {
  450. if (!str1)
  451. return -(str1 != str2);
  452. if (!str2)
  453. return str1 != str2;
  454. return strcmp (str1, str2);
  455. }
  456. TOOLKIT_API char* TOOLKIT_CC strdup_printf(const char *format, ...)
  457. {
  458. char *buffer;
  459. va_list args;
  460. va_start(args, format);
  461. buffer = strdup_vprintf(format, args);
  462. va_end(args);
  463. return buffer;
  464. }
  465. static char *vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args);
  466. TOOLKIT_API char* TOOLKIT_CC strdup_vprintf(const char *format, va_list args)
  467. {
  468. size_t length;
  469. return vasnprintf (NULL, &length, format, args);
  470. }
  471. TOOLKIT_API size_t strlcpy(char *dest, const char *src, size_t size)
  472. {
  473. const char *start;
  474. start = src;
  475. if (size) {
  476. while (--size && *src)
  477. *dest++ = *src++;
  478. *dest = '\0';
  479. }
  480. while (*src)
  481. ++src;
  482. return src - start;
  483. }
  484. TOOLKIT_API size_t strlcat(char * dest, const char *src, size_t size)
  485. {
  486. size_t len;
  487. char *end;
  488. for (end = dest; *end; ++end) ;
  489. len = end - dest;
  490. return len + strlcpy(end, src, size > len ? size - len : 0);
  491. }
  492. TOOLKIT_API char** strdupv (char **str_array)
  493. {
  494. if (str_array)
  495. {
  496. int i;
  497. char **retval;
  498. i = 0;
  499. while (str_array[i])
  500. ++i;
  501. retval = (char**)malloc(i+1);
  502. i = 0;
  503. while (str_array[i])
  504. {
  505. retval[i] = _strdup (str_array[i]);
  506. ++i;
  507. }
  508. retval[i] = NULL;
  509. return retval;
  510. }
  511. else
  512. return NULL;
  513. }
  514. TOOLKIT_API char* strjoinv (const char *separator,char **str_array)
  515. {
  516. char *string;
  517. char *ptr;
  518. if (separator == NULL)
  519. separator = "";
  520. if (*str_array)
  521. {
  522. int i;
  523. size_t len;
  524. size_t separator_len;
  525. separator_len = strlen (separator);
  526. /* First part, getting length */
  527. len = 1 + strlen (str_array[0]);
  528. for (i = 1; str_array[i] != NULL; i++)
  529. len += strlen (str_array[i]);
  530. len += separator_len * (i - 1);
  531. /* Second part, building string */
  532. string = (char*)malloc(len);
  533. ptr = stpcpy (string, *str_array);
  534. for (i = 1; str_array[i] != NULL; i++)
  535. {
  536. ptr = stpcpy (ptr, separator);
  537. ptr = stpcpy (ptr, str_array[i]);
  538. }
  539. }
  540. else
  541. string = _strdup ("");
  542. return string;
  543. }
  544. TOOLKIT_API char* TOOLKIT_CC strjoin (const char *separator, ...)
  545. {
  546. char *string, *s;
  547. va_list args;
  548. size_t len;
  549. size_t separator_len;
  550. char *ptr;
  551. if (separator == NULL)
  552. separator = "";
  553. separator_len = strlen (separator);
  554. va_start (args, separator);
  555. s = va_arg (args, char*);
  556. if (s)
  557. {
  558. /* First part, getting length */
  559. len = 1 + strlen (s);
  560. s = va_arg (args, char*);
  561. while (s)
  562. {
  563. len += separator_len + strlen (s);
  564. s = va_arg (args, char*);
  565. }
  566. va_end (args);
  567. /* Second part, building string */
  568. string = (char*)malloc(len);
  569. va_start (args, separator);
  570. s = va_arg (args, char*);
  571. ptr = stpcpy (string, s);
  572. s = va_arg (args, char*);
  573. while (s)
  574. {
  575. ptr = stpcpy (ptr, separator);
  576. ptr = stpcpy (ptr, s);
  577. s = va_arg (args, char*);
  578. }
  579. }
  580. else
  581. string = _strdup ("");
  582. va_end (args);
  583. return string;
  584. }
  585. TOOLKIT_API int str_has_suffix (const char *str, const char *suffix)
  586. {
  587. size_t str_len;
  588. size_t suffix_len;
  589. str_len = strlen (str);
  590. suffix_len = strlen (suffix);
  591. if (str_len < suffix_len)
  592. return FALSE;
  593. return strcmp (str + str_len - suffix_len, suffix) == 0;
  594. }
  595. TOOLKIT_API int str_has_prefix (const char *str,const char *prefix)
  596. {
  597. size_t str_len;
  598. size_t prefix_len;
  599. str_len = strlen (str);
  600. prefix_len = strlen (prefix);
  601. if (str_len < prefix_len)
  602. return FALSE;
  603. return strncmp (str, prefix, prefix_len) == 0;
  604. }
  605. TOOLKIT_API unsigned int strv_length (char **str_array)
  606. {
  607. unsigned int i = 0;
  608. while (str_array[i])
  609. ++i;
  610. return i;
  611. }
  612. /* asnprintf, extract from glib */
  613. #define FLAG_GROUP 1 /* ' flag */
  614. #define FLAG_LEFT 2 /* - flag */
  615. #define FLAG_SHOWSIGN 4 /* + flag */
  616. #define FLAG_SPACE 8 /* space flag */
  617. #define FLAG_ALT 16 /* # flag */
  618. #define FLAG_ZERO 32
  619. /* A parsed directive. */
  620. typedef struct
  621. {
  622. const char* dir_start;
  623. const char* dir_end;
  624. int flags;
  625. const char* width_start;
  626. const char* width_end;
  627. int width_arg_index;
  628. const char* precision_start;
  629. const char* precision_end;
  630. int precision_arg_index;
  631. char conversion; /* d i o u x X f e E g G c s p n U % but not C S */
  632. int arg_index;
  633. }
  634. char_directive;
  635. /* A parsed format string. */
  636. typedef struct
  637. {
  638. unsigned int count;
  639. char_directive *dir;
  640. unsigned int max_width_length;
  641. unsigned int max_precision_length;
  642. }
  643. char_directives;
  644. /* Argument types */
  645. typedef enum
  646. {
  647. TYPE_NONE,
  648. TYPE_SCHAR,
  649. TYPE_UCHAR,
  650. TYPE_SHORT,
  651. TYPE_USHORT,
  652. TYPE_INT,
  653. TYPE_UINT,
  654. TYPE_LONGINT,
  655. TYPE_ULONGINT,
  656. TYPE_LONGLONGINT,
  657. TYPE_ULONGLONGINT,
  658. TYPE_INT64,
  659. TYPE_UINT64,
  660. TYPE_DOUBLE,
  661. TYPE_LONGDOUBLE,
  662. TYPE_CHAR,
  663. TYPE_STRING,
  664. TYPE_POINTER,
  665. TYPE_COUNT_SCHAR_POINTER,
  666. TYPE_COUNT_SHORT_POINTER,
  667. TYPE_COUNT_INT_POINTER,
  668. TYPE_COUNT_LONGINT_POINTER
  669. , TYPE_COUNT_LONGLONGINT_POINTER
  670. } arg_type;
  671. /* Polymorphic argument */
  672. typedef struct
  673. {
  674. arg_type type;
  675. union
  676. {
  677. signed char a_schar;
  678. unsigned char a_uchar;
  679. short a_short;
  680. unsigned short a_ushort;
  681. int a_int;
  682. unsigned int a_uint;
  683. long int a_longint;
  684. unsigned long int a_ulongint;
  685. long long int a_longlongint;
  686. unsigned long long int a_ulonglongint;
  687. __int64 a_int64;
  688. u__int64_t a_uint64;
  689. float a_float;
  690. double a_double;
  691. long double a_longdouble;
  692. int a_char;
  693. const char* a_string;
  694. void* a_pointer;
  695. signed char * a_count_schar_pointer;
  696. short * a_count_short_pointer;
  697. int * a_count_int_pointer;
  698. long int * a_count_longint_pointer;
  699. long long int * a_count_longlongint_pointer;
  700. }
  701. a;
  702. }
  703. argument;
  704. typedef struct
  705. {
  706. unsigned int count;
  707. argument *arg;
  708. }
  709. arguments;
  710. static int printf_parse (const char *format, char_directives *d, arguments *a)
  711. {
  712. const char *cp = format; /* pointer into format */
  713. int arg_posn = 0; /* number of regular arguments consumed */
  714. unsigned int d_allocated; /* allocated elements of d->dir */
  715. unsigned int a_allocated; /* allocated elements of a->arg */
  716. unsigned int max_width_length = 0;
  717. unsigned int max_precision_length = 0;
  718. d->count = 0;
  719. d_allocated = 1;
  720. d->dir = malloc (d_allocated * sizeof (char_directive));
  721. if (d->dir == NULL)
  722. /* Out of memory. */
  723. return -1;
  724. a->count = 0;
  725. a_allocated = 0;
  726. a->arg = NULL;
  727. #define REGISTER_ARG(_index_,_type_) \
  728. { \
  729. unsigned int n = (_index_); \
  730. if (n >= a_allocated) \
  731. { \
  732. argument *memory; \
  733. a_allocated = 2 * a_allocated; \
  734. if (a_allocated <= n) \
  735. a_allocated = n + 1; \
  736. memory = (a->arg \
  737. ? realloc (a->arg, a_allocated * sizeof (argument)) \
  738. : malloc (a_allocated * sizeof (argument))); \
  739. if (memory == NULL) \
  740. /* Out of memory. */ \
  741. goto error; \
  742. a->arg = memory; \
  743. } \
  744. while (a->count <= n) \
  745. a->arg[a->count++].type = TYPE_NONE; \
  746. if (a->arg[n].type == TYPE_NONE) \
  747. a->arg[n].type = (_type_); \
  748. else if (a->arg[n].type != (_type_)) \
  749. /* Ambiguous type for positional argument. */ \
  750. goto error; \
  751. }
  752. while (*cp != '\0')
  753. {
  754. char c = *cp++;
  755. if (c == '%')
  756. {
  757. int arg_index = -1;
  758. char_directive *dp = &d->dir[d->count];/* pointer to next directive */
  759. /* Initialize the next directive. */
  760. dp->dir_start = cp - 1;
  761. dp->flags = 0;
  762. dp->width_start = NULL;
  763. dp->width_end = NULL;
  764. dp->width_arg_index = -1;
  765. dp->precision_start = NULL;
  766. dp->precision_end = NULL;
  767. dp->precision_arg_index = -1;
  768. dp->arg_index = -1;
  769. /* Test for positional argument. */
  770. if (*cp >= '0' && *cp <= '9')
  771. {
  772. const char *np;
  773. for (np = cp; *np >= '0' && *np <= '9'; np++)
  774. ;
  775. if (*np == '$')
  776. {
  777. unsigned int n = 0;
  778. for (np = cp; *np >= '0' && *np <= '9'; np++)
  779. n = 10 * n + (*np - '0');
  780. if (n == 0)
  781. /* Positional argument 0. */
  782. goto error;
  783. arg_index = n - 1;
  784. cp = np + 1;
  785. }
  786. }
  787. /* Read the flags. */
  788. for (;;)
  789. {
  790. if (*cp == '\'')
  791. {
  792. dp->flags |= FLAG_GROUP;
  793. cp++;
  794. }
  795. else if (*cp == '-')
  796. {
  797. dp->flags |= FLAG_LEFT;
  798. cp++;
  799. }
  800. else if (*cp == '+')
  801. {
  802. dp->flags |= FLAG_SHOWSIGN;
  803. cp++;
  804. }
  805. else if (*cp == ' ')
  806. {
  807. dp->flags |= FLAG_SPACE;
  808. cp++;
  809. }
  810. else if (*cp == '#')
  811. {
  812. dp->flags |= FLAG_ALT;
  813. cp++;
  814. }
  815. else if (*cp == '0')
  816. {
  817. dp->flags |= FLAG_ZERO;
  818. cp++;
  819. }
  820. else
  821. break;
  822. }
  823. /* Parse the field width. */
  824. if (*cp == '*')
  825. {
  826. dp->width_start = cp;
  827. cp++;
  828. dp->width_end = cp;
  829. if (max_width_length < 1)
  830. max_width_length = 1;
  831. /* Test for positional argument. */
  832. if (*cp >= '0' && *cp <= '9')
  833. {
  834. const char *np;
  835. for (np = cp; *np >= '0' && *np <= '9'; np++)
  836. ;
  837. if (*np == '$')
  838. {
  839. unsigned int n = 0;
  840. for (np = cp; *np >= '0' && *np <= '9'; np++)
  841. n = 10 * n + (*np - '0');
  842. if (n == 0)
  843. /* Positional argument 0. */
  844. goto error;
  845. dp->width_arg_index = n - 1;
  846. cp = np + 1;
  847. }
  848. }
  849. if (dp->width_arg_index < 0)
  850. dp->width_arg_index = arg_posn++;
  851. REGISTER_ARG (dp->width_arg_index, TYPE_INT);
  852. }
  853. else if (*cp >= '0' && *cp <= '9')
  854. {
  855. unsigned int width_length;
  856. dp->width_start = cp;
  857. for (; *cp >= '0' && *cp <= '9'; cp++)
  858. ;
  859. dp->width_end = cp;
  860. width_length = (unsigned int)(dp->width_end - dp->width_start);
  861. if (max_width_length < width_length)
  862. max_width_length = width_length;
  863. }
  864. /* Parse the precision. */
  865. if (*cp == '.')
  866. {
  867. cp++;
  868. if (*cp == '*')
  869. {
  870. dp->precision_start = cp - 1;
  871. cp++;
  872. dp->precision_end = cp;
  873. if (max_precision_length < 2)
  874. max_precision_length = 2;
  875. /* Test for positional argument. */
  876. if (*cp >= '0' && *cp <= '9')
  877. {
  878. const char *np;
  879. for (np = cp; *np >= '0' && *np <= '9'; np++)
  880. ;
  881. if (*np == '$')
  882. {
  883. unsigned int n = 0;
  884. for (np = cp; *np >= '0' && *np <= '9'; np++)
  885. n = 10 * n + (*np - '0');
  886. if (n == 0)
  887. /* Positional argument 0. */
  888. goto error;
  889. dp->precision_arg_index = n - 1;
  890. cp = np + 1;
  891. }
  892. }
  893. if (dp->precision_arg_index < 0)
  894. dp->precision_arg_index = arg_posn++;
  895. REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
  896. }
  897. else
  898. {
  899. unsigned int precision_length;
  900. dp->precision_start = cp - 1;
  901. for (; *cp >= '0' && *cp <= '9'; cp++)
  902. ;
  903. dp->precision_end = cp;
  904. precision_length = (unsigned int)(dp->precision_end - dp->precision_start);
  905. if (max_precision_length < precision_length)
  906. max_precision_length = precision_length;
  907. }
  908. }
  909. {
  910. arg_type type;
  911. /* Parse argument type/size specifiers. */
  912. {
  913. int flags = 0;
  914. for (;;)
  915. {
  916. if (*cp == 'h')
  917. {
  918. flags |= (1 << (flags & 1));
  919. cp++;
  920. }
  921. else if (*cp == 'L')
  922. {
  923. flags |= 4;
  924. cp++;
  925. }
  926. else if (*cp == 'l')
  927. {
  928. flags += 8;
  929. cp++;
  930. }
  931. else if (cp[0] == 'I' &&
  932. cp[1] == '6' &&
  933. cp[2] == '4')
  934. {
  935. flags = 64;
  936. cp += 3;
  937. }
  938. else if (*cp == 'z' || *cp == 'Z')
  939. {
  940. /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
  941. because the warning facility in gcc-2.95.2 understands
  942. only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
  943. if (sizeof (size_t) > sizeof (long))
  944. {
  945. /* size_t = long long */
  946. flags += 16;
  947. }
  948. else if (sizeof (size_t) > sizeof (int))
  949. {
  950. /* size_t = long */
  951. flags += 8;
  952. }
  953. cp++;
  954. }
  955. else if (*cp == 't')
  956. {
  957. if (sizeof (ptrdiff_t) > sizeof (long))
  958. {
  959. /* ptrdiff_t = long long */
  960. flags += 16;
  961. }
  962. else if (sizeof (ptrdiff_t) > sizeof (int))
  963. {
  964. /* ptrdiff_t = long */
  965. flags += 8;
  966. }
  967. cp++;
  968. }
  969. else
  970. break;
  971. }
  972. /* Read the conversion character. */
  973. c = *cp++;
  974. switch (c)
  975. {
  976. case 'd': case 'i':
  977. if (flags == 64)
  978. type = TYPE_INT64;
  979. else
  980. if (flags >= 16 || (flags & 4))
  981. type = TYPE_LONGLONGINT;
  982. else
  983. if (flags >= 8)
  984. type = TYPE_LONGINT;
  985. else if (flags & 2)
  986. type = TYPE_SCHAR;
  987. else if (flags & 1)
  988. type = TYPE_SHORT;
  989. else
  990. type = TYPE_INT;
  991. break;
  992. case 'o': case 'u': case 'x': case 'X':
  993. if (flags == 64)
  994. type = TYPE_UINT64;
  995. else
  996. if (flags >= 16 || (flags & 4))
  997. type = TYPE_ULONGLONGINT;
  998. else
  999. if (flags >= 8)
  1000. type = TYPE_ULONGINT;
  1001. else if (flags & 2)
  1002. type = TYPE_UCHAR;
  1003. else if (flags & 1)
  1004. type = TYPE_USHORT;
  1005. else
  1006. type = TYPE_UINT;
  1007. break;
  1008. case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
  1009. case 'a': case 'A':
  1010. if (flags >= 16 || (flags & 4))
  1011. type = TYPE_LONGDOUBLE;
  1012. else
  1013. type = TYPE_DOUBLE;
  1014. break;
  1015. case 'c':
  1016. if (flags >= 8)
  1017. goto error;
  1018. else
  1019. type = TYPE_CHAR;
  1020. break;
  1021. case 's':
  1022. if (flags >= 8)
  1023. goto error;
  1024. else
  1025. type = TYPE_STRING;
  1026. break;
  1027. case 'p':
  1028. type = TYPE_POINTER;
  1029. break;
  1030. case 'n':
  1031. if (flags >= 16 || (flags & 4))
  1032. type = TYPE_COUNT_LONGLONGINT_POINTER;
  1033. else
  1034. if (flags >= 8)
  1035. type = TYPE_COUNT_LONGINT_POINTER;
  1036. else if (flags & 2)
  1037. type = TYPE_COUNT_SCHAR_POINTER;
  1038. else if (flags & 1)
  1039. type = TYPE_COUNT_SHORT_POINTER;
  1040. else
  1041. type = TYPE_COUNT_INT_POINTER;
  1042. break;
  1043. case '%':
  1044. type = TYPE_NONE;
  1045. break;
  1046. default:
  1047. /* Unknown conversion character. */
  1048. goto error;
  1049. }
  1050. }
  1051. if (type != TYPE_NONE)
  1052. {
  1053. dp->arg_index = arg_index;
  1054. if (dp->arg_index < 0)
  1055. dp->arg_index = arg_posn++;
  1056. REGISTER_ARG (dp->arg_index, type);
  1057. }
  1058. dp->conversion = c;
  1059. dp->dir_end = cp;
  1060. }
  1061. d->count++;
  1062. if (d->count >= d_allocated)
  1063. {
  1064. char_directive *memory;
  1065. d_allocated = 2 * d_allocated;
  1066. memory = realloc (d->dir, d_allocated * sizeof (char_directive));
  1067. if (memory == NULL)
  1068. /* Out of memory. */
  1069. goto error;
  1070. d->dir = memory;
  1071. }
  1072. }
  1073. }
  1074. d->dir[d->count].dir_start = cp;
  1075. d->max_width_length = max_width_length;
  1076. d->max_precision_length = max_precision_length;
  1077. return 0;
  1078. error:
  1079. if (a->arg)
  1080. free (a->arg);
  1081. if (d->dir)
  1082. free (d->dir);
  1083. return -1;
  1084. }
  1085. static int printf_fetchargs (va_list args, arguments *a)
  1086. {
  1087. unsigned int i;
  1088. argument *ap;
  1089. for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++)
  1090. switch (ap->type)
  1091. {
  1092. case TYPE_SCHAR:
  1093. ap->a.a_schar = va_arg (args, /*signed char*/ int);
  1094. break;
  1095. case TYPE_UCHAR:
  1096. ap->a.a_uchar = va_arg (args, /*unsigned char*/ int);
  1097. break;
  1098. case TYPE_SHORT:
  1099. ap->a.a_short = va_arg (args, /*short*/ int);
  1100. break;
  1101. case TYPE_USHORT:
  1102. ap->a.a_ushort = va_arg (args, /*unsigned short*/ int);
  1103. break;
  1104. case TYPE_INT:
  1105. ap->a.a_int = va_arg (args, int);
  1106. break;
  1107. case TYPE_UINT:
  1108. ap->a.a_uint = va_arg (args, unsigned int);
  1109. break;
  1110. case TYPE_LONGINT:
  1111. ap->a.a_longint = va_arg (args, long int);
  1112. break;
  1113. case TYPE_ULONGINT:
  1114. ap->a.a_ulongint = va_arg (args, unsigned long int);
  1115. break;
  1116. case TYPE_LONGLONGINT:
  1117. ap->a.a_longlongint = va_arg (args, long long int);
  1118. break;
  1119. case TYPE_ULONGLONGINT:
  1120. ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
  1121. break;
  1122. case TYPE_INT64:
  1123. ap->a.a_int64 = va_arg (args, __int64);
  1124. break;
  1125. case TYPE_UINT64:
  1126. ap->a.a_uint64 = va_arg (args, u__int64_t);
  1127. break;
  1128. case TYPE_DOUBLE:
  1129. ap->a.a_double = va_arg (args, double);
  1130. break;
  1131. case TYPE_LONGDOUBLE:
  1132. ap->a.a_longdouble = va_arg (args, long double);
  1133. break;
  1134. case TYPE_CHAR:
  1135. ap->a.a_char = va_arg (args, int);
  1136. break;
  1137. case TYPE_STRING:
  1138. ap->a.a_string = va_arg (args, const char *);
  1139. break;
  1140. case TYPE_POINTER:
  1141. ap->a.a_pointer = va_arg (args, void *);
  1142. break;
  1143. case TYPE_COUNT_SCHAR_POINTER:
  1144. ap->a.a_count_schar_pointer = va_arg (args, signed char *);
  1145. break;
  1146. case TYPE_COUNT_SHORT_POINTER:
  1147. ap->a.a_count_short_pointer = va_arg (args, short *);
  1148. break;
  1149. case TYPE_COUNT_INT_POINTER:
  1150. ap->a.a_count_int_pointer = va_arg (args, int *);
  1151. break;
  1152. case TYPE_COUNT_LONGINT_POINTER:
  1153. ap->a.a_count_longint_pointer = va_arg (args, long int *);
  1154. break;
  1155. case TYPE_COUNT_LONGLONGINT_POINTER:
  1156. ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
  1157. break;
  1158. default:
  1159. /* Unknown type. */
  1160. return -1;
  1161. }
  1162. return 0;
  1163. }
  1164. static int print_long_long (char *buf, int len, int width,int precision,unsigned long flags,char conversion,unsigned long long number)
  1165. {
  1166. int negative = FALSE;
  1167. char buffer[128];
  1168. char *bufferend;
  1169. char *pointer;
  1170. int base;
  1171. static const char *upper = "0123456789ABCDEFX";
  1172. static const char *lower = "0123456789abcdefx";
  1173. const char *digits;
  1174. int i;
  1175. char *p;
  1176. int count;
  1177. #define EMIT(c) \
  1178. if (p - buf == len - 1) \
  1179. { \
  1180. *p++ = '\0'; \
  1181. return len; \
  1182. } \
  1183. else \
  1184. *p++ = c;
  1185. p = buf;
  1186. switch (conversion)
  1187. {
  1188. case 'o':
  1189. base = 8;
  1190. digits = lower;
  1191. negative = FALSE;
  1192. break;
  1193. case 'x':
  1194. base = 16;
  1195. digits = lower;
  1196. negative = FALSE;
  1197. break;
  1198. case 'X':
  1199. base = 16;
  1200. digits = upper;
  1201. negative = FALSE;
  1202. break;
  1203. default:
  1204. base = 10;
  1205. digits = lower;
  1206. negative = (long long)number < 0;
  1207. if (negative)
  1208. number = -((long long)number);
  1209. break;
  1210. }
  1211. /* Build number */
  1212. pointer = bufferend = &buffer[sizeof(buffer) - 1];
  1213. *pointer-- = '\0';
  1214. for (i = 1; i < (int)sizeof(buffer); i++)
  1215. {
  1216. *pointer-- = digits[number % base];
  1217. number /= base;
  1218. if (number == 0)
  1219. break;
  1220. }
  1221. /* Adjust width */
  1222. width -= (int)((bufferend - pointer) - 1);
  1223. /* Adjust precision */
  1224. if (precision != -1)
  1225. {
  1226. precision -= (int)((bufferend - pointer) - 1);
  1227. if (precision < 0)
  1228. precision = 0;
  1229. flags |= FLAG_ZERO;
  1230. }
  1231. /* Adjust width further */
  1232. if (negative || (flags & FLAG_SHOWSIGN) || (flags & FLAG_SPACE))
  1233. width--;
  1234. if (flags & FLAG_ALT)
  1235. {
  1236. switch (base)
  1237. {
  1238. case 16:
  1239. width -= 2;
  1240. break;
  1241. case 8:
  1242. width--;
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. }
  1248. /* Output prefixes spaces if needed */
  1249. if (! ((flags & FLAG_LEFT) ||
  1250. ((flags & FLAG_ZERO) && (precision == -1))))
  1251. {
  1252. count = (precision == -1) ? 0 : precision;
  1253. while (width-- > count)
  1254. *p++ = ' ';
  1255. }
  1256. /* width has been adjusted for signs and alternatives */
  1257. if (negative)
  1258. {
  1259. EMIT ('-');
  1260. }
  1261. else if (flags & FLAG_SHOWSIGN)
  1262. {
  1263. EMIT('+');
  1264. }
  1265. else if (flags & FLAG_SPACE)
  1266. {
  1267. EMIT(' ');
  1268. }
  1269. if (flags & FLAG_ALT)
  1270. {
  1271. switch (base)
  1272. {
  1273. case 8:
  1274. EMIT('0');
  1275. break;
  1276. case 16:
  1277. EMIT('0');
  1278. EMIT(digits[16]);
  1279. break;
  1280. default:
  1281. break;
  1282. } /* switch base */
  1283. }
  1284. /* Output prefixed zero padding if needed */
  1285. if (flags & FLAG_ZERO)
  1286. {
  1287. if (precision == -1)
  1288. precision = width;
  1289. while (precision-- > 0)
  1290. {
  1291. EMIT('0');
  1292. width--;
  1293. }
  1294. }
  1295. /* Output the number itself */
  1296. while (*(++pointer))
  1297. {
  1298. EMIT(*pointer);
  1299. }
  1300. /* Output trailing spaces if needed */
  1301. if (flags & FLAG_LEFT)
  1302. {
  1303. while (width-- > 0)
  1304. EMIT(' ');
  1305. }
  1306. EMIT('\0');
  1307. return (int)(p - buf - 1);
  1308. }
  1309. static char *vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
  1310. {
  1311. char_directives d;
  1312. arguments a;
  1313. if (printf_parse (format, &d, &a) < 0)
  1314. {
  1315. errno = EINVAL;
  1316. return NULL;
  1317. }
  1318. #define CLEANUP() \
  1319. free (d.dir); \
  1320. if (a.arg) \
  1321. free (a.arg);
  1322. if (printf_fetchargs (args, &a) < 0)
  1323. {
  1324. CLEANUP ();
  1325. errno = EINVAL;
  1326. return NULL;
  1327. }
  1328. {
  1329. char *buf =
  1330. (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
  1331. const char *cp;
  1332. unsigned int i;
  1333. char_directive *dp;
  1334. /* Output string accumulator. */
  1335. char *result;
  1336. size_t allocated;
  1337. size_t length;
  1338. if (resultbuf != NULL)
  1339. {
  1340. result = resultbuf;
  1341. allocated = *lengthp;
  1342. }
  1343. else
  1344. {
  1345. result = NULL;
  1346. allocated = 0;
  1347. }
  1348. length = 0;
  1349. /* Invariants:
  1350. result is either == resultbuf or == NULL or malloc-allocated.
  1351. If length > 0, then result != NULL. */
  1352. #define ENSURE_ALLOCATION(needed) \
  1353. if ((needed) > allocated) \
  1354. { \
  1355. char *memory; \
  1356. \
  1357. allocated = (allocated > 0 ? 2 * allocated : 12); \
  1358. if ((needed) > allocated) \
  1359. allocated = (needed); \
  1360. if (result == resultbuf || result == NULL) \
  1361. memory = (char *) malloc (allocated); \
  1362. else \
  1363. memory = (char *) realloc (result, allocated); \
  1364. \
  1365. if (memory == NULL) \
  1366. { \
  1367. if (!(result == resultbuf || result == NULL)) \
  1368. free (result); \
  1369. CLEANUP (); \
  1370. errno = ENOMEM; \
  1371. return NULL; \
  1372. } \
  1373. if (result == resultbuf && length > 0) \
  1374. memcpy (memory, result, length); \
  1375. result = memory; \
  1376. }
  1377. for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
  1378. {
  1379. if (cp != dp->dir_start)
  1380. {
  1381. size_t n = dp->dir_start - cp;
  1382. ENSURE_ALLOCATION (length + n);
  1383. memcpy (result + length, cp, n);
  1384. length += n;
  1385. }
  1386. if (i == d.count)
  1387. break;
  1388. /* Execute a single directive. */
  1389. if (dp->conversion == '%')
  1390. {
  1391. if (!(dp->arg_index < 0))
  1392. abort ();
  1393. ENSURE_ALLOCATION (length + 1);
  1394. result[length] = '%';
  1395. length += 1;
  1396. }
  1397. else
  1398. {
  1399. if (!(dp->arg_index >= 0))
  1400. abort ();
  1401. if (dp->conversion == 'n')
  1402. {
  1403. switch (a.arg[dp->arg_index].type)
  1404. {
  1405. case TYPE_COUNT_SCHAR_POINTER:
  1406. *a.arg[dp->arg_index].a.a_count_schar_pointer = (char)length;
  1407. break;
  1408. case TYPE_COUNT_SHORT_POINTER:
  1409. *a.arg[dp->arg_index].a.a_count_short_pointer = (short)length;
  1410. break;
  1411. case TYPE_COUNT_INT_POINTER:
  1412. *a.arg[dp->arg_index].a.a_count_int_pointer = (int)length;
  1413. break;
  1414. case TYPE_COUNT_LONGINT_POINTER:
  1415. *a.arg[dp->arg_index].a.a_count_longint_pointer = (long)length;
  1416. break;
  1417. case TYPE_COUNT_LONGLONGINT_POINTER:
  1418. *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
  1419. break;
  1420. default:
  1421. abort ();
  1422. }
  1423. }
  1424. else
  1425. {
  1426. arg_type type = a.arg[dp->arg_index].type;
  1427. char *p;
  1428. unsigned int prefix_count;
  1429. int prefixes[2];
  1430. unsigned int tmp_length;
  1431. char tmpbuf[700];
  1432. char *tmp;
  1433. /* Allocate a temporary buffer of sufficient size for calling
  1434. sprintf. */
  1435. {
  1436. unsigned int width;
  1437. unsigned int precision;
  1438. width = 0;
  1439. if (dp->width_start != dp->width_end)
  1440. {
  1441. if (dp->width_arg_index >= 0)
  1442. {
  1443. int arg;
  1444. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1445. abort ();
  1446. arg = a.arg[dp->width_arg_index].a.a_int;
  1447. width = (arg < 0 ? -arg : arg);
  1448. }
  1449. else
  1450. {
  1451. const char *digitp = dp->width_start;
  1452. do
  1453. width = width * 10 + (*digitp++ - '0');
  1454. while (digitp != dp->width_end);
  1455. }
  1456. }
  1457. precision = 6;
  1458. if (dp->precision_start != dp->precision_end)
  1459. {
  1460. if (dp->precision_arg_index >= 0)
  1461. {
  1462. int arg;
  1463. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1464. abort ();
  1465. arg = a.arg[dp->precision_arg_index].a.a_int;
  1466. precision = (arg < 0 ? 0 : arg);
  1467. }
  1468. else
  1469. {
  1470. const char *digitp = dp->precision_start + 1;
  1471. precision = 0;
  1472. while (digitp != dp->precision_end)
  1473. precision = precision * 10 + (*digitp++ - '0');
  1474. }
  1475. }
  1476. switch (dp->conversion)
  1477. {
  1478. case 'd': case 'i': case 'u':
  1479. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1480. tmp_length =
  1481. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1482. * 0.30103 /* binary -> decimal */
  1483. * 2 /* estimate for FLAG_GROUP */
  1484. )
  1485. + 1 /* turn floor into ceil */
  1486. + 1; /* account for leading sign */
  1487. else
  1488. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1489. tmp_length =
  1490. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1491. * 0.30103 /* binary -> decimal */
  1492. * 2 /* estimate for FLAG_GROUP */
  1493. )
  1494. + 1 /* turn floor into ceil */
  1495. + 1; /* account for leading sign */
  1496. else
  1497. tmp_length =
  1498. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1499. * 0.30103 /* binary -> decimal */
  1500. * 2 /* estimate for FLAG_GROUP */
  1501. )
  1502. + 1 /* turn floor into ceil */
  1503. + 1; /* account for leading sign */
  1504. break;
  1505. case 'o':
  1506. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1507. tmp_length =
  1508. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1509. * 0.333334 /* binary -> octal */
  1510. )
  1511. + 1 /* turn floor into ceil */
  1512. + 1; /* account for leading sign */
  1513. else
  1514. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1515. tmp_length =
  1516. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1517. * 0.333334 /* binary -> octal */
  1518. )
  1519. + 1 /* turn floor into ceil */
  1520. + 1; /* account for leading sign */
  1521. else
  1522. tmp_length =
  1523. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1524. * 0.333334 /* binary -> octal */
  1525. )
  1526. + 1 /* turn floor into ceil */
  1527. + 1; /* account for leading sign */
  1528. break;
  1529. case 'x': case 'X':
  1530. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1531. tmp_length =
  1532. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1533. * 0.25 /* binary -> hexadecimal */
  1534. )
  1535. + 1 /* turn floor into ceil */
  1536. + 2; /* account for leading sign or alternate form */
  1537. else
  1538. if (type == TYPE_INT64 || type == TYPE_UINT64)
  1539. tmp_length =
  1540. (unsigned int) (sizeof (u__int64_t) * CHAR_BIT
  1541. * 0.25 /* binary -> hexadecimal */
  1542. )
  1543. + 1 /* turn floor into ceil */
  1544. + 2; /* account for leading sign or alternate form */
  1545. else
  1546. if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1547. tmp_length =
  1548. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1549. * 0.25 /* binary -> hexadecimal */
  1550. )
  1551. + 1 /* turn floor into ceil */
  1552. + 2; /* account for leading sign or alternate form */
  1553. else
  1554. tmp_length =
  1555. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1556. * 0.25 /* binary -> hexadecimal */
  1557. )
  1558. + 1 /* turn floor into ceil */
  1559. + 2; /* account for leading sign or alternate form */
  1560. break;
  1561. case 'f': case 'F':
  1562. if (type == TYPE_LONGDOUBLE)
  1563. tmp_length =
  1564. (unsigned int) (LDBL_MAX_EXP
  1565. * 0.30103 /* binary -> decimal */
  1566. * 2 /* estimate for FLAG_GROUP */
  1567. )
  1568. + 1 /* turn floor into ceil */
  1569. + precision
  1570. + 10; /* sign, decimal point etc. */
  1571. else
  1572. tmp_length =
  1573. (unsigned int) (DBL_MAX_EXP
  1574. * 0.30103 /* binary -> decimal */
  1575. * 2 /* estimate for FLAG_GROUP */
  1576. )
  1577. + 1 /* turn floor into ceil */
  1578. + precision
  1579. + 10; /* sign, decimal point etc. */
  1580. break;
  1581. case 'e': case 'E': case 'g': case 'G':
  1582. case 'a': case 'A':
  1583. tmp_length =
  1584. precision
  1585. + 12; /* sign, decimal point, exponent etc. */
  1586. break;
  1587. case 'c':
  1588. tmp_length = 1;
  1589. break;
  1590. case 's':
  1591. tmp_length = (unsigned int)strlen (a.arg[dp->arg_index].a.a_string);
  1592. break;
  1593. case 'p':
  1594. tmp_length =
  1595. (unsigned int) (sizeof (void *) * CHAR_BIT
  1596. * 0.25 /* binary -> hexadecimal */
  1597. )
  1598. + 1 /* turn floor into ceil */
  1599. + 2; /* account for leading 0x */
  1600. break;
  1601. default:
  1602. abort ();
  1603. }
  1604. if (tmp_length < width)
  1605. tmp_length = width;
  1606. tmp_length++; /* account for trailing NUL */
  1607. }
  1608. if (tmp_length <= sizeof (tmpbuf))
  1609. tmp = tmpbuf;
  1610. else
  1611. {
  1612. tmp = (char *) malloc (tmp_length);
  1613. if (tmp == NULL)
  1614. {
  1615. /* Out of memory. */
  1616. if (!(result == resultbuf || result == NULL))
  1617. free (result);
  1618. CLEANUP ();
  1619. errno = ENOMEM;
  1620. return NULL;
  1621. }
  1622. }
  1623. /* Construct the format string for calling snprintf or
  1624. sprintf. */
  1625. p = buf;
  1626. *p++ = '%';
  1627. if (dp->flags & FLAG_GROUP)
  1628. *p++ = '\'';
  1629. if (dp->flags & FLAG_LEFT)
  1630. *p++ = '-';
  1631. if (dp->flags & FLAG_SHOWSIGN)
  1632. *p++ = '+';
  1633. if (dp->flags & FLAG_SPACE)
  1634. *p++ = ' ';
  1635. if (dp->flags & FLAG_ALT)
  1636. *p++ = '#';
  1637. if (dp->flags & FLAG_ZERO)
  1638. *p++ = '0';
  1639. if (dp->width_start != dp->width_end)
  1640. {
  1641. size_t n = dp->width_end - dp->width_start;
  1642. memcpy (p, dp->width_start, n);
  1643. p += n;
  1644. }
  1645. if (dp->precision_start != dp->precision_end)
  1646. {
  1647. size_t n = dp->precision_end - dp->precision_start;
  1648. memcpy (p, dp->precision_start, n);
  1649. p += n;
  1650. }
  1651. switch (type)
  1652. {
  1653. case TYPE_INT64:
  1654. case TYPE_UINT64:
  1655. *p++ = 'I';
  1656. *p++ = '6';
  1657. *p++ = '4';
  1658. break;
  1659. case TYPE_LONGLONGINT:
  1660. case TYPE_ULONGLONGINT:
  1661. *p++ = 'I';
  1662. *p++ = '6';
  1663. *p++ = '4';
  1664. break;
  1665. *p++ = 'l';
  1666. /*FALLTHROUGH*/
  1667. case TYPE_LONGINT:
  1668. case TYPE_ULONGINT:
  1669. *p++ = 'l';
  1670. break;
  1671. case TYPE_LONGDOUBLE:
  1672. *p++ = 'L';
  1673. break;
  1674. default:
  1675. break;
  1676. }
  1677. *p = dp->conversion;
  1678. p[1] = '\0';
  1679. /* Construct the arguments for calling snprintf or sprintf. */
  1680. prefix_count = 0;
  1681. if (dp->width_arg_index >= 0)
  1682. {
  1683. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1684. abort ();
  1685. prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
  1686. }
  1687. if (dp->precision_arg_index >= 0)
  1688. {
  1689. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1690. abort ();
  1691. prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
  1692. }
  1693. for (;;)
  1694. {
  1695. size_t maxlen;
  1696. int count;
  1697. int retcount;
  1698. maxlen = allocated - length;
  1699. count = -1;
  1700. retcount = 0;
  1701. #define SNPRINTF_BUF(arg) \
  1702. switch (prefix_count) \
  1703. { \
  1704. case 0: \
  1705. count = sprintf (tmp, buf, arg); \
  1706. break; \
  1707. case 1: \
  1708. count = sprintf (tmp, buf, prefixes[0], arg); \
  1709. break; \
  1710. case 2: \
  1711. count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
  1712. arg); \
  1713. break; \
  1714. default: \
  1715. abort (); \
  1716. }
  1717. switch (type)
  1718. {
  1719. case TYPE_SCHAR:
  1720. {
  1721. int arg = a.arg[dp->arg_index].a.a_schar;
  1722. SNPRINTF_BUF (arg);
  1723. }
  1724. break;
  1725. case TYPE_UCHAR:
  1726. {
  1727. unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
  1728. SNPRINTF_BUF (arg);
  1729. }
  1730. break;
  1731. case TYPE_SHORT:
  1732. {
  1733. int arg = a.arg[dp->arg_index].a.a_short;
  1734. SNPRINTF_BUF (arg);
  1735. }
  1736. break;
  1737. case TYPE_USHORT:
  1738. {
  1739. unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
  1740. SNPRINTF_BUF (arg);
  1741. }
  1742. break;
  1743. case TYPE_INT:
  1744. {
  1745. int arg = a.arg[dp->arg_index].a.a_int;
  1746. SNPRINTF_BUF (arg);
  1747. }
  1748. break;
  1749. case TYPE_UINT:
  1750. {
  1751. unsigned int arg = a.arg[dp->arg_index].a.a_uint;
  1752. SNPRINTF_BUF (arg);
  1753. }
  1754. break;
  1755. case TYPE_LONGINT:
  1756. {
  1757. long int arg = a.arg[dp->arg_index].a.a_longint;
  1758. SNPRINTF_BUF (arg);
  1759. }
  1760. break;
  1761. case TYPE_ULONGINT:
  1762. {
  1763. unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
  1764. SNPRINTF_BUF (arg);
  1765. }
  1766. break;
  1767. case TYPE_INT64:
  1768. {
  1769. __int64 arg = a.arg[dp->arg_index].a.a_int64;
  1770. SNPRINTF_BUF (arg);
  1771. }
  1772. break;
  1773. case TYPE_UINT64:
  1774. {
  1775. u__int64_t arg = a.arg[dp->arg_index].a.a_uint64;
  1776. SNPRINTF_BUF (arg);
  1777. }
  1778. break;
  1779. case TYPE_LONGLONGINT:
  1780. case TYPE_ULONGLONGINT:
  1781. {
  1782. unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
  1783. int width;
  1784. int precision;
  1785. width = 0;
  1786. if (dp->width_start != dp->width_end)
  1787. {
  1788. if (dp->width_arg_index >= 0)
  1789. {
  1790. int arg;
  1791. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1792. abort ();
  1793. arg = a.arg[dp->width_arg_index].a.a_int;
  1794. width = (arg < 0 ? -arg : arg);
  1795. }
  1796. else
  1797. {
  1798. const char *digitp = dp->width_start;
  1799. do
  1800. width = width * 10 + (*digitp++ - '0');
  1801. while (digitp != dp->width_end);
  1802. }
  1803. }
  1804. precision = -1;
  1805. if (dp->precision_start != dp->precision_end)
  1806. {
  1807. if (dp->precision_arg_index >= 0)
  1808. {
  1809. int arg;
  1810. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1811. abort ();
  1812. arg = a.arg[dp->precision_arg_index].a.a_int;
  1813. precision = (arg < 0 ? 0 : arg);
  1814. }
  1815. else
  1816. {
  1817. const char *digitp = dp->precision_start + 1;
  1818. precision = 0;
  1819. do
  1820. precision = precision * 10 + (*digitp++ - '0');
  1821. while (digitp != dp->precision_end);
  1822. }
  1823. }
  1824. count = print_long_long (tmp, tmp_length,
  1825. width, precision,
  1826. dp->flags,
  1827. dp->conversion,
  1828. arg);
  1829. }
  1830. break;
  1831. case TYPE_DOUBLE:
  1832. {
  1833. double arg = a.arg[dp->arg_index].a.a_double;
  1834. SNPRINTF_BUF (arg);
  1835. }
  1836. break;
  1837. case TYPE_LONGDOUBLE:
  1838. {
  1839. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  1840. SNPRINTF_BUF (arg);
  1841. }
  1842. break;
  1843. case TYPE_CHAR:
  1844. {
  1845. int arg = a.arg[dp->arg_index].a.a_char;
  1846. SNPRINTF_BUF (arg);
  1847. }
  1848. break;
  1849. case TYPE_STRING:
  1850. {
  1851. const char *arg = a.arg[dp->arg_index].a.a_string;
  1852. SNPRINTF_BUF (arg);
  1853. }
  1854. break;
  1855. case TYPE_POINTER:
  1856. {
  1857. void *arg = a.arg[dp->arg_index].a.a_pointer;
  1858. SNPRINTF_BUF (arg);
  1859. }
  1860. break;
  1861. default:
  1862. abort ();
  1863. }
  1864. /* Attempt to handle failure. */
  1865. if (count < 0)
  1866. {
  1867. if (!(result == resultbuf || result == NULL))
  1868. free (result);
  1869. CLEANUP ();
  1870. errno = EINVAL;
  1871. return NULL;
  1872. }
  1873. if (count >= (int)tmp_length)
  1874. /* tmp_length was incorrectly calculated - fix the
  1875. code above! */
  1876. abort ();
  1877. /* Make room for the result. */
  1878. if (count >= (int)maxlen)
  1879. {
  1880. /* Need at least count bytes. But allocate
  1881. proportionally, to avoid looping eternally if
  1882. snprintf() reports a too small count. */
  1883. size_t n = length + count;
  1884. if (n < 2 * allocated)
  1885. n = 2 * allocated;
  1886. ENSURE_ALLOCATION (n);
  1887. }
  1888. /* Append the sprintf() result. */
  1889. memcpy (result + length, tmp, count);
  1890. if (tmp != tmpbuf)
  1891. free (tmp);
  1892. length += count;
  1893. break;
  1894. }
  1895. }
  1896. }
  1897. }
  1898. /* Add the final NUL. */
  1899. ENSURE_ALLOCATION (length + 1);
  1900. result[length] = '\0';
  1901. if (result != resultbuf && length + 1 < allocated)
  1902. {
  1903. /* Shrink the allocated memory if possible. */
  1904. char *memory;
  1905. memory = (char *) realloc (result, length + 1);
  1906. if (memory != NULL)
  1907. result = memory;
  1908. }
  1909. CLEANUP ();
  1910. *lengthp = length;
  1911. return result;
  1912. }
  1913. }
  1914. #define _XT(str) str
  1915. #define CHR_LT_ _XT('<')
  1916. #define CHR_GT_ _XT('>')
  1917. #define CHR_AMP_ _XT('&')
  1918. #define CHR_APOS_ _XT('\'')
  1919. #define CHR_QUOT_ _XT('"')
  1920. #define XML_LT_ _XT("&lt;")
  1921. #define XML_GT_ _XT("&gt;")
  1922. #define XML_AMP_ _XT("&amp;")
  1923. #define XML_APOS_ _XT("&apos;")
  1924. #define XML_QUOT_ _XT("&quot;")
  1925. enum
  1926. {
  1927. LT_SIZE_ = 4, /**< Size of &lt; */
  1928. GT_SIZE_ = 4, /**< Size of &gt; */
  1929. AMP_SIZE_ = 5, /**< Size of &amp; */
  1930. APOS_SIZE_ = 6, /**< Size of &apos; */
  1931. QUOT_SIZE_ = 6 /**< Size of &quot; */
  1932. };
  1933. TOOLKIT_API char *str_xml_escape(const char *src)
  1934. {
  1935. char *p = (char *) src;
  1936. char *escaped = NULL;
  1937. unsigned int len = 0;
  1938. assert (src != NULL);
  1939. /* We first need to calculate the size of the new escaped string. */
  1940. while (*p != _XT('\0'))
  1941. {
  1942. switch (*p)
  1943. {
  1944. case CHR_LT_:
  1945. len += LT_SIZE_;
  1946. break;
  1947. case CHR_GT_:
  1948. len += GT_SIZE_;
  1949. break;
  1950. case CHR_AMP_:
  1951. len += AMP_SIZE_;
  1952. break;
  1953. case CHR_APOS_:
  1954. len += APOS_SIZE_;
  1955. break;
  1956. case CHR_QUOT_:
  1957. len += QUOT_SIZE_;
  1958. break;
  1959. default:
  1960. len += 1;
  1961. break;
  1962. }
  1963. p += 1;
  1964. }
  1965. /* Allocate new string (if necessary). */
  1966. escaped = calloc (len + 1, sizeof (char));
  1967. /* Append characters to new string, escaping the needed ones. */
  1968. p = (char *) src;
  1969. len = 0;
  1970. while (*p != _XT('\0'))
  1971. {
  1972. switch (*p)
  1973. {
  1974. case CHR_LT_:
  1975. memcpy (&escaped[len], XML_LT_, LT_SIZE_);
  1976. len += LT_SIZE_;
  1977. break;
  1978. case CHR_GT_:
  1979. memcpy (&escaped[len], XML_GT_, GT_SIZE_);
  1980. len += GT_SIZE_;
  1981. break;
  1982. case CHR_AMP_:
  1983. memcpy (&escaped[len], XML_AMP_, AMP_SIZE_);
  1984. len += AMP_SIZE_;
  1985. break;
  1986. case CHR_APOS_:
  1987. memcpy (&escaped[len], XML_APOS_, APOS_SIZE_);
  1988. len += APOS_SIZE_;
  1989. break;
  1990. case CHR_QUOT_:
  1991. memcpy (&escaped[len], XML_QUOT_, QUOT_SIZE_);
  1992. len += QUOT_SIZE_;
  1993. break;
  1994. default:
  1995. escaped[len] = *p;
  1996. len += 1;
  1997. break;
  1998. }
  1999. p += 1;
  2000. }
  2001. return escaped;
  2002. }
  2003. TOOLKIT_API void str_parse_cmdline (char *cmdstart,char **argv,char *args,int *numargs,int *numchars)
  2004. {
  2005. char *p;
  2006. char c;
  2007. int inquote; /* 1 = inside quotes */
  2008. int copychar; /* 1 = copy char to *args */
  2009. unsigned numslash; /* num of backslashes seen */
  2010. *numchars = 0;
  2011. *numargs = 1; /* the program name at least */
  2012. /* first scan the program name, copy it, and count the bytes */
  2013. p = cmdstart;
  2014. if (argv)
  2015. *argv++ = args;
  2016. /* A quoted program name is handled here. The handling is much
  2017. simpler than for other arguments. Basically, whatever lies
  2018. between the leading double-quote and next one, or a terminal null
  2019. character is simply accepted. Fancier handling is not required
  2020. because the program name must be a legal NTFS/HPFS file name.
  2021. Note that the double-quote characters are not copied, nor do they
  2022. contribute to numchars. */
  2023. inquote = FALSE;
  2024. do {
  2025. if (*p == '\"' )
  2026. {
  2027. inquote = !inquote;
  2028. c = (char) *p++;
  2029. continue;
  2030. }
  2031. ++*numchars;
  2032. if (args)
  2033. *args++ = *p;
  2034. c = (char) *p++;
  2035. } while ( (c != 0 && (inquote || (c !=' ' && c != '\t'))) );
  2036. if ( c == 0 ) {
  2037. p--;
  2038. } else {
  2039. if (args)
  2040. *(args-1) = 0;
  2041. }
  2042. inquote = 0;
  2043. /* loop on each argument */
  2044. for(;;) {
  2045. if ( *p ) {
  2046. while (*p == ' ' || *p == '\t')
  2047. ++p;
  2048. }
  2049. if (*p == 0)
  2050. break; /* end of args */
  2051. /* scan an argument */
  2052. if (argv)
  2053. *argv++ = args; /* store ptr to arg */
  2054. ++*numargs;
  2055. /* loop through scanning one argument */
  2056. for (;;) {
  2057. copychar = 1;
  2058. /* Rules: 2N backslashes + " ==> N backslashes and begin/end quote
  2059. 2N+1 backslashes + " ==> N backslashes + literal "
  2060. N backslashes ==> N backslashes */
  2061. numslash = 0;
  2062. while (*p == '\\') {
  2063. /* count number of backslashes for use below */
  2064. ++p;
  2065. ++numslash;
  2066. }
  2067. if (*p == '\"') {
  2068. /* if 2N backslashes before, start/end quote, otherwise
  2069. copy literally */
  2070. if (numslash % 2 == 0) {
  2071. if (inquote && p[1] == '\"') {
  2072. p++; /* Double quote inside quoted string */
  2073. } else { /* skip first quote char and copy second */
  2074. copychar = 0; /* don't copy quote */
  2075. inquote = !inquote;
  2076. }
  2077. }
  2078. numslash /= 2; /* divide numslash by two */
  2079. }
  2080. /* copy slashes */
  2081. while (numslash--) {
  2082. if (args)
  2083. *args++ = '\\';
  2084. ++*numchars;
  2085. }
  2086. /* if at end of arg, break loop */
  2087. if (*p == 0 || (!inquote && (*p == ' ' || *p == '\t')))
  2088. break;
  2089. if (copychar) {
  2090. if (args)
  2091. *args++ = *p;
  2092. ++*numchars;
  2093. }
  2094. ++p;
  2095. }
  2096. /* null-terminate the argument */
  2097. if (args)
  2098. *args++ = 0; /* terminate string */
  2099. ++*numchars;
  2100. }
  2101. /* We put one last argument in -- a null ptr */
  2102. if (argv)
  2103. *argv++ = NULL;
  2104. ++*numargs;
  2105. }