]> Dogcows Code - chaz/tar/blob - src/misc.c
Started merging with cpio into paxutils.
[chaz/tar] / src / misc.c
1 /* Miscellaneous functions, not really specific to GNU tar.
2
3 Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004 Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include <system.h>
21 #include <rmt.h>
22 #include "common.h"
23 #include <quotearg.h>
24 #include <save-cwd.h>
25
26 static void call_arg_fatal (char const *, char const *)
27 __attribute__ ((noreturn));
28 \f
29 /* Handling strings. */
30
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
41 /* Allocate a copy of the string quoted as in C, and returns that. If
42 the string does not have to be quoted, it returns a null pointer.
43 The allocated copy should normally be freed with free() after the
44 caller is done with it.
45
46 This is used in one context only: generating the directory file in
47 incremental dumps. The quoted string is not intended for human
48 consumption; it is intended only for unquote_string. The quoting
49 is locale-independent, so that users needn't worry about locale
50 when reading directory files. This means that we can't use
51 quotearg, as quotearg is locale-dependent and is meant for human
52 consumption. */
53 char *
54 quote_copy_string (const char *string)
55 {
56 const char *source = string;
57 char *destination = 0;
58 char *buffer = 0;
59 int copying = 0;
60
61 while (*source)
62 {
63 int character = *source++;
64
65 switch (character)
66 {
67 case '\n': case '\\':
68 if (!copying)
69 {
70 size_t length = (source - string) - 1;
71
72 copying = 1;
73 buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
74 memcpy (buffer, string, length);
75 destination = buffer + length;
76 }
77 *destination++ = '\\';
78 *destination++ = character == '\\' ? '\\' : 'n';
79 break;
80
81 default:
82 if (copying)
83 *destination++ = character;
84 break;
85 }
86 }
87 if (copying)
88 {
89 *destination = '\0';
90 return buffer;
91 }
92 return 0;
93 }
94
95 /* Takes a quoted C string (like those produced by quote_copy_string)
96 and turns it back into the un-quoted original. This is done in
97 place. Returns 0 only if the string was not properly quoted, but
98 completes the unquoting anyway.
99
100 This is used for reading the saved directory file in incremental
101 dumps. It is used for decoding old `N' records (demangling names).
102 But also, it is used for decoding file arguments, would they come
103 from the shell or a -T file, and for decoding the --exclude
104 argument. */
105 int
106 unquote_string (char *string)
107 {
108 int result = 1;
109 char *source = string;
110 char *destination = string;
111
112 /* Escape sequences other than \\ and \n are no longer generated by
113 quote_copy_string, but accept them for backwards compatibility,
114 and also because unquote_string is used for purposes other than
115 parsing the output of quote_copy_string. */
116
117 while (*source)
118 if (*source == '\\')
119 switch (*++source)
120 {
121 case '\\':
122 *destination++ = '\\';
123 source++;
124 break;
125
126 case 'a':
127 *destination++ = '\a';
128 source++;
129 break;
130
131 case 'b':
132 *destination++ = '\b';
133 source++;
134 break;
135
136 case 'f':
137 *destination++ = '\f';
138 source++;
139 break;
140
141 case 'n':
142 *destination++ = '\n';
143 source++;
144 break;
145
146 case 'r':
147 *destination++ = '\r';
148 source++;
149 break;
150
151 case 't':
152 *destination++ = '\t';
153 source++;
154 break;
155
156 case 'v':
157 *destination++ = '\v';
158 source++;
159 break;
160
161 case '?':
162 *destination++ = 0177;
163 source++;
164 break;
165
166 case '0':
167 case '1':
168 case '2':
169 case '3':
170 case '4':
171 case '5':
172 case '6':
173 case '7':
174 {
175 int value = *source++ - '0';
176
177 if (*source < '0' || *source > '7')
178 {
179 *destination++ = value;
180 break;
181 }
182 value = value * 8 + *source++ - '0';
183 if (*source < '0' || *source > '7')
184 {
185 *destination++ = value;
186 break;
187 }
188 value = value * 8 + *source++ - '0';
189 *destination++ = value;
190 break;
191 }
192
193 default:
194 result = 0;
195 *destination++ = '\\';
196 if (*source)
197 *destination++ = *source++;
198 break;
199 }
200 else if (source != destination)
201 *destination++ = *source++;
202 else
203 source++, destination++;
204
205 if (source != destination)
206 *destination = '\0';
207 return result;
208 }
209 \f
210 /* File handling. */
211
212 /* Saved names in case backup needs to be undone. */
213 static char *before_backup_name;
214 static char *after_backup_name;
215
216 /* Return 1 if FILE_NAME is obviously "." or "/". */
217 static bool
218 must_be_dot_or_slash (char const *file_name)
219 {
220 file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
221
222 if (ISSLASH (file_name[0]))
223 {
224 for (;;)
225 if (ISSLASH (file_name[1]))
226 file_name++;
227 else if (file_name[1] == '.'
228 && ISSLASH (file_name[2 + (file_name[2] == '.')]))
229 file_name += 2 + (file_name[2] == '.');
230 else
231 return ! file_name[1];
232 }
233 else
234 {
235 while (file_name[0] == '.' && ISSLASH (file_name[1]))
236 {
237 file_name += 2;
238 while (ISSLASH (*file_name))
239 file_name++;
240 }
241
242 return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
243 }
244 }
245
246 /* Some implementations of rmdir let you remove '.' or '/'.
247 Report an error with errno set to zero for obvious cases of this;
248 otherwise call rmdir. */
249 static int
250 safer_rmdir (const char *file_name)
251 {
252 if (must_be_dot_or_slash (file_name))
253 {
254 errno = 0;
255 return -1;
256 }
257
258 return rmdir (file_name);
259 }
260
261 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
262 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
263 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
264 a directory that cannot be removed (e.g., because it is nonempty)
265 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
266 Return 0 on error, with errno set; if FILE_NAME is obviously the working
267 directory return zero with errno set to zero. */
268 int
269 remove_any_file (const char *file_name, enum remove_option option)
270 {
271 /* Try unlink first if we are not root, as this saves us a system
272 call in the common case where we're removing a non-directory. */
273 if (! we_are_root)
274 {
275 if (unlink (file_name) == 0)
276 return 1;
277
278 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
279 directory without appropriate privileges, but many Linux
280 kernels return the more-sensible EISDIR. */
281 if (errno != EPERM && errno != EISDIR)
282 return 0;
283 }
284
285 if (safer_rmdir (file_name) == 0)
286 return 1;
287
288 switch (errno)
289 {
290 case ENOTDIR:
291 return we_are_root && unlink (file_name) == 0;
292
293 case 0:
294 case EEXIST:
295 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
296 case ENOTEMPTY:
297 #endif
298 switch (option)
299 {
300 case ORDINARY_REMOVE_OPTION:
301 break;
302
303 case WANT_DIRECTORY_REMOVE_OPTION:
304 return -1;
305
306 case RECURSIVE_REMOVE_OPTION:
307 {
308 char *directory = savedir (file_name);
309 char const *entry;
310 size_t entrylen;
311
312 if (! directory)
313 return 0;
314
315 for (entry = directory;
316 (entrylen = strlen (entry)) != 0;
317 entry += entrylen + 1)
318 {
319 char *file_name_buffer = new_name (file_name, entry);
320 int r = remove_any_file (file_name_buffer, 1);
321 int e = errno;
322 free (file_name_buffer);
323
324 if (! r)
325 {
326 free (directory);
327 errno = e;
328 return 0;
329 }
330 }
331
332 free (directory);
333 return safer_rmdir (file_name) == 0;
334 }
335 }
336 break;
337 }
338
339 return 0;
340 }
341
342 /* Check if FILE_NAME already exists and make a backup of it right now.
343 Return success (nonzero) only if the backup is either unneeded, or
344 successful. For now, directories are considered to never need
345 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
346 so, we do not have to backup block or character devices, nor remote
347 entities. */
348 bool
349 maybe_backup_file (const char *file_name, int this_is_the_archive)
350 {
351 struct stat file_stat;
352
353 /* Check if we really need to backup the file. */
354
355 if (this_is_the_archive && _remdev (file_name))
356 return true;
357
358 if (stat (file_name, &file_stat))
359 {
360 if (errno == ENOENT)
361 return true;
362
363 stat_error (file_name);
364 return false;
365 }
366
367 if (S_ISDIR (file_stat.st_mode))
368 return true;
369
370 if (this_is_the_archive
371 && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
372 return true;
373
374 assign_string (&before_backup_name, file_name);
375
376 /* A run situation may exist between Emacs or other GNU programs trying to
377 make a backup for the same file simultaneously. If theoretically
378 possible, real problems are unlikely. Doing any better would require a
379 convention, GNU-wide, for all programs doing backups. */
380
381 assign_string (&after_backup_name, 0);
382 after_backup_name = find_backup_file_name (file_name, backup_type);
383 if (! after_backup_name)
384 xalloc_die ();
385
386 if (rename (before_backup_name, after_backup_name) == 0)
387 {
388 if (verbose_option)
389 fprintf (stdlis, _("Renaming %s to %s\n"),
390 quote_n (0, before_backup_name),
391 quote_n (1, after_backup_name));
392 return true;
393 }
394 else
395 {
396 /* The backup operation failed. */
397 int e = errno;
398 ERROR ((0, e, _("%s: Cannot rename to %s"),
399 quotearg_colon (before_backup_name),
400 quote_n (1, after_backup_name)));
401 assign_string (&after_backup_name, 0);
402 return false;
403 }
404 }
405
406 /* Try to restore the recently backed up file to its original name.
407 This is usually only needed after a failed extraction. */
408 void
409 undo_last_backup (void)
410 {
411 if (after_backup_name)
412 {
413 if (rename (after_backup_name, before_backup_name) != 0)
414 {
415 int e = errno;
416 ERROR ((0, e, _("%s: Cannot rename to %s"),
417 quotearg_colon (after_backup_name),
418 quote_n (1, before_backup_name)));
419 }
420 if (verbose_option)
421 fprintf (stdlis, _("Renaming %s back to %s\n"),
422 quote_n (0, after_backup_name),
423 quote_n (1, before_backup_name));
424 assign_string (&after_backup_name, 0);
425 }
426 }
427
428 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
429 int
430 deref_stat (bool deref, char const *name, struct stat *buf)
431 {
432 return deref ? stat (name, buf) : lstat (name, buf);
433 }
434
435 /* A description of a working directory. */
436 struct wd
437 {
438 char const *name;
439 int saved;
440 struct saved_cwd saved_cwd;
441 };
442
443 /* A vector of chdir targets. wd[0] is the initial working directory. */
444 static struct wd *wd;
445
446 /* The number of working directories in the vector. */
447 static size_t wds;
448
449 /* The allocated size of the vector. */
450 static size_t wd_alloc;
451
452 /* DIR is the operand of a -C option; add it to vector of chdir targets,
453 and return the index of its location. */
454 int
455 chdir_arg (char const *dir)
456 {
457 if (wds == wd_alloc)
458 {
459 wd_alloc = 2 * (wd_alloc + 1);
460 wd = xrealloc (wd, sizeof *wd * wd_alloc);
461 if (! wds)
462 {
463 wd[wds].name = ".";
464 wd[wds].saved = 0;
465 wds++;
466 }
467 }
468
469 /* Optimize the common special case of the working directory,
470 or the working directory as a prefix. */
471 if (dir[0])
472 {
473 while (dir[0] == '.' && ISSLASH (dir[1]))
474 for (dir += 2; ISSLASH (*dir); dir++)
475 continue;
476 if (! dir[dir[0] == '.'])
477 return wds - 1;
478 }
479
480 wd[wds].name = dir;
481 wd[wds].saved = 0;
482 return wds++;
483 }
484
485 /* Change to directory I. If I is 0, change to the initial working
486 directory; otherwise, I must be a value returned by chdir_arg. */
487 void
488 chdir_do (int i)
489 {
490 static int previous;
491
492 if (previous != i)
493 {
494 struct wd *prev = &wd[previous];
495 struct wd *curr = &wd[i];
496
497 if (! prev->saved)
498 {
499 prev->saved = 1;
500 if (save_cwd (&prev->saved_cwd) != 0)
501 FATAL_ERROR ((0, 0, _("Cannot save working directory")));
502 }
503
504 if (curr->saved)
505 {
506 if (restore_cwd (&curr->saved_cwd))
507 FATAL_ERROR ((0, 0, _("Cannot change working directory")));
508 }
509 else
510 {
511 if (i && ! ISSLASH (curr->name[0]))
512 chdir_do (i - 1);
513 if (chdir (curr->name) != 0)
514 chdir_fatal (curr->name);
515 }
516
517 previous = i;
518 }
519 }
520 \f
521 /* Decode MODE from its binary form in a stat structure, and encode it
522 into a 9-byte string STRING, terminated with a NUL. */
523
524 void
525 decode_mode (mode_t mode, char *string)
526 {
527 *string++ = mode & S_IRUSR ? 'r' : '-';
528 *string++ = mode & S_IWUSR ? 'w' : '-';
529 *string++ = (mode & S_ISUID
530 ? (mode & S_IXUSR ? 's' : 'S')
531 : (mode & S_IXUSR ? 'x' : '-'));
532 *string++ = mode & S_IRGRP ? 'r' : '-';
533 *string++ = mode & S_IWGRP ? 'w' : '-';
534 *string++ = (mode & S_ISGID
535 ? (mode & S_IXGRP ? 's' : 'S')
536 : (mode & S_IXGRP ? 'x' : '-'));
537 *string++ = mode & S_IROTH ? 'r' : '-';
538 *string++ = mode & S_IWOTH ? 'w' : '-';
539 *string++ = (mode & S_ISVTX
540 ? (mode & S_IXOTH ? 't' : 'T')
541 : (mode & S_IXOTH ? 'x' : '-'));
542 *string = '\0';
543 }
544
545 /* Report an error associated with the system call CALL and the
546 optional name NAME. */
547 static void
548 call_arg_error (char const *call, char const *name)
549 {
550 int e = errno;
551 ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
552 }
553
554 /* Report a fatal error associated with the system call CALL and
555 the optional file name NAME. */
556 static void
557 call_arg_fatal (char const *call, char const *name)
558 {
559 int e = errno;
560 FATAL_ERROR ((0, e, _("%s: Cannot %s"), quotearg_colon (name), call));
561 }
562
563 /* Report a warning associated with the system call CALL and
564 the optional file name NAME. */
565 static void
566 call_arg_warn (char const *call, char const *name)
567 {
568 int e = errno;
569 WARN ((0, e, _("%s: Warning: Cannot %s"), quotearg_colon (name), call));
570 }
571
572 void
573 chdir_fatal (char const *name)
574 {
575 call_arg_fatal ("chdir", name);
576 }
577
578 void
579 chmod_error_details (char const *name, mode_t mode)
580 {
581 int e = errno;
582 char buf[10];
583 decode_mode (mode, buf);
584 ERROR ((0, e, _("%s: Cannot change mode to %s"),
585 quotearg_colon (name), buf));
586 }
587
588 void
589 chown_error_details (char const *name, uid_t uid, gid_t gid)
590 {
591 int e = errno;
592 ERROR ((0, e, _("%s: Cannot change ownership to uid %lu, gid %lu"),
593 quotearg_colon (name), (unsigned long) uid, (unsigned long) gid));
594 }
595
596 void
597 close_error (char const *name)
598 {
599 call_arg_error ("close", name);
600 }
601
602 void
603 close_warn (char const *name)
604 {
605 call_arg_warn ("close", name);
606 }
607
608 void
609 close_diag (char const *name)
610 {
611 if (ignore_failed_read_option)
612 close_warn (name);
613 else
614 close_error (name);
615 }
616
617 void
618 exec_fatal (char const *name)
619 {
620 call_arg_fatal ("exec", name);
621 }
622
623 void
624 link_error (char const *target, char const *source)
625 {
626 int e = errno;
627 ERROR ((0, e, _("%s: Cannot hard link to %s"),
628 quotearg_colon (source), quote_n (1, target)));
629 }
630
631 void
632 mkdir_error (char const *name)
633 {
634 call_arg_error ("mkdir", name);
635 }
636
637 void
638 mkfifo_error (char const *name)
639 {
640 call_arg_error ("mkfifo", name);
641 }
642
643 void
644 mknod_error (char const *name)
645 {
646 call_arg_error ("mknod", name);
647 }
648
649 void
650 open_error (char const *name)
651 {
652 call_arg_error ("open", name);
653 }
654
655 void
656 open_fatal (char const *name)
657 {
658 call_arg_fatal ("open", name);
659 }
660
661 void
662 open_warn (char const *name)
663 {
664 call_arg_warn ("open", name);
665 }
666
667 void
668 open_diag (char const *name)
669 {
670 if (ignore_failed_read_option)
671 open_warn (name);
672 else
673 open_error (name);
674 }
675
676 void
677 read_error (char const *name)
678 {
679 call_arg_error ("read", name);
680 }
681
682 void
683 read_error_details (char const *name, off_t offset, size_t size)
684 {
685 char buf[UINTMAX_STRSIZE_BOUND];
686 int e = errno;
687 ERROR ((0, e,
688 ngettext ("%s: Read error at byte %s, reading %lu byte",
689 "%s: Read error at byte %s, reading %lu bytes",
690 size),
691 quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
692 (unsigned long) size));
693 }
694
695 void
696 read_warn_details (char const *name, off_t offset, size_t size)
697 {
698 char buf[UINTMAX_STRSIZE_BOUND];
699 int e = errno;
700 WARN ((0, e,
701 ngettext ("%s: Warning: Read error at byte %s, reading %lu byte",
702 "%s: Warning: Read error at byte %s, reading %lu bytes",
703 size),
704 quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
705 (unsigned long) size));
706 }
707
708 void
709 read_diag_details (char const *name, off_t offset, size_t size)
710 {
711 if (ignore_failed_read_option)
712 read_warn_details (name, offset, size);
713 else
714 read_error_details (name, offset, size);
715 }
716
717 void
718 read_fatal (char const *name)
719 {
720 call_arg_fatal ("read", name);
721 }
722
723 void
724 read_fatal_details (char const *name, off_t offset, size_t size)
725 {
726 char buf[UINTMAX_STRSIZE_BOUND];
727 int e = errno;
728 FATAL_ERROR ((0, e,
729 ngettext ("%s: Read error at byte %s, reading %lu byte",
730 "%s: Read error at byte %s, reading %lu bytes",
731 size),
732 quotearg_colon (name), STRINGIFY_BIGINT (offset, buf),
733 (unsigned long) size));
734 }
735
736 void
737 readlink_error (char const *name)
738 {
739 call_arg_error ("readlink", name);
740 }
741
742 void
743 readlink_warn (char const *name)
744 {
745 call_arg_warn ("readlink", name);
746 }
747
748 void
749 readlink_diag (char const *name)
750 {
751 if (ignore_failed_read_option)
752 readlink_warn (name);
753 else
754 readlink_error (name);
755 }
756
757 void
758 savedir_error (char const *name)
759 {
760 call_arg_error ("savedir", name);
761 }
762
763 void
764 savedir_warn (char const *name)
765 {
766 call_arg_warn ("savedir", name);
767 }
768
769 void
770 savedir_diag (char const *name)
771 {
772 if (ignore_failed_read_option)
773 savedir_warn (name);
774 else
775 savedir_error (name);
776 }
777
778 void
779 seek_error (char const *name)
780 {
781 call_arg_error ("seek", name);
782 }
783
784 void
785 seek_error_details (char const *name, off_t offset)
786 {
787 char buf[UINTMAX_STRSIZE_BOUND];
788 int e = errno;
789 ERROR ((0, e, _("%s: Cannot seek to %s"),
790 quotearg_colon (name),
791 STRINGIFY_BIGINT (offset, buf)));
792 }
793
794 void
795 seek_warn (char const *name)
796 {
797 call_arg_warn ("seek", name);
798 }
799
800 void
801 seek_warn_details (char const *name, off_t offset)
802 {
803 char buf[UINTMAX_STRSIZE_BOUND];
804 int e = errno;
805 WARN ((0, e, _("%s: Warning: Cannot seek to %s"),
806 quotearg_colon (name),
807 STRINGIFY_BIGINT (offset, buf)));
808 }
809
810 void
811 seek_diag_details (char const *name, off_t offset)
812 {
813 if (ignore_failed_read_option)
814 seek_warn_details (name, offset);
815 else
816 seek_error_details (name, offset);
817 }
818
819 void
820 symlink_error (char const *contents, char const *name)
821 {
822 int e = errno;
823 ERROR ((0, e, _("%s: Cannot create symlink to %s"),
824 quotearg_colon (name), quote_n (1, contents)));
825 }
826
827 void
828 stat_error (char const *name)
829 {
830 call_arg_error ("stat", name);
831 }
832
833 void
834 stat_warn (char const *name)
835 {
836 call_arg_warn ("stat", name);
837 }
838
839 void
840 stat_diag (char const *name)
841 {
842 if (ignore_failed_read_option)
843 stat_warn (name);
844 else
845 stat_error (name);
846 }
847
848 void
849 truncate_error (char const *name)
850 {
851 call_arg_error ("truncate", name);
852 }
853
854 void
855 truncate_warn (char const *name)
856 {
857 call_arg_warn ("truncate", name);
858 }
859
860 void
861 unlink_error (char const *name)
862 {
863 call_arg_error ("unlink", name);
864 }
865
866 void
867 utime_error (char const *name)
868 {
869 call_arg_error ("utime", name);
870 }
871
872 void
873 waitpid_error (char const *name)
874 {
875 call_arg_error ("waitpid", name);
876 }
877
878 void
879 write_error (char const *name)
880 {
881 call_arg_error ("write", name);
882 }
883
884 void
885 write_error_details (char const *name, size_t status, size_t size)
886 {
887 if (status == 0)
888 write_error (name);
889 else
890 ERROR ((0, 0,
891 ngettext ("%s: Wrote only %lu of %lu byte",
892 "%s: Wrote only %lu of %lu bytes",
893 size),
894 name, (unsigned long int) status, (unsigned long int) size));
895 }
896
897 void
898 write_fatal (char const *name)
899 {
900 call_arg_fatal ("write", name);
901 }
902
903 void
904 write_fatal_details (char const *name, ssize_t status, size_t size)
905 {
906 write_error_details (name, status, size);
907 fatal_exit ();
908 }
909
910
911 /* Fork, aborting if unsuccessful. */
912 pid_t
913 xfork (void)
914 {
915 pid_t p = fork ();
916 if (p == (pid_t) -1)
917 call_arg_fatal ("fork", _("child process"));
918 return p;
919 }
920
921 /* Create a pipe, aborting if unsuccessful. */
922 void
923 xpipe (int fd[2])
924 {
925 if (pipe (fd) < 0)
926 call_arg_fatal ("pipe", _("interprocess channel"));
927 }
928
929 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
930 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
931 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
932 locations. */
933
934 static inline void *
935 ptr_align (void *ptr, size_t alignment)
936 {
937 char *p0 = ptr;
938 char *p1 = p0 + alignment - 1;
939 return p1 - (size_t) p1 % alignment;
940 }
941
942 /* Return the address of a page-aligned buffer of at least SIZE bytes.
943 The caller should free *PTR when done with the buffer. */
944
945 void *
946 page_aligned_alloc (void **ptr, size_t size)
947 {
948 size_t alignment = getpagesize ();
949 size_t size1 = size + alignment;
950 if (size1 < size)
951 xalloc_die ();
952 *ptr = xmalloc (size1);
953 return ptr_align (*ptr, alignment);
954 }
This page took 0.074358 seconds and 5 git commands to generate.