util.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. Copyright (c) 2003-2007 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "polipo.h"
  20. /* Note that this is different from GNU's strndup(3). */
  21. char *
  22. strdup_n(const char *restrict buf, int n)
  23. {
  24. char *s;
  25. s = malloc(n + 1);
  26. if(s == NULL)
  27. return NULL;
  28. memcpy(s, buf, n);
  29. s[n] = '\0';
  30. return s;
  31. }
  32. int
  33. snnprintf(char *restrict buf, int n, int len, const char *format, ...)
  34. {
  35. va_list args;
  36. int rc;
  37. va_start(args, format);
  38. rc = snnvprintf(buf, n, len, format, args);
  39. va_end(args);
  40. return rc;
  41. }
  42. int
  43. snnvprintf(char *restrict buf, int n, int len, const char *format, va_list args)
  44. {
  45. int rc = -1;
  46. va_list args_copy;
  47. if(n < 0) return -2;
  48. if(n < len) {
  49. va_copy(args_copy, args);
  50. rc = vsnprintf(buf + n, len - n, format, args_copy);
  51. va_end(args_copy);
  52. }
  53. if(rc >= 0 && n + rc <= len)
  54. return n + rc;
  55. else
  56. return -1;
  57. }
  58. int
  59. snnprint_n(char *restrict buf, int n, int len, const char *s, int slen)
  60. {
  61. int i = 0;
  62. if(n < 0) return -2;
  63. while(i < slen && n < len)
  64. buf[n++] = s[i++];
  65. if(n < len)
  66. return n;
  67. else
  68. return -1;
  69. }
  70. int
  71. letter(char c)
  72. {
  73. if(c >= 'A' && c <= 'Z') return 1;
  74. if(c >= 'a' && c <= 'z') return 1;
  75. return 0;
  76. }
  77. int
  78. digit(char c)
  79. {
  80. if(c >= '0' && c <= '9')
  81. return 1;
  82. return 0;
  83. }
  84. char
  85. lwr(char a)
  86. {
  87. if(a >= 'A' && a <= 'Z')
  88. return a | 0x20;
  89. else
  90. return a;
  91. }
  92. char *
  93. lwrcpy(char *restrict dst, const char *restrict src, int n)
  94. {
  95. int i;
  96. for(i = 0; i < n; i++)
  97. dst[i] = lwr(src[i]);
  98. return dst;
  99. }
  100. int
  101. lwrcmp(const char *as, const char *bs, int n)
  102. {
  103. int i;
  104. for(i = 0; i < n; i++) {
  105. char a = lwr(as[i]), b = lwr(bs[i]);
  106. if(a < b)
  107. return -1;
  108. else if(a > b)
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. int
  114. strcasecmp_n(const char *string, const char *buf, int n)
  115. {
  116. int i;
  117. i = 0;
  118. while(string[i] != '\0' && i < n) {
  119. char a = lwr(string[i]), b = lwr(buf[i]);
  120. if(a < b)
  121. return -1;
  122. else if(a > b)
  123. return 1;
  124. i++;
  125. }
  126. if(string[i] == '\0' && i == n)
  127. return 0;
  128. else if(i == n)
  129. return 1;
  130. else
  131. return -1;
  132. }
  133. int
  134. atoi_n(const char *restrict string, int n, int len, int *value_return)
  135. {
  136. int i = n;
  137. int val = 0;
  138. if(i >= len || !digit(string[i]))
  139. return -1;
  140. while(i < len && digit(string[i])) {
  141. val = val * 10 + (string[i] - '0');
  142. i++;
  143. }
  144. *value_return = val;
  145. return i;
  146. }
  147. int
  148. isWhitespace(const char *string)
  149. {
  150. while(*string != '\0') {
  151. if(*string == ' ' || *string == '\t')
  152. string++;
  153. else
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. #ifndef HAVE_MEMRCHR
  159. void *
  160. memrchr(const void *s, int c, size_t n)
  161. {
  162. const unsigned char *ss = s;
  163. unsigned char cc = c;
  164. size_t i;
  165. for(i = 1; i <= n; i++)
  166. if(ss[n - i] == cc)
  167. return (void*)(ss + n - i);
  168. return NULL;
  169. }
  170. #endif
  171. int
  172. h2i(char h)
  173. {
  174. if(h >= '0' && h <= '9')
  175. return h - '0';
  176. else if(h >= 'a' && h <= 'f')
  177. return h - 'a' + 10;
  178. else if(h >= 'A' && h <= 'F')
  179. return h - 'A' + 10;
  180. else
  181. return -1;
  182. }
  183. char
  184. i2h(int i)
  185. {
  186. if(i < 0 || i >= 16)
  187. return '?';
  188. if(i < 10)
  189. return i + '0';
  190. else
  191. return i - 10 + 'A';
  192. }
  193. /* floor(log2(x)) */
  194. int
  195. log2_floor(int x)
  196. {
  197. int i, j;
  198. assert(x > 0);
  199. i = 0;
  200. j = 1;
  201. while(2 * j <= x) {
  202. i++;
  203. j *= 2;
  204. }
  205. return i;
  206. }
  207. /* ceil(log2(x)) */
  208. int
  209. log2_ceil(int x)
  210. {
  211. int i, j;
  212. assert(x > 0);
  213. i = 0;
  214. j = 1;
  215. while(j < x) {
  216. i++;
  217. j *= 2;
  218. }
  219. return i;
  220. }
  221. #ifdef HAVE_ASPRINTF
  222. char *
  223. vsprintf_a(const char *f, va_list args)
  224. {
  225. char *r;
  226. int rc;
  227. va_list args_copy;
  228. va_copy(args_copy, args);
  229. rc = vasprintf(&r, f, args_copy);
  230. va_end(args_copy);
  231. if(rc < 0)
  232. return NULL;
  233. return r;
  234. }
  235. #else
  236. char*
  237. vsprintf_a(const char *f, va_list args)
  238. {
  239. int n, size;
  240. char buf[64];
  241. char *string;
  242. va_list args_copy;
  243. va_copy(args_copy, args);
  244. n = vsnprintf(buf, 64, f, args_copy);
  245. va_end(args_copy);
  246. if(n >= 0 && n < 64) {
  247. return strdup_n(buf, n);
  248. }
  249. if(n >= 64)
  250. size = n + 1;
  251. else
  252. size = 96;
  253. while(1) {
  254. string = malloc(size);
  255. if(!string)
  256. return NULL;
  257. va_copy(args_copy, args);
  258. n = vsnprintf(string, size, f, args_copy);
  259. va_end(args_copy);
  260. if(n >= 0 && n < size) {
  261. return string;
  262. } else if(n >= size)
  263. size = n + 1;
  264. else
  265. size = size * 3 / 2;
  266. free(string);
  267. if(size > 16 * 1024)
  268. return NULL;
  269. }
  270. /* NOTREACHED */
  271. }
  272. #endif
  273. char*
  274. sprintf_a(const char *f, ...)
  275. {
  276. char *s;
  277. va_list args;
  278. va_start(args, f);
  279. s = vsprintf_a(f, args);
  280. va_end(args);
  281. return s;
  282. }
  283. unsigned int
  284. hash(unsigned int seed, const void *restrict key, int key_size,
  285. unsigned int hash_size)
  286. {
  287. int i;
  288. unsigned int h;
  289. h = seed;
  290. for(i = 0; i < key_size; i++)
  291. h = (h << 5) + (h >> (hash_size - 5)) +
  292. ((unsigned char*)key)[i];
  293. return h & ((1 << hash_size) - 1);
  294. }
  295. char *
  296. pstrerror(int e)
  297. {
  298. char *s;
  299. static char buf[20];
  300. switch(e) {
  301. case EDOSHUTDOWN: s = "Immediate shutdown requested"; break;
  302. case EDOGRACEFUL: s = "Graceful shutdown requested"; break;
  303. case EDOTIMEOUT: s = "Timeout"; break;
  304. case ECLIENTRESET: s = "Connection reset by client"; break;
  305. case ESYNTAX: s = "Incorrect syntax"; break;
  306. case EREDIRECTOR: s = "Redirector error"; break;
  307. case EDNS_HOST_NOT_FOUND: s = "Host not found"; break;
  308. case EDNS_NO_ADDRESS: s = "No address"; break;
  309. case EDNS_NO_RECOVERY: s = "Permanent name server failure"; break;
  310. case EDNS_TRY_AGAIN: s = "Temporary name server failure"; break;
  311. case EDNS_INVALID: s = "Invalid reply from name server"; break;
  312. case EDNS_UNSUPPORTED: s = "Unsupported DNS reply"; break;
  313. case EDNS_FORMAT: s = "Invalid DNS query"; break;
  314. case EDNS_REFUSED: s = "DNS query refused by server"; break;
  315. case EDNS_CNAME_LOOP: s = "DNS CNAME loop"; break;
  316. #ifndef NO_SOCKS
  317. case ESOCKS_PROTOCOL: s = "SOCKS protocol error"; break;
  318. case ESOCKS_REJECT_FAIL: s = "SOCKS request rejected or failed"; break;
  319. case ESOCKS_REJECT_IDENTD: s = "SOCKS request rejected: "
  320. "server couldn't connect to identd";
  321. break;
  322. case ESOCKS_REJECT_UID_MISMATCH: s = "SOCKS request rejected: "
  323. "uid mismatch";
  324. break;
  325. case ESOCKS5_BASE: s = "SOCKS success"; break;
  326. case ESOCKS5_BASE + 1: s = "General SOCKS server failure"; break;
  327. case ESOCKS5_BASE + 2: s = "SOCKS connection not allowed"; break;
  328. case ESOCKS5_BASE + 3: s = "SOCKS error: network unreachable"; break;
  329. case ESOCKS5_BASE + 4: s = "SOCKS error: host unreachable"; break;
  330. case ESOCKS5_BASE + 5: s = "SOCKS error: connection refused"; break;
  331. case ESOCKS5_BASE + 6: s = "SOCKS error: TTL expired"; break;
  332. case ESOCKS5_BASE + 7: s = "SOCKS command not supported"; break;
  333. case ESOCKS5_BASE + 8: s = "SOCKS error: address type not supported";
  334. break;
  335. #endif
  336. case EUNKNOWN: s = "Unknown error"; break;
  337. default: s = NULL; break;
  338. }
  339. if(!s) s = strerror(e);
  340. #ifdef WIN32 /*MINGW*/
  341. if(!s) {
  342. if(e >= WSABASEERR && e <= WSABASEERR + 2000) {
  343. /* This should be okay, as long as the caller discards the
  344. pointer before another error occurs. */
  345. snprintf(buf, 20, "Winsock error %d", e);
  346. s = buf;
  347. }
  348. }
  349. #endif
  350. if(!s) {
  351. snprintf(buf, 20, "Unknown error %d", e);
  352. s = buf;
  353. }
  354. return s;
  355. }
  356. /* Like mktime(3), but UTC rather than local time */
  357. #if defined(HAVE_TIMEGM)
  358. time_t
  359. mktime_gmt(struct tm *tm)
  360. {
  361. return timegm(tm);
  362. }
  363. #elif defined(HAVE_MKGMTIME)
  364. time_t
  365. mktime_gmt(struct tm *tm)
  366. {
  367. return _mkgmtime(tm);
  368. }
  369. #elif defined(HAVE_TM_GMTOFF)
  370. time_t
  371. mktime_gmt(struct tm *tm)
  372. {
  373. time_t t;
  374. struct tm *ltm;
  375. t = mktime(tm);
  376. if(t < 0)
  377. return -1;
  378. ltm = localtime(&t);
  379. if(ltm == NULL)
  380. return -1;
  381. return t + ltm->tm_gmtoff;
  382. }
  383. #elif defined(HAVE_TZSET)
  384. #ifdef HAVE_SETENV
  385. /* Taken from the Linux timegm(3) man page. */
  386. time_t
  387. mktime_gmt(struct tm *tm)
  388. {
  389. time_t t;
  390. char *tz;
  391. tz = getenv("TZ");
  392. setenv("TZ", "GMT", 1);
  393. tzset();
  394. t = mktime(tm);
  395. if(tz)
  396. setenv("TZ", tz, 1);
  397. else
  398. unsetenv("TZ");
  399. tzset();
  400. return t;
  401. }
  402. #else
  403. time_t
  404. mktime_gmt(struct tm *tm)
  405. {
  406. time_t t;
  407. char *tz;
  408. static char *old_tz = NULL;
  409. tz = getenv("TZ");
  410. putenv("TZ=GMT");
  411. tzset();
  412. t = mktime(tm);
  413. if(old_tz)
  414. free(old_tz);
  415. if(tz)
  416. old_tz = sprintf_a("TZ=%s", tz);
  417. else
  418. old_tz = strdup("TZ"); /* XXX - non-portable? */
  419. if(old_tz)
  420. putenv(old_tz);
  421. tzset();
  422. return t;
  423. }
  424. #endif
  425. #else
  426. #error no mktime_gmt implementation on this platform
  427. #endif
  428. AtomPtr
  429. expandTilde(AtomPtr filename)
  430. {
  431. char *buf;
  432. char *home;
  433. int len;
  434. AtomPtr ret;
  435. if(filename == NULL || filename->length < 1 ||
  436. filename->string[0] != '~' || filename->string[1] != '/')
  437. return filename;
  438. home = getenv("HOME");
  439. if(home == NULL) {
  440. return NULL;
  441. }
  442. len = strlen(home);
  443. buf = malloc(len + 1 + 1 + filename->length - 2);
  444. if(buf == NULL) {
  445. do_log(L_ERROR, "Could not allocate buffer.\n");
  446. return NULL;
  447. }
  448. memcpy(buf, home, len);
  449. if(buf[len - 1] != '/')
  450. buf[len++] = '/';
  451. memcpy(buf + len, filename->string + 2, filename->length - 2);
  452. len += filename->length - 2;
  453. ret = internAtomN(buf, len);
  454. free(buf);
  455. if(ret != NULL)
  456. releaseAtom(filename);
  457. return ret;
  458. }
  459. #ifdef HAVE_FORK
  460. void
  461. do_daemonise(int noclose)
  462. {
  463. int rc;
  464. fflush(stdout);
  465. fflush(stderr);
  466. rc = fork();
  467. if(rc < 0) {
  468. do_log_error(L_ERROR, errno, "Couldn't fork");
  469. exit(1);
  470. }
  471. if(rc > 0)
  472. exit(0);
  473. if(!noclose) {
  474. int fd;
  475. close(0);
  476. close(1);
  477. close(2);
  478. /* Leaving the default file descriptors free is not a good
  479. idea, as it will cause library functions such as abort to
  480. thrash the on-disk cache. */
  481. fd = open("/dev/null", O_RDONLY);
  482. if(fd > 0) {
  483. dup2(fd, 0);
  484. close(fd);
  485. }
  486. fd = open("/dev/null", O_WRONLY);
  487. if(fd >= 0) {
  488. if(fd != 1)
  489. dup2(fd, 1);
  490. if(fd != 2)
  491. dup2(fd, 2);
  492. if(fd != 1 && fd != 2)
  493. close(fd);
  494. }
  495. }
  496. rc = setsid();
  497. if(rc < 0) {
  498. do_log_error(L_ERROR, errno, "Couldn't create new session");
  499. exit(1);
  500. }
  501. }
  502. #elif defined(WIN32)
  503. void
  504. do_daemonise(int noclose)
  505. {
  506. do_log(L_INFO, "Detaching console");
  507. FreeConsole();
  508. }
  509. #else
  510. void
  511. do_daemonise(int noclose)
  512. {
  513. do_log(L_ERROR, "Cannot daemonise on this platform");
  514. exit(1);
  515. }
  516. #endif
  517. void
  518. writePid(char *pidfile)
  519. {
  520. int fd, n, rc;
  521. char buf[16];
  522. fd = open(pidfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
  523. if(fd < 0) {
  524. do_log_error(L_ERROR, errno,
  525. "Couldn't create pid file %s", pidfile);
  526. exit(1);
  527. }
  528. n = snprintf(buf, 16, "%ld\n", (long)getpid());
  529. if(n < 0 || n >= 16) {
  530. close(fd);
  531. unlink(pidfile);
  532. do_log(L_ERROR, "Couldn't format pid.\n");
  533. exit(1);
  534. }
  535. rc = write(fd, buf, n);
  536. if(rc != n) {
  537. close(fd);
  538. unlink(pidfile);
  539. do_log_error(L_ERROR, errno, "Couldn't write pid");
  540. exit(1);
  541. }
  542. close(fd);
  543. return;
  544. }
  545. static const char b64[64] =
  546. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  547. /* "/" replaced with "-" */
  548. static const char b64fss[64] =
  549. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-";
  550. int
  551. b64cpy(char *restrict dst, const char *restrict src, int n, int fss)
  552. {
  553. const char *b = fss ? b64fss: b64;
  554. int i, j;
  555. j = 0;
  556. for(i = 0; i < n; i += 3) {
  557. unsigned char a0, a1, a2;
  558. a0 = src[i];
  559. a1 = i < n - 1 ? src[i + 1] : 0;
  560. a2 = i < n - 2 ? src[i + 2] : 0;
  561. dst[j++] = b[(a0 >> 2) & 0x3F];
  562. dst[j++] = b[((a0 << 4) & 0x30) | ((a1 >> 4) & 0x0F)];
  563. if(i < n - 1)
  564. dst[j++] = b[((a1 << 2) & 0x3C) | ((a2 >> 6) & 0x03)];
  565. else
  566. dst[j++] = '=';
  567. if(i < n - 2)
  568. dst[j++] = b[a2 & 0x3F];
  569. else
  570. dst[j++] = '=';
  571. }
  572. return j;
  573. }
  574. int
  575. b64cmp(const char *restrict a, int an, const char *restrict b, int bn)
  576. {
  577. char *buf;
  578. int r;
  579. if(an % 4 != 0)
  580. return -1;
  581. if((bn + 2) / 3 != an / 4)
  582. return -1;
  583. buf = malloc(an);
  584. if(buf == NULL)
  585. return -1;
  586. b64cpy(buf, b, bn, 0);
  587. r = memcmp(buf, a, an);
  588. free(buf);
  589. return r;
  590. }
  591. IntListPtr
  592. makeIntList(int size)
  593. {
  594. IntListPtr list;
  595. if(size <= 0)
  596. size = 4;
  597. list = malloc(sizeof(IntListRec));
  598. if(list == NULL)
  599. return NULL;
  600. list->ranges = malloc(size * sizeof(IntRangeRec));
  601. if(list->ranges == NULL) {
  602. free(list);
  603. return NULL;
  604. }
  605. list->length = 0;
  606. list->size = size;
  607. return list;
  608. }
  609. void
  610. destroyIntList(IntListPtr list)
  611. {
  612. free(list->ranges);
  613. free(list);
  614. }
  615. int
  616. intListMember(int n, IntListPtr list)
  617. {
  618. int lo = 0, hi = list->length - 1;
  619. int mid;
  620. while(hi >= lo) {
  621. mid = (hi + lo) / 2;
  622. if(list->ranges[mid].from > n)
  623. hi = mid - 1;
  624. else if(list->ranges[mid].to < n)
  625. lo = mid + 1;
  626. else
  627. return 1;
  628. }
  629. return 0;
  630. }
  631. static int
  632. deleteRange(IntListPtr list, int i)
  633. {
  634. assert(list->length > i);
  635. if(list->length > i + 1)
  636. memmove(list->ranges + i, list->ranges + i + 1,
  637. (list->length - i - 1) * sizeof(IntRangeRec));
  638. list->length--;
  639. return 1;
  640. }
  641. static int
  642. insertRange(int from, int to, IntListPtr list, int i)
  643. {
  644. assert(i >= 0 && i <= list->length);
  645. assert(i == 0 || list->ranges[i - 1].to < from - 1);
  646. assert(i == list->length || list->ranges[i].from > to + 1);
  647. if(list->length >= list->size) {
  648. int newsize = list->size * 2 + 1;
  649. IntRangePtr newranges =
  650. realloc(list->ranges, newsize * sizeof(IntRangeRec));
  651. if(newranges == NULL)
  652. return -1;
  653. list->size = newsize;
  654. list->ranges = newranges;
  655. }
  656. if(i < list->length)
  657. memmove(list->ranges + i + 1, list->ranges + i,
  658. list->length - i);
  659. list->length++;
  660. list->ranges[i].from = from;
  661. list->ranges[i].to = to;
  662. return 1;
  663. }
  664. static int
  665. maybeMergeRanges(IntListPtr list, int i)
  666. {
  667. int rc;
  668. while(i > 0 && list->ranges[i].from <= list->ranges[i - 1].to + 1) {
  669. list->ranges[i - 1].from =
  670. MIN(list->ranges[i - 1].from, list->ranges[i].from);
  671. list->ranges[i - 1].to =
  672. MAX(list->ranges[i - 1].to, list->ranges[i].to);
  673. rc = deleteRange(list, i);
  674. if(rc < 0) return -1;
  675. i--;
  676. }
  677. while(i < list->length - 1 &&
  678. list->ranges[i].to >= list->ranges[i + 1].from - 1) {
  679. list->ranges[i + 1].from =
  680. MIN(list->ranges[i + 1].from, list->ranges[i].from);
  681. list->ranges[i - 1].to =
  682. MAX(list->ranges[i + 1].to, list->ranges[i].to);
  683. rc = deleteRange(list, i);
  684. if(rc < 0) return -1;
  685. }
  686. return 1;
  687. }
  688. int
  689. intListCons(int from, int to, IntListPtr list)
  690. {
  691. int i;
  692. /* Don't bother with the dichotomy. */
  693. for(i = 0; i < list->length; i++) {
  694. if(list->ranges[i].to >= from - 1)
  695. break;
  696. }
  697. if(i < list->length &&
  698. (from >= list->ranges[i].from - 1 || to <= list->ranges[i].to + 1)) {
  699. if(from <= list->ranges[i].from)
  700. list->ranges[i].from = from;
  701. if(to >= list->ranges[i].to)
  702. list->ranges[i].to = to;
  703. return maybeMergeRanges(list, i);
  704. }
  705. return insertRange(from, to, list, i);
  706. }
  707. /* Return the amount of physical memory on the box, -1 if unknown or
  708. over two gigs. */
  709. #ifdef __linux
  710. #include <sys/sysinfo.h>
  711. int
  712. physicalMemory()
  713. {
  714. int rc;
  715. struct sysinfo info;
  716. rc = sysinfo(&info);
  717. if(rc < 0)
  718. return -1;
  719. if(info.totalram <= 0x7fffffff / info.mem_unit)
  720. return (int)(info.totalram * info.mem_unit);
  721. return -1;
  722. }
  723. #elif defined(__FreeBSD__)
  724. #include <sys/sysctl.h>
  725. int
  726. physicalMemory()
  727. {
  728. unsigned long membytes;
  729. size_t len;
  730. int res;
  731. len = sizeof(membytes);
  732. res = sysctlbyname("hw.physmem", &membytes, &len, NULL, 0);
  733. if (res || membytes > INT_MAX)
  734. return -1;
  735. return (int)membytes;
  736. }
  737. #else
  738. int
  739. physicalMemory()
  740. {
  741. return -1;
  742. }
  743. #endif