misc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /* Miscellaneous functions, not really specific to GNU tar.
  2. Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <system.h>
  16. #include <rmt.h>
  17. #include "common.h"
  18. #include <quotearg.h>
  19. #include <save-cwd.h>
  20. #include <xgetcwd.h>
  21. #include <unlinkdir.h>
  22. #include <utimens.h>
  23. #include <canonicalize.h>
  24. #if HAVE_STROPTS_H
  25. # include <stropts.h>
  26. #endif
  27. #if HAVE_SYS_FILIO_H
  28. # include <sys/filio.h>
  29. #endif
  30. /* Handling strings. */
  31. /* Assign STRING to a copy of VALUE if not zero, or to zero. If
  32. STRING was nonzero, it is freed first. */
  33. void
  34. assign_string (char **string, const char *value)
  35. {
  36. if (*string)
  37. free (*string);
  38. *string = value ? xstrdup (value) : 0;
  39. }
  40. /* Allocate a copy of the string quoted as in C, and returns that. If
  41. the string does not have to be quoted, it returns a null pointer.
  42. The allocated copy should normally be freed with free() after the
  43. caller is done with it.
  44. This is used in one context only: generating the directory file in
  45. incremental dumps. The quoted string is not intended for human
  46. consumption; it is intended only for unquote_string. The quoting
  47. is locale-independent, so that users needn't worry about locale
  48. when reading directory files. This means that we can't use
  49. quotearg, as quotearg is locale-dependent and is meant for human
  50. consumption. */
  51. char *
  52. quote_copy_string (const char *string)
  53. {
  54. const char *source = string;
  55. char *destination = 0;
  56. char *buffer = 0;
  57. int copying = 0;
  58. while (*source)
  59. {
  60. int character = *source++;
  61. switch (character)
  62. {
  63. case '\n': case '\\':
  64. if (!copying)
  65. {
  66. size_t length = (source - string) - 1;
  67. copying = 1;
  68. buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
  69. memcpy (buffer, string, length);
  70. destination = buffer + length;
  71. }
  72. *destination++ = '\\';
  73. *destination++ = character == '\\' ? '\\' : 'n';
  74. break;
  75. default:
  76. if (copying)
  77. *destination++ = character;
  78. break;
  79. }
  80. }
  81. if (copying)
  82. {
  83. *destination = '\0';
  84. return buffer;
  85. }
  86. return 0;
  87. }
  88. /* Takes a quoted C string (like those produced by quote_copy_string)
  89. and turns it back into the un-quoted original. This is done in
  90. place. Returns 0 only if the string was not properly quoted, but
  91. completes the unquoting anyway.
  92. This is used for reading the saved directory file in incremental
  93. dumps. It is used for decoding old `N' records (demangling names).
  94. But also, it is used for decoding file arguments, would they come
  95. from the shell or a -T file, and for decoding the --exclude
  96. argument. */
  97. int
  98. unquote_string (char *string)
  99. {
  100. int result = 1;
  101. char *source = string;
  102. char *destination = string;
  103. /* Escape sequences other than \\ and \n are no longer generated by
  104. quote_copy_string, but accept them for backwards compatibility,
  105. and also because unquote_string is used for purposes other than
  106. parsing the output of quote_copy_string. */
  107. while (*source)
  108. if (*source == '\\')
  109. switch (*++source)
  110. {
  111. case '\\':
  112. *destination++ = '\\';
  113. source++;
  114. break;
  115. case 'a':
  116. *destination++ = '\a';
  117. source++;
  118. break;
  119. case 'b':
  120. *destination++ = '\b';
  121. source++;
  122. break;
  123. case 'f':
  124. *destination++ = '\f';
  125. source++;
  126. break;
  127. case 'n':
  128. *destination++ = '\n';
  129. source++;
  130. break;
  131. case 'r':
  132. *destination++ = '\r';
  133. source++;
  134. break;
  135. case 't':
  136. *destination++ = '\t';
  137. source++;
  138. break;
  139. case 'v':
  140. *destination++ = '\v';
  141. source++;
  142. break;
  143. case '?':
  144. *destination++ = 0177;
  145. source++;
  146. break;
  147. case '0':
  148. case '1':
  149. case '2':
  150. case '3':
  151. case '4':
  152. case '5':
  153. case '6':
  154. case '7':
  155. {
  156. int value = *source++ - '0';
  157. if (*source < '0' || *source > '7')
  158. {
  159. *destination++ = value;
  160. break;
  161. }
  162. value = value * 8 + *source++ - '0';
  163. if (*source < '0' || *source > '7')
  164. {
  165. *destination++ = value;
  166. break;
  167. }
  168. value = value * 8 + *source++ - '0';
  169. *destination++ = value;
  170. break;
  171. }
  172. default:
  173. result = 0;
  174. *destination++ = '\\';
  175. if (*source)
  176. *destination++ = *source++;
  177. break;
  178. }
  179. else if (source != destination)
  180. *destination++ = *source++;
  181. else
  182. source++, destination++;
  183. if (source != destination)
  184. *destination = '\0';
  185. return result;
  186. }
  187. /* Zap trailing slashes. */
  188. char *
  189. zap_slashes (char *name)
  190. {
  191. char *q;
  192. if (!name || *name == 0)
  193. return name;
  194. q = name + strlen (name) - 1;
  195. while (q > name && ISSLASH (*q))
  196. *q-- = '\0';
  197. return name;
  198. }
  199. char *
  200. normalize_filename (const char *name)
  201. {
  202. return zap_slashes (canonicalize_filename_mode (name, CAN_MISSING));
  203. }
  204. void
  205. replace_prefix (char **pname, const char *samp, size_t slen,
  206. const char *repl, size_t rlen)
  207. {
  208. char *name = *pname;
  209. size_t nlen = strlen (name);
  210. if (nlen > slen && memcmp (name, samp, slen) == 0 && ISSLASH (name[slen]))
  211. {
  212. if (rlen > slen)
  213. {
  214. name = xrealloc (name, nlen - slen + rlen + 1);
  215. *pname = name;
  216. }
  217. memmove (name + rlen, name + slen, nlen - slen + 1);
  218. memcpy (name, repl, rlen);
  219. }
  220. }
  221. /* Handling numbers. */
  222. /* Output fraction and trailing digits appropriate for a nanoseconds
  223. count equal to NS, but don't output unnecessary '.' or trailing
  224. zeros. */
  225. void
  226. code_ns_fraction (int ns, char *p)
  227. {
  228. if (ns == 0)
  229. *p = '\0';
  230. else
  231. {
  232. int i = 9;
  233. *p++ = '.';
  234. while (ns % 10 == 0)
  235. {
  236. ns /= 10;
  237. i--;
  238. }
  239. p[i] = '\0';
  240. for (;;)
  241. {
  242. p[--i] = '0' + ns % 10;
  243. if (i == 0)
  244. break;
  245. ns /= 10;
  246. }
  247. }
  248. }
  249. char const *
  250. code_timespec (struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
  251. {
  252. time_t s = t.tv_sec;
  253. int ns = t.tv_nsec;
  254. char *np;
  255. bool negative = s < 0;
  256. if (negative && ns != 0)
  257. {
  258. s++;
  259. ns = BILLION - ns;
  260. }
  261. np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1);
  262. if (negative)
  263. *--np = '-';
  264. code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND);
  265. return np;
  266. }
  267. /* File handling. */
  268. /* Saved names in case backup needs to be undone. */
  269. static char *before_backup_name;
  270. static char *after_backup_name;
  271. /* Return 1 if FILE_NAME is obviously "." or "/". */
  272. static bool
  273. must_be_dot_or_slash (char const *file_name)
  274. {
  275. file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
  276. if (ISSLASH (file_name[0]))
  277. {
  278. for (;;)
  279. if (ISSLASH (file_name[1]))
  280. file_name++;
  281. else if (file_name[1] == '.'
  282. && ISSLASH (file_name[2 + (file_name[2] == '.')]))
  283. file_name += 2 + (file_name[2] == '.');
  284. else
  285. return ! file_name[1];
  286. }
  287. else
  288. {
  289. while (file_name[0] == '.' && ISSLASH (file_name[1]))
  290. {
  291. file_name += 2;
  292. while (ISSLASH (*file_name))
  293. file_name++;
  294. }
  295. return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
  296. }
  297. }
  298. /* Some implementations of rmdir let you remove '.' or '/'.
  299. Report an error with errno set to zero for obvious cases of this;
  300. otherwise call rmdir. */
  301. static int
  302. safer_rmdir (const char *file_name)
  303. {
  304. if (must_be_dot_or_slash (file_name))
  305. {
  306. errno = 0;
  307. return -1;
  308. }
  309. return rmdir (file_name);
  310. }
  311. /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
  312. then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
  313. recursively; otherwise, remove it only if it is empty. If FILE_NAME is
  314. a directory that cannot be removed (e.g., because it is nonempty)
  315. and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
  316. Return 0 on error, with errno set; if FILE_NAME is obviously the working
  317. directory return zero with errno set to zero. */
  318. int
  319. remove_any_file (const char *file_name, enum remove_option option)
  320. {
  321. /* Try unlink first if we cannot unlink directories, as this saves
  322. us a system call in the common case where we're removing a
  323. non-directory. */
  324. bool try_unlink_first = cannot_unlink_dir ();
  325. if (try_unlink_first)
  326. {
  327. if (unlink (file_name) == 0)
  328. return 1;
  329. /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
  330. directory without appropriate privileges, but many Linux
  331. kernels return the more-sensible EISDIR. */
  332. if (errno != EPERM && errno != EISDIR)
  333. return 0;
  334. }
  335. if (safer_rmdir (file_name) == 0)
  336. return 1;
  337. switch (errno)
  338. {
  339. case ENOTDIR:
  340. return !try_unlink_first && unlink (file_name) == 0;
  341. case 0:
  342. case EEXIST:
  343. #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
  344. case ENOTEMPTY:
  345. #endif
  346. switch (option)
  347. {
  348. case ORDINARY_REMOVE_OPTION:
  349. break;
  350. case WANT_DIRECTORY_REMOVE_OPTION:
  351. return -1;
  352. case RECURSIVE_REMOVE_OPTION:
  353. {
  354. char *directory = savedir (file_name);
  355. char const *entry;
  356. size_t entrylen;
  357. if (! directory)
  358. return 0;
  359. for (entry = directory;
  360. (entrylen = strlen (entry)) != 0;
  361. entry += entrylen + 1)
  362. {
  363. char *file_name_buffer = new_name (file_name, entry);
  364. int r = remove_any_file (file_name_buffer,
  365. RECURSIVE_REMOVE_OPTION);
  366. int e = errno;
  367. free (file_name_buffer);
  368. if (! r)
  369. {
  370. free (directory);
  371. errno = e;
  372. return 0;
  373. }
  374. }
  375. free (directory);
  376. return safer_rmdir (file_name) == 0;
  377. }
  378. }
  379. break;
  380. }
  381. return 0;
  382. }
  383. /* Check if FILE_NAME already exists and make a backup of it right now.
  384. Return success (nonzero) only if the backup is either unneeded, or
  385. successful. For now, directories are considered to never need
  386. backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
  387. so, we do not have to backup block or character devices, nor remote
  388. entities. */
  389. bool
  390. maybe_backup_file (const char *file_name, bool this_is_the_archive)
  391. {
  392. struct stat file_stat;
  393. assign_string (&before_backup_name, file_name);
  394. /* A run situation may exist between Emacs or other GNU programs trying to
  395. make a backup for the same file simultaneously. If theoretically
  396. possible, real problems are unlikely. Doing any better would require a
  397. convention, GNU-wide, for all programs doing backups. */
  398. assign_string (&after_backup_name, 0);
  399. /* Check if we really need to backup the file. */
  400. if (this_is_the_archive && _remdev (file_name))
  401. return true;
  402. if (stat (file_name, &file_stat))
  403. {
  404. if (errno == ENOENT)
  405. return true;
  406. stat_error (file_name);
  407. return false;
  408. }
  409. if (S_ISDIR (file_stat.st_mode))
  410. return true;
  411. if (this_is_the_archive
  412. && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
  413. return true;
  414. after_backup_name = find_backup_file_name (file_name, backup_type);
  415. if (! after_backup_name)
  416. xalloc_die ();
  417. if (rename (before_backup_name, after_backup_name) == 0)
  418. {
  419. if (verbose_option)
  420. fprintf (stdlis, _("Renaming %s to %s\n"),
  421. quote_n (0, before_backup_name),
  422. quote_n (1, after_backup_name));
  423. return true;
  424. }
  425. else
  426. {
  427. /* The backup operation failed. */
  428. int e = errno;
  429. ERROR ((0, e, _("%s: Cannot rename to %s"),
  430. quotearg_colon (before_backup_name),
  431. quote_n (1, after_backup_name)));
  432. assign_string (&after_backup_name, 0);
  433. return false;
  434. }
  435. }
  436. /* Try to restore the recently backed up file to its original name.
  437. This is usually only needed after a failed extraction. */
  438. void
  439. undo_last_backup (void)
  440. {
  441. if (after_backup_name)
  442. {
  443. if (rename (after_backup_name, before_backup_name) != 0)
  444. {
  445. int e = errno;
  446. ERROR ((0, e, _("%s: Cannot rename to %s"),
  447. quotearg_colon (after_backup_name),
  448. quote_n (1, before_backup_name)));
  449. }
  450. if (verbose_option)
  451. fprintf (stdlis, _("Renaming %s back to %s\n"),
  452. quote_n (0, after_backup_name),
  453. quote_n (1, before_backup_name));
  454. assign_string (&after_backup_name, 0);
  455. }
  456. }
  457. /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
  458. int
  459. deref_stat (bool deref, char const *name, struct stat *buf)
  460. {
  461. return deref ? stat (name, buf) : lstat (name, buf);
  462. }
  463. /* Set FD's (i.e., FILE's) access time to TIMESPEC[0]. If that's not
  464. possible to do by itself, set its access and data modification
  465. times to TIMESPEC[0] and TIMESPEC[1], respectively. */
  466. int
  467. set_file_atime (int fd, char const *file, struct timespec const timespec[2])
  468. {
  469. #ifdef _FIOSATIME
  470. if (0 <= fd)
  471. {
  472. struct timeval timeval;
  473. timeval.tv_sec = timespec[0].tv_sec;
  474. timeval.tv_usec = timespec[0].tv_nsec / 1000;
  475. if (ioctl (fd, _FIOSATIME, &timeval) == 0)
  476. return 0;
  477. }
  478. #endif
  479. return gl_futimens (fd, file, timespec);
  480. }
  481. /* A description of a working directory. */
  482. struct wd
  483. {
  484. char const *name;
  485. int saved;
  486. struct saved_cwd saved_cwd;
  487. };
  488. /* A vector of chdir targets. wd[0] is the initial working directory. */
  489. static struct wd *wd;
  490. /* The number of working directories in the vector. */
  491. static size_t wd_count;
  492. /* The allocated size of the vector. */
  493. static size_t wd_alloc;
  494. int
  495. chdir_count ()
  496. {
  497. if (wd_count == 0)
  498. return wd_count;
  499. return wd_count - 1;
  500. }
  501. /* DIR is the operand of a -C option; add it to vector of chdir targets,
  502. and return the index of its location. */
  503. int
  504. chdir_arg (char const *dir)
  505. {
  506. if (wd_count == wd_alloc)
  507. {
  508. if (wd_alloc == 0)
  509. {
  510. wd_alloc = 2;
  511. wd = xmalloc (sizeof *wd * wd_alloc);
  512. }
  513. else
  514. wd = x2nrealloc (wd, &wd_alloc, sizeof *wd);
  515. if (! wd_count)
  516. {
  517. wd[wd_count].name = ".";
  518. wd[wd_count].saved = 0;
  519. wd_count++;
  520. }
  521. }
  522. /* Optimize the common special case of the working directory,
  523. or the working directory as a prefix. */
  524. if (dir[0])
  525. {
  526. while (dir[0] == '.' && ISSLASH (dir[1]))
  527. for (dir += 2; ISSLASH (*dir); dir++)
  528. continue;
  529. if (! dir[dir[0] == '.'])
  530. return wd_count - 1;
  531. }
  532. wd[wd_count].name = dir;
  533. wd[wd_count].saved = 0;
  534. return wd_count++;
  535. }
  536. /* Change to directory I. If I is 0, change to the initial working
  537. directory; otherwise, I must be a value returned by chdir_arg. */
  538. void
  539. chdir_do (int i)
  540. {
  541. static int previous;
  542. if (previous != i)
  543. {
  544. struct wd *prev = &wd[previous];
  545. struct wd *curr = &wd[i];
  546. if (! prev->saved)
  547. {
  548. int err = 0;
  549. prev->saved = 1;
  550. if (save_cwd (&prev->saved_cwd) != 0)
  551. err = errno;
  552. else if (0 <= prev->saved_cwd.desc)
  553. {
  554. /* Make sure we still have at least one descriptor available. */
  555. int fd1 = prev->saved_cwd.desc;
  556. int fd2 = dup (fd1);
  557. if (0 <= fd2)
  558. close (fd2);
  559. else if (errno == EMFILE)
  560. {
  561. /* Force restore_cwd to use chdir_long. */
  562. close (fd1);
  563. prev->saved_cwd.desc = -1;
  564. prev->saved_cwd.name = xgetcwd ();
  565. }
  566. else
  567. err = errno;
  568. }
  569. if (err)
  570. FATAL_ERROR ((0, err, _("Cannot save working directory")));
  571. }
  572. if (curr->saved)
  573. {
  574. if (restore_cwd (&curr->saved_cwd))
  575. FATAL_ERROR ((0, 0, _("Cannot change working directory")));
  576. }
  577. else
  578. {
  579. if (i && ! ISSLASH (curr->name[0]))
  580. chdir_do (i - 1);
  581. if (chdir (curr->name) != 0)
  582. chdir_fatal (curr->name);
  583. }
  584. previous = i;
  585. }
  586. }
  587. void
  588. close_diag (char const *name)
  589. {
  590. if (ignore_failed_read_option)
  591. close_warn (name);
  592. else
  593. close_error (name);
  594. }
  595. void
  596. open_diag (char const *name)
  597. {
  598. if (ignore_failed_read_option)
  599. open_warn (name);
  600. else
  601. open_error (name);
  602. }
  603. void
  604. read_diag_details (char const *name, off_t offset, size_t size)
  605. {
  606. if (ignore_failed_read_option)
  607. read_warn_details (name, offset, size);
  608. else
  609. read_error_details (name, offset, size);
  610. }
  611. void
  612. readlink_diag (char const *name)
  613. {
  614. if (ignore_failed_read_option)
  615. readlink_warn (name);
  616. else
  617. readlink_error (name);
  618. }
  619. void
  620. savedir_diag (char const *name)
  621. {
  622. if (ignore_failed_read_option)
  623. savedir_warn (name);
  624. else
  625. savedir_error (name);
  626. }
  627. void
  628. seek_diag_details (char const *name, off_t offset)
  629. {
  630. if (ignore_failed_read_option)
  631. seek_warn_details (name, offset);
  632. else
  633. seek_error_details (name, offset);
  634. }
  635. void
  636. stat_diag (char const *name)
  637. {
  638. if (ignore_failed_read_option)
  639. stat_warn (name);
  640. else
  641. stat_error (name);
  642. }
  643. void
  644. file_removed_diag (const char *name, bool top_level,
  645. void (*diagfn) (char const *name))
  646. {
  647. if (!top_level && errno == ENOENT)
  648. WARNOPT (WARN_FILE_REMOVED,
  649. (0, 0, _("%s: File removed before we read it"),
  650. quotearg_colon (name)));
  651. else
  652. diagfn (name);
  653. }
  654. void
  655. dir_removed_diag (const char *name, bool top_level,
  656. void (*diagfn) (char const *name))
  657. {
  658. if (!top_level && errno == ENOENT)
  659. WARNOPT (WARN_FILE_REMOVED,
  660. (0, 0, _("%s: Directory removed before we read it"),
  661. quotearg_colon (name)));
  662. else
  663. diagfn (name);
  664. }
  665. void
  666. write_fatal_details (char const *name, ssize_t status, size_t size)
  667. {
  668. write_error_details (name, status, size);
  669. fatal_exit ();
  670. }
  671. /* Fork, aborting if unsuccessful. */
  672. pid_t
  673. xfork (void)
  674. {
  675. pid_t p = fork ();
  676. if (p == (pid_t) -1)
  677. call_arg_fatal ("fork", _("child process"));
  678. return p;
  679. }
  680. /* Create a pipe, aborting if unsuccessful. */
  681. void
  682. xpipe (int fd[2])
  683. {
  684. if (pipe (fd) < 0)
  685. call_arg_fatal ("pipe", _("interprocess channel"));
  686. }
  687. /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
  688. ALIGNMENT must be nonzero. The caller must arrange for ((char *)
  689. PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
  690. locations. */
  691. static inline void *
  692. ptr_align (void *ptr, size_t alignment)
  693. {
  694. char *p0 = ptr;
  695. char *p1 = p0 + alignment - 1;
  696. return p1 - (size_t) p1 % alignment;
  697. }
  698. /* Return the address of a page-aligned buffer of at least SIZE bytes.
  699. The caller should free *PTR when done with the buffer. */
  700. void *
  701. page_aligned_alloc (void **ptr, size_t size)
  702. {
  703. size_t alignment = getpagesize ();
  704. size_t size1 = size + alignment;
  705. if (size1 < size)
  706. xalloc_die ();
  707. *ptr = xmalloc (size1);
  708. return ptr_align (*ptr, alignment);
  709. }