You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* vsnprintf_r.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* a reduced but async-signal-safe and thread-safe version of vsnprintf */
  4. #include "config.h"
  5. #include <stddef.h> /* ptrdiff_t */
  6. #include <ctype.h> /* isdigit() */
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #if HAVE_SYSLOG_H
  11. #include <syslog.h>
  12. #endif
  13. #include <sys/utsname.h>
  14. #include <time.h> /* time_t, strftime() */
  15. #include <sys/time.h> /* gettimeofday() */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #if HAVE_UNISTD_H
  19. #include <unistd.h>
  20. #endif
  21. #include "vsnprintf_r.h"
  22. /* helper functions for vsnprintf_r():
  23. e.g. convert an unsigned long to decimal string.
  24. in: field (must be long enough for all digits and \0
  25. n: length of field in bytes
  26. ulo: the value
  27. returns: the pointer to the result string (need not be ==field)
  28. */
  29. /* this function converts an unsigned long number to decimal ASCII
  30. it is async signal safe and thread safe
  31. it returns NULL if n==0
  32. it returns NULL if field is too short to hold the result
  33. it returns a pointer to the result string (somewhere within field)
  34. it terminates result with \0
  35. */
  36. static char *_diag_ulong_to_dec(char *field, size_t n, unsigned long ulo) {
  37. char *np = field+n; /* point to the end */
  38. if (n == 0) return NULL;
  39. *--np = '\0'; /* \0 in last char of string */
  40. /* this is not optimal - uses much CPU, but simple to implement */
  41. /* calculate the result from right to left */
  42. do { if (np==field) return NULL; *--np = '0'+(ulo%10); } while (ulo/=10);
  43. return np;
  44. }
  45. /* this function converts an unsigned long number to decimal ASCII
  46. and pads it with space or '0' when size and leading0 are set appropriately
  47. it is async signal safe and thread safe
  48. it returns NULL if n==0
  49. it returns NULL if field is too short to hold the result
  50. it returns a pointer to the result string (somewhere within field)
  51. it reduces size to n-1 if it is greater or equal
  52. it terminates result with \0
  53. */
  54. static char *diag_ulong_to_dec(char *field, size_t n, unsigned long ulo, int leading0, int size) {
  55. char *np;
  56. char c;
  57. int i;
  58. if (n == 0) return NULL;
  59. np = _diag_ulong_to_dec(field, n, ulo);
  60. if (np == NULL) return np;
  61. if (size) {
  62. if (size >= n) size = n-1;
  63. if (leading0) {
  64. c = '0';
  65. } else {
  66. c = ' ';
  67. }
  68. i = size - strlen(np);
  69. while (--i >= 0) {
  70. *--np = c;
  71. }
  72. }
  73. return np;
  74. }
  75. /* this function converts a signed long number to decimal ASCII
  76. and pads it with space or '0' when size and leading0 are set appropriately
  77. it is async signal safe and thread safe
  78. it returns NULL if n==0
  79. it returns NULL if field is too short to hold the result
  80. it returns a pointer to the result string (somewhere within field)
  81. it reduces size to n-1 if it is greater or equal
  82. it terminates result with \0
  83. */
  84. /* like diag_ulong_to_dec but signed; fields need also space for '-' */
  85. static char *diag_long_to_dec(char *field, size_t n, long lo, int leading0, int size) {
  86. char *np;
  87. int minus;
  88. unsigned long ulo;
  89. int i;
  90. if ((minus = (lo < 0))) {
  91. ulo = (~lo)+1;
  92. } else {
  93. ulo = lo;
  94. }
  95. np = _diag_ulong_to_dec(field, n, ulo);
  96. if (np == NULL) return np;
  97. if (size) {
  98. if (size >= n) size = n-1;
  99. i = size - strlen(np);
  100. if (leading0) {
  101. if (minus) --i;
  102. while (--i >= 0) {
  103. *--np = '0';
  104. }
  105. if (minus) *--np = '-';
  106. } else {
  107. if (minus) { *--np = '-'; --i; }
  108. while (--i >= 0) {
  109. *--np = ' ';
  110. }
  111. }
  112. } else {
  113. if (minus) *--np = '-';
  114. }
  115. return np;
  116. }
  117. /* this function converts an unsigned long number to hexadecimal ASCII
  118. it is async signal safe and thread safe
  119. it returns NULL if n==0
  120. it returns NULL if field is too short to hold the result
  121. it returns a pointer to the result string (somewhere within field)
  122. it terminates result with \0
  123. */
  124. static char *diag_ulong_to_hex(char *field, size_t n, unsigned long ulo, int leading0, size_t size) {
  125. char *np = field+n; /* point to the end */
  126. int i;
  127. char c;
  128. if (n == 0) return NULL;
  129. *--np = '\0'; /* \0 in last char of string */
  130. /* calculate the result from right to left */
  131. do { if (np==field) return NULL; i = (ulo&0x0f);
  132. *--np = (i<10?'0':('a'-10))+i; }
  133. while (ulo>>=4);
  134. if (size) {
  135. if (size >= n) size = n-1;
  136. if (leading0) {
  137. c = '0';
  138. } else {
  139. c = ' ';
  140. }
  141. i = size - strlen(np);
  142. while (--i >= 0) {
  143. *--np = c;
  144. }
  145. }
  146. return np;
  147. }
  148. /* this function converts an unsigned long number to octal ASCII
  149. it is async signal safe and thread safe
  150. it returns NULL if n==0
  151. it returns NULL if field is too short to hold the result
  152. it returns a pointer to the result string (somewhere within field)
  153. it terminates result with \0
  154. */
  155. static char *diag_ulong_to_oct(char *field, size_t n, unsigned long ulo, int leading0, size_t size) {
  156. char *np = field+n; /* point to the end */
  157. int i;
  158. char c;
  159. if (n == 0) return NULL;
  160. *--np = '\0'; /* \0 in last char of string */
  161. /* calculate the result from right to left */
  162. do { if (np==field) return NULL; i = (ulo&0x07); *--np = '0'+i; }
  163. while (ulo>>=3);
  164. if (size) {
  165. if (size >= n) size = n-1;
  166. if (leading0) {
  167. c = '0';
  168. } else {
  169. c = ' ';
  170. }
  171. i = size - strlen(np);
  172. while (--i >= 0) {
  173. *--np = c;
  174. }
  175. }
  176. return np;
  177. }
  178. #if HAVE_TYPE_LONGLONG
  179. /* this function converts an unsigned long long number to decimal ASCII
  180. it is async signal safe and thread safe
  181. it returns NULL if n==0
  182. it returns NULL if field is too short to hold the result
  183. it returns a pointer to the result string (somewhere within field)
  184. it terminates result with \0
  185. */
  186. static char *_diag_ulonglong_to_dec(char *field, size_t n, unsigned long long ull) {
  187. char *np = field+n; /* point to the end */
  188. if (n == 0) return NULL;
  189. *--np = '\0'; /* \0 in last char of string */
  190. /* this is not optimal - uses much CPU, but simple to implement */
  191. /* calculate the result from right to left */
  192. do { if (np==field) return NULL; *--np = '0'+(ull%10); } while (ull/=10);
  193. return np;
  194. }
  195. /* this function converts an unsigned long long number to decimal ASCII
  196. and pads it with space or '0' when size and leading0 are set appropriately
  197. it is async signal safe and thread safe
  198. it returns NULL if n==0
  199. it returns NULL if field is too short to hold the result
  200. it returns a pointer to the result string (somewhere within field)
  201. it reduces size to n-1 if it is greater or equal
  202. it terminates result with \0
  203. */
  204. static char *diag_ulonglong_to_dec(char *field, size_t n, unsigned long long ull, int leading0, int size) {
  205. char *np;
  206. char c;
  207. int i;
  208. if (n == 0) return NULL;
  209. np = _diag_ulonglong_to_dec(field, n, ull);
  210. if (size) {
  211. if (size >= n) size = n-1;
  212. if (leading0) {
  213. c = '0';
  214. } else {
  215. c = ' ';
  216. }
  217. i = size - strlen(np);
  218. while (i-- > 0) {
  219. *--np = c;
  220. }
  221. }
  222. return np;
  223. }
  224. /* this function converts a signed long long number to decimal ASCII
  225. and pads it with space or '0' when size and leading0 are set appropriately
  226. it is async signal safe and thread safe
  227. it returns NULL if n==0
  228. it returns NULL if field is too short to hold the result
  229. it returns a pointer to the result string (somewhere within field)
  230. it reduces size to n-1 if it is greater or equal
  231. it terminates result with \0
  232. */
  233. /* like diag_ulonglong_to_dec but signed; fields need also space for '-' */
  234. static char *diag_longlong_to_dec(char *field, size_t n, long long ll, int leading0, int size) {
  235. char *np;
  236. int minus;
  237. unsigned long ull;
  238. int i;
  239. if ((minus = (ll < 0))) {
  240. ull = (~ll)+1;
  241. } else {
  242. ull = ll;
  243. }
  244. np = _diag_ulonglong_to_dec(field, n, ull);
  245. if (np == NULL) return np;
  246. if (size) {
  247. if (size >= n) size = n-1;
  248. i = size - strlen(np);
  249. if (leading0) {
  250. if (minus) --i;
  251. while (--i >= 0) {
  252. *--np = '0';
  253. }
  254. if (minus) *--np = '-';
  255. } else {
  256. if (minus) { *--np = '-'; --i; }
  257. while (--i >= 0) {
  258. *--np = ' ';
  259. }
  260. }
  261. }
  262. return np;
  263. }
  264. /* this function converts an unsigned long long number to hexadecimal ASCII
  265. it is async signal safe and thread safe
  266. it returns NULL if n==0
  267. it returns NULL if field is too short to hold the result
  268. it returns a pointer to the result string (somewhere within field)
  269. it terminates result with \0
  270. */
  271. static char *diag_ulonglong_to_hex(char *field, size_t n, unsigned long long ull, int leading0, size_t size) {
  272. char *np = field+n; /* point to the end */
  273. ptrdiff_t i;
  274. char c;
  275. if (n == 0) return NULL;
  276. *--np = '\0'; /* \0 in last char of string */
  277. /* calculate the result from right to left */
  278. do { if (np==field) return NULL; i = (ull&0x0f);
  279. *--np = (i<10?'0':('a'-10))+i; }
  280. while (ull>>=4);
  281. if (size) {
  282. if (size >= n) size = n-1;
  283. if (leading0) {
  284. c = '0';
  285. } else {
  286. c = ' ';
  287. }
  288. i = size - strlen(np);
  289. while (i-- > 0) {
  290. *--np = c;
  291. }
  292. }
  293. return np;
  294. }
  295. /* this function converts an unsigned long long number to octal ASCII
  296. it is async signal safe and thread safe
  297. it returns NULL if n==0
  298. it returns NULL if field is too short to hold the result
  299. it returns a pointer to the result string (somewhere within field)
  300. it terminates result with \0
  301. */
  302. static char *diag_ulonglong_to_oct(char *field, size_t n, unsigned long long ull, int leading0, size_t size) {
  303. char *np = field+n; /* point to the end */
  304. int i;
  305. char c;
  306. if (n == 0) return NULL;
  307. *--np = '\0'; /* \0 in last char of string */
  308. /* calculate the result from right to left */
  309. do { if (np==field) return NULL; i = (ull&0x07); *--np = '0'+i; }
  310. while (ull>>=3);
  311. if (size) {
  312. if (size >= n) size = n-1;
  313. if (leading0) {
  314. c = '0';
  315. } else {
  316. c = ' ';
  317. }
  318. i = size - strlen(np);
  319. while (--i >= 0) {
  320. *--np = c;
  321. }
  322. }
  323. return np;
  324. }
  325. #endif /* HAVE_TYPE_LONGLONG */
  326. /* this function is designed as a variant of vsnprintf(3) but async signal safe
  327. and thread safe
  328. it currently only implements a subset of the format directives
  329. returns <0 if an error occurred (no scenario know yet)
  330. returns >=size if output is truncated (conforming to C99 standard)
  331. */
  332. int vsnprintf_r(char *str, size_t size, const char *format, va_list ap) {
  333. size_t i = 0;
  334. char c;
  335. int full = 0; /* indicate if output buffer full */
  336. --size; /* without trailing \0 */
  337. while (c = *format++) {
  338. if (c == '\\') {
  339. } else if (c == '%') {
  340. #if HAVE_TYPE_LONGLONG
  341. # define num_buff_len ((sizeof(unsigned long long)*8+2)/3+1) /* hold up to u long long in octal w/ \0 */
  342. #else
  343. # define num_buff_len ((sizeof(unsigned long)*8+2)/3+1)]; /* hold up to u long in octal w/ \0 */
  344. #endif
  345. char lengthmod = '\0'; /* 'h' 'l' 'L' 'z' */
  346. int leading0 = 0; /* or 1 */
  347. size_t fsize = 0; /* size of field */
  348. const char *st; /* string */
  349. long lo; unsigned long ulo;
  350. #if HAVE_TYPE_LONGLONG
  351. long long ll; unsigned long long ull;
  352. #endif
  353. char field[num_buff_len]; /* result of number conversion */
  354. char *np; /* num pointer */
  355. c = *format++;
  356. if (c == '\0') { break; }
  357. /* flag characters */
  358. switch (c) {
  359. case '0': leading0 = 1; c = *format++; break;
  360. /* not handled: '#' '-' ' ' '+' '\'' */
  361. }
  362. if (c == '\0') { break; }
  363. /* field width */
  364. switch (c) {
  365. case '1': case '2': case '3': case '4':
  366. case '5': case '6': case '7': case '8': case '9':
  367. do {
  368. fsize = 10*fsize+(c-'0');
  369. c = *format++;
  370. } while (c && isdigit(c));
  371. break;
  372. }
  373. if (c == '\0') { break; }
  374. /* precision - not handles */
  375. /* length modifier */
  376. switch (c) {
  377. /* not handled: 'q' 'j' 't' */
  378. /* handled: 'h' 'hh'->'H' 'z' 'Z'->'z' 'l' 'll'->'L' 'L' */
  379. case 'Z': c = 'z'; /* fall through */
  380. #if HAVE_TYPE_LONGLONG
  381. case 'L':
  382. #endif
  383. case 'z': lengthmod = c; c = *format++; break;
  384. case 'h':
  385. lengthmod = c;
  386. if ((c = *format++) == 'h') {
  387. lengthmod = 'H'; c = *format++;
  388. }
  389. break;
  390. case 'l':
  391. lengthmod = c;
  392. if ((c = *format++) == 'l') {
  393. lengthmod = 'L'; c = *format++;
  394. }
  395. break;
  396. }
  397. if (c == '\0') { break; }
  398. /* conversion specifier */
  399. switch (c) {
  400. case 'c': c = va_arg(ap, int); /* fall through */
  401. case '%': *str++ = c; if (++i == size) { full = 1; } break;
  402. case 's': st = va_arg(ap, const char *);
  403. /* no modifier handled! */
  404. while (c = *st++) {
  405. *str++ = c;
  406. if (++i == size) { full = 1; break; }
  407. }
  408. break;
  409. case 'd':
  410. #if HAVE_TYPE_LONGLONG
  411. if (lengthmod == 'L') {
  412. ll = va_arg(ap, long long);
  413. np = diag_longlong_to_dec(field, num_buff_len, ll, leading0, fsize);
  414. while (c = *np++) {
  415. *str++ = c;
  416. if (++i == size) { full = 1; break; }
  417. }
  418. } else
  419. #endif
  420. {
  421. switch (lengthmod) {
  422. case 'l': lo = va_arg(ap, long); break;
  423. case 'z': lo = va_arg(ap, ptrdiff_t); break;
  424. default: lo = va_arg(ap, int); break;
  425. }
  426. np = diag_long_to_dec(field, num_buff_len, lo, leading0, fsize);
  427. while (c = *np++) { *str++ = c;
  428. if (++i == size) { full = 1; break; }
  429. }
  430. }
  431. break;
  432. case 'u':
  433. #if HAVE_TYPE_LONGLONG
  434. if (lengthmod == 'L') {
  435. ull = va_arg(ap, unsigned long long);
  436. np = diag_ulonglong_to_dec(field, num_buff_len, ull, leading0, fsize);
  437. while (c = *np++) { *str++ = c;
  438. if (++i == size) { full = 1; break; }
  439. }
  440. } else
  441. #endif
  442. {
  443. switch (lengthmod) {
  444. case 'l': ulo = va_arg(ap, unsigned long); break;
  445. case 'z': ulo = va_arg(ap, size_t); break;
  446. default: ulo = va_arg(ap, unsigned int); break;
  447. }
  448. np = diag_ulong_to_dec(field, num_buff_len, ulo, leading0, fsize);
  449. while (c = *np++) { *str++ = c;
  450. if (++i == size) { full = 1; break; }
  451. }
  452. }
  453. break;
  454. case 'p':
  455. ulo = va_arg(ap, size_t);
  456. np = diag_ulong_to_hex(field, num_buff_len, ulo, leading0, fsize);
  457. *str++ = '0'; if (++i == size) { full = 1; break; }
  458. *str++ = 'x'; if (++i == size) { full = 1; break; }
  459. while (c = *np++) { *str++ = c;
  460. if (++i == size) { full = 1; break; }
  461. }
  462. break;
  463. case 'x':
  464. #if HAVE_TYPE_LONGLONG
  465. if (lengthmod == 'L') {
  466. ull = va_arg(ap, unsigned long long);
  467. np = diag_ulonglong_to_hex(field, num_buff_len, ull, leading0, fsize);
  468. while (c = *np++) { *str++ = c;
  469. if (++i == size) { full = 1; break; }
  470. }
  471. } else
  472. #endif
  473. {
  474. switch (lengthmod) {
  475. case 'l': ulo = va_arg(ap, unsigned long); break;
  476. case 'z': ulo = va_arg(ap, size_t); break;
  477. default: ulo = va_arg(ap, unsigned int); break;
  478. }
  479. np = diag_ulong_to_hex(field, num_buff_len, ulo, leading0, fsize);
  480. while (c = *np++) { *str++ = c;
  481. if (++i == size) { full = 1; break; }
  482. }
  483. }
  484. break;
  485. case 'o':
  486. #if HAVE_TYPE_LONGLONG
  487. if (lengthmod == 'L') {
  488. ull = va_arg(ap, unsigned long long);
  489. np = diag_ulonglong_to_oct(field, num_buff_len, ull, leading0, fsize);
  490. while (c = *np++) { *str++ = c;
  491. if (++i == size) break;
  492. }
  493. } else
  494. #endif
  495. {
  496. switch (lengthmod) {
  497. case 'l': ulo = va_arg(ap, unsigned long); break;
  498. case 'z': ulo = va_arg(ap, size_t); break;
  499. default: ulo = va_arg(ap, unsigned int); break;
  500. }
  501. np = diag_ulong_to_oct(field, num_buff_len, ulo, leading0, fsize);
  502. while (c = *np++) { *str++ = c;
  503. if (++i == size) { full = 1; break; }
  504. }
  505. }
  506. break;
  507. default:
  508. *str++ = c; if (++i == size) { full = 1; break; }
  509. }
  510. if (full) break;
  511. } else {
  512. *str++ = c;
  513. if (++i == size) break;
  514. }
  515. }
  516. *str = '\0';
  517. return i;
  518. }
  519. int snprintf_r(char *str, size_t size, const char *format, ...) {
  520. int result;
  521. va_list ap;
  522. va_start(ap, format);
  523. result = vsnprintf_r(str, size, format, ap);
  524. va_end(ap);
  525. return result;
  526. }