]> Dogcows Code - chaz/tar/blob - src/misc.c
tar: fix bug with -C and delayed setting of metadata
[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, 2005, 2006, 2007, 2009, 2010 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 3, 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 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20 #include <system.h>
21 #include <rmt.h>
22 #include "common.h"
23 #include <quotearg.h>
24 #include <save-cwd.h>
25 #include <xgetcwd.h>
26 #include <unlinkdir.h>
27 #include <utimens.h>
28
29 #if HAVE_STROPTS_H
30 # include <stropts.h>
31 #endif
32 #if HAVE_SYS_FILIO_H
33 # include <sys/filio.h>
34 #endif
35
36 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
37 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
38 #endif
39
40 \f
41 /* Handling strings. */
42
43 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
44 STRING was nonzero, it is freed first. */
45 void
46 assign_string (char **string, const char *value)
47 {
48 if (*string)
49 free (*string);
50 *string = value ? xstrdup (value) : 0;
51 }
52
53 #if 0
54 /* This function is currently unused; perhaps it should be removed? */
55
56 /* Allocate a copy of the string quoted as in C, and returns that. If
57 the string does not have to be quoted, it returns a null pointer.
58 The allocated copy should normally be freed with free() after the
59 caller is done with it.
60
61 This is used in one context only: generating the directory file in
62 incremental dumps. The quoted string is not intended for human
63 consumption; it is intended only for unquote_string. The quoting
64 is locale-independent, so that users needn't worry about locale
65 when reading directory files. This means that we can't use
66 quotearg, as quotearg is locale-dependent and is meant for human
67 consumption. */
68 static char *
69 quote_copy_string (const char *string)
70 {
71 const char *source = string;
72 char *destination = 0;
73 char *buffer = 0;
74 int copying = 0;
75
76 while (*source)
77 {
78 int character = *source++;
79
80 switch (character)
81 {
82 case '\n': case '\\':
83 if (!copying)
84 {
85 size_t length = (source - string) - 1;
86
87 copying = 1;
88 buffer = xmalloc (length + 2 + 2 * strlen (source) + 1);
89 memcpy (buffer, string, length);
90 destination = buffer + length;
91 }
92 *destination++ = '\\';
93 *destination++ = character == '\\' ? '\\' : 'n';
94 break;
95
96 default:
97 if (copying)
98 *destination++ = character;
99 break;
100 }
101 }
102 if (copying)
103 {
104 *destination = '\0';
105 return buffer;
106 }
107 return 0;
108 }
109 #endif
110
111 /* Takes a quoted C string (like those produced by quote_copy_string)
112 and turns it back into the un-quoted original. This is done in
113 place. Returns 0 only if the string was not properly quoted, but
114 completes the unquoting anyway.
115
116 This is used for reading the saved directory file in incremental
117 dumps. It is used for decoding old `N' records (demangling names).
118 But also, it is used for decoding file arguments, would they come
119 from the shell or a -T file, and for decoding the --exclude
120 argument. */
121 int
122 unquote_string (char *string)
123 {
124 int result = 1;
125 char *source = string;
126 char *destination = string;
127
128 /* Escape sequences other than \\ and \n are no longer generated by
129 quote_copy_string, but accept them for backwards compatibility,
130 and also because unquote_string is used for purposes other than
131 parsing the output of quote_copy_string. */
132
133 while (*source)
134 if (*source == '\\')
135 switch (*++source)
136 {
137 case '\\':
138 *destination++ = '\\';
139 source++;
140 break;
141
142 case 'a':
143 *destination++ = '\a';
144 source++;
145 break;
146
147 case 'b':
148 *destination++ = '\b';
149 source++;
150 break;
151
152 case 'f':
153 *destination++ = '\f';
154 source++;
155 break;
156
157 case 'n':
158 *destination++ = '\n';
159 source++;
160 break;
161
162 case 'r':
163 *destination++ = '\r';
164 source++;
165 break;
166
167 case 't':
168 *destination++ = '\t';
169 source++;
170 break;
171
172 case 'v':
173 *destination++ = '\v';
174 source++;
175 break;
176
177 case '?':
178 *destination++ = 0177;
179 source++;
180 break;
181
182 case '0':
183 case '1':
184 case '2':
185 case '3':
186 case '4':
187 case '5':
188 case '6':
189 case '7':
190 {
191 int value = *source++ - '0';
192
193 if (*source < '0' || *source > '7')
194 {
195 *destination++ = value;
196 break;
197 }
198 value = value * 8 + *source++ - '0';
199 if (*source < '0' || *source > '7')
200 {
201 *destination++ = value;
202 break;
203 }
204 value = value * 8 + *source++ - '0';
205 *destination++ = value;
206 break;
207 }
208
209 default:
210 result = 0;
211 *destination++ = '\\';
212 if (*source)
213 *destination++ = *source++;
214 break;
215 }
216 else if (source != destination)
217 *destination++ = *source++;
218 else
219 source++, destination++;
220
221 if (source != destination)
222 *destination = '\0';
223 return result;
224 }
225
226 /* Zap trailing slashes. */
227 char *
228 zap_slashes (char *name)
229 {
230 char *q;
231
232 if (!name || *name == 0)
233 return name;
234 q = name + strlen (name) - 1;
235 while (q > name && ISSLASH (*q))
236 *q-- = '\0';
237 return name;
238 }
239
240 /* Normalize FILE_NAME by removing redundant slashes and "."
241 components, including redundant trailing slashes. Leave ".."
242 alone, as it may be significant in the presence of symlinks and on
243 platforms where "/.." != "/". Destructive version: modifies its
244 argument. */
245 static void
246 normalize_filename_x (char *file_name)
247 {
248 char *name = file_name + FILE_SYSTEM_PREFIX_LEN (file_name);
249 char *p;
250 char const *q;
251 char c;
252
253 /* Don't squeeze leading "//" to "/", on hosts where they're distinct. */
254 name += (DOUBLE_SLASH_IS_DISTINCT_ROOT
255 && ISSLASH (*name) && ISSLASH (name[1]) && ! ISSLASH (name[2]));
256
257 /* Omit redundant leading "." components. */
258 for (q = p = name; (*p = *q) == '.' && ISSLASH (q[1]); p += !*q)
259 for (q += 2; ISSLASH (*q); q++)
260 continue;
261
262 /* Copy components from Q to P, omitting redundant slashes and
263 internal "." components. */
264 while ((*p++ = c = *q++) != '\0')
265 if (ISSLASH (c))
266 while (ISSLASH (q[*q == '.']))
267 q += (*q == '.') + 1;
268
269 /* Omit redundant trailing "." component and slash. */
270 if (2 < p - name)
271 {
272 p -= p[-2] == '.' && ISSLASH (p[-3]);
273 p -= 2 < p - name && ISSLASH (p[-2]);
274 p[-1] = '\0';
275 }
276 }
277
278 /* Normalize NAME by removing redundant slashes and "." components,
279 including redundant trailing slashes. Return a normalized
280 newly-allocated copy. */
281
282 char *
283 normalize_filename (const char *name)
284 {
285 char *copy = NULL;
286
287 if (IS_RELATIVE_FILE_NAME (name))
288 {
289 /* Set COPY to the absolute file name if possible.
290
291 FIXME: There should be no need to get the absolute file name.
292 getcwd is slow, it might fail, and it does not necessarily
293 return a canonical name even when it succeeds. Perhaps we
294 can use dev+ino pairs instead of names? */
295 copy = xgetcwd ();
296 if (copy)
297 {
298 size_t copylen = strlen (copy);
299 bool need_separator = ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
300 && copylen == 2 && ISSLASH (copy[1]));
301 copy = xrealloc (copy, copylen + need_separator + strlen (name) + 1);
302 copy[copylen] = DIRECTORY_SEPARATOR;
303 strcpy (copy + copylen + need_separator, name);
304 }
305 else
306 WARN ((0, errno, _("Cannot get working directory")));
307 }
308
309 if (! copy)
310 copy = xstrdup (name);
311 normalize_filename_x (copy);
312 return copy;
313 }
314
315 \f
316 void
317 replace_prefix (char **pname, const char *samp, size_t slen,
318 const char *repl, size_t rlen)
319 {
320 char *name = *pname;
321 size_t nlen = strlen (name);
322 if (nlen > slen && memcmp (name, samp, slen) == 0 && ISSLASH (name[slen]))
323 {
324 if (rlen > slen)
325 {
326 name = xrealloc (name, nlen - slen + rlen + 1);
327 *pname = name;
328 }
329 memmove (name + rlen, name + slen, nlen - slen + 1);
330 memcpy (name, repl, rlen);
331 }
332 }
333
334 \f
335 /* Handling numbers. */
336
337 /* Output fraction and trailing digits appropriate for a nanoseconds
338 count equal to NS, but don't output unnecessary '.' or trailing
339 zeros. */
340
341 void
342 code_ns_fraction (int ns, char *p)
343 {
344 if (ns == 0)
345 *p = '\0';
346 else
347 {
348 int i = 9;
349 *p++ = '.';
350
351 while (ns % 10 == 0)
352 {
353 ns /= 10;
354 i--;
355 }
356
357 p[i] = '\0';
358
359 for (;;)
360 {
361 p[--i] = '0' + ns % 10;
362 if (i == 0)
363 break;
364 ns /= 10;
365 }
366 }
367 }
368
369 char const *
370 code_timespec (struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
371 {
372 time_t s = t.tv_sec;
373 int ns = t.tv_nsec;
374 char *np;
375 bool negative = s < 0;
376
377 /* ignore invalid values of ns */
378 if (BILLION <= ns || ns < 0)
379 ns = 0;
380
381 if (negative && ns != 0)
382 {
383 s++;
384 ns = BILLION - ns;
385 }
386
387 np = umaxtostr (negative ? - (uintmax_t) s : (uintmax_t) s, sbuf + 1);
388 if (negative)
389 *--np = '-';
390 code_ns_fraction (ns, sbuf + UINTMAX_STRSIZE_BOUND);
391 return np;
392 }
393 \f
394 /* File handling. */
395
396 /* Saved names in case backup needs to be undone. */
397 static char *before_backup_name;
398 static char *after_backup_name;
399
400 /* Return 1 if FILE_NAME is obviously "." or "/". */
401 static bool
402 must_be_dot_or_slash (char const *file_name)
403 {
404 file_name += FILE_SYSTEM_PREFIX_LEN (file_name);
405
406 if (ISSLASH (file_name[0]))
407 {
408 for (;;)
409 if (ISSLASH (file_name[1]))
410 file_name++;
411 else if (file_name[1] == '.'
412 && ISSLASH (file_name[2 + (file_name[2] == '.')]))
413 file_name += 2 + (file_name[2] == '.');
414 else
415 return ! file_name[1];
416 }
417 else
418 {
419 while (file_name[0] == '.' && ISSLASH (file_name[1]))
420 {
421 file_name += 2;
422 while (ISSLASH (*file_name))
423 file_name++;
424 }
425
426 return ! file_name[0] || (file_name[0] == '.' && ! file_name[1]);
427 }
428 }
429
430 /* Some implementations of rmdir let you remove '.' or '/'.
431 Report an error with errno set to zero for obvious cases of this;
432 otherwise call rmdir. */
433 static int
434 safer_rmdir (const char *file_name)
435 {
436 if (must_be_dot_or_slash (file_name))
437 {
438 errno = 0;
439 return -1;
440 }
441
442 return rmdir (file_name);
443 }
444
445 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
446 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
447 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
448 a directory that cannot be removed (e.g., because it is nonempty)
449 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
450 Return 0 on error, with errno set; if FILE_NAME is obviously the working
451 directory return zero with errno set to zero. */
452 int
453 remove_any_file (const char *file_name, enum remove_option option)
454 {
455 /* Try unlink first if we cannot unlink directories, as this saves
456 us a system call in the common case where we're removing a
457 non-directory. */
458 bool try_unlink_first = cannot_unlink_dir ();
459
460 if (try_unlink_first)
461 {
462 if (unlink (file_name) == 0)
463 return 1;
464
465 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
466 directory without appropriate privileges, but many Linux
467 kernels return the more-sensible EISDIR. */
468 if (errno != EPERM && errno != EISDIR)
469 return 0;
470 }
471
472 if (safer_rmdir (file_name) == 0)
473 return 1;
474
475 switch (errno)
476 {
477 case ENOTDIR:
478 return !try_unlink_first && unlink (file_name) == 0;
479
480 case 0:
481 case EEXIST:
482 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
483 case ENOTEMPTY:
484 #endif
485 switch (option)
486 {
487 case ORDINARY_REMOVE_OPTION:
488 break;
489
490 case WANT_DIRECTORY_REMOVE_OPTION:
491 return -1;
492
493 case RECURSIVE_REMOVE_OPTION:
494 {
495 char *directory = savedir (file_name);
496 char const *entry;
497 size_t entrylen;
498
499 if (! directory)
500 return 0;
501
502 for (entry = directory;
503 (entrylen = strlen (entry)) != 0;
504 entry += entrylen + 1)
505 {
506 char *file_name_buffer = new_name (file_name, entry);
507 int r = remove_any_file (file_name_buffer,
508 RECURSIVE_REMOVE_OPTION);
509 int e = errno;
510 free (file_name_buffer);
511
512 if (! r)
513 {
514 free (directory);
515 errno = e;
516 return 0;
517 }
518 }
519
520 free (directory);
521 return safer_rmdir (file_name) == 0;
522 }
523 }
524 break;
525 }
526
527 return 0;
528 }
529
530 /* Check if FILE_NAME already exists and make a backup of it right now.
531 Return success (nonzero) only if the backup is either unneeded, or
532 successful. For now, directories are considered to never need
533 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
534 so, we do not have to backup block or character devices, nor remote
535 entities. */
536 bool
537 maybe_backup_file (const char *file_name, bool this_is_the_archive)
538 {
539 struct stat file_stat;
540
541 assign_string (&before_backup_name, file_name);
542
543 /* A run situation may exist between Emacs or other GNU programs trying to
544 make a backup for the same file simultaneously. If theoretically
545 possible, real problems are unlikely. Doing any better would require a
546 convention, GNU-wide, for all programs doing backups. */
547
548 assign_string (&after_backup_name, 0);
549
550 /* Check if we really need to backup the file. */
551
552 if (this_is_the_archive && _remdev (file_name))
553 return true;
554
555 if (stat (file_name, &file_stat))
556 {
557 if (errno == ENOENT)
558 return true;
559
560 stat_error (file_name);
561 return false;
562 }
563
564 if (S_ISDIR (file_stat.st_mode))
565 return true;
566
567 if (this_is_the_archive
568 && (S_ISBLK (file_stat.st_mode) || S_ISCHR (file_stat.st_mode)))
569 return true;
570
571 after_backup_name = find_backup_file_name (file_name, backup_type);
572 if (! after_backup_name)
573 xalloc_die ();
574
575 if (rename (before_backup_name, after_backup_name) == 0)
576 {
577 if (verbose_option)
578 fprintf (stdlis, _("Renaming %s to %s\n"),
579 quote_n (0, before_backup_name),
580 quote_n (1, after_backup_name));
581 return true;
582 }
583 else
584 {
585 /* The backup operation failed. */
586 int e = errno;
587 ERROR ((0, e, _("%s: Cannot rename to %s"),
588 quotearg_colon (before_backup_name),
589 quote_n (1, after_backup_name)));
590 assign_string (&after_backup_name, 0);
591 return false;
592 }
593 }
594
595 /* Try to restore the recently backed up file to its original name.
596 This is usually only needed after a failed extraction. */
597 void
598 undo_last_backup (void)
599 {
600 if (after_backup_name)
601 {
602 if (rename (after_backup_name, before_backup_name) != 0)
603 {
604 int e = errno;
605 ERROR ((0, e, _("%s: Cannot rename to %s"),
606 quotearg_colon (after_backup_name),
607 quote_n (1, before_backup_name)));
608 }
609 if (verbose_option)
610 fprintf (stdlis, _("Renaming %s back to %s\n"),
611 quote_n (0, after_backup_name),
612 quote_n (1, before_backup_name));
613 assign_string (&after_backup_name, 0);
614 }
615 }
616
617 /* Depending on DEREF, apply either stat or lstat to (NAME, BUF). */
618 int
619 deref_stat (bool deref, char const *name, struct stat *buf)
620 {
621 return deref ? stat (name, buf) : lstat (name, buf);
622 }
623
624 /* Set FD's (i.e., FILE's) access time to TIMESPEC[0]. If that's not
625 possible to do by itself, set its access and data modification
626 times to TIMESPEC[0] and TIMESPEC[1], respectively. */
627 int
628 set_file_atime (int fd, char const *file, struct timespec const timespec[2])
629 {
630 #ifdef _FIOSATIME
631 if (0 <= fd)
632 {
633 struct timeval timeval;
634 timeval.tv_sec = timespec[0].tv_sec;
635 timeval.tv_usec = timespec[0].tv_nsec / 1000;
636 if (ioctl (fd, _FIOSATIME, &timeval) == 0)
637 return 0;
638 }
639 #endif
640
641 return gl_futimens (fd, file, timespec);
642 }
643
644 /* A description of a working directory. */
645 struct wd
646 {
647 /* The directory's name. */
648 char const *name;
649
650 /* A negative value if no attempt has been made to save the
651 directory, 0 if it was saved successfully, and a positive errno
652 value if it was not saved successfully. */
653 int err;
654
655 /* The saved version of the directory, if ERR == 0. */
656 struct saved_cwd saved_cwd;
657 };
658
659 /* A vector of chdir targets. wd[0] is the initial working directory. */
660 static struct wd *wd;
661
662 /* The number of working directories in the vector. */
663 static size_t wd_count;
664
665 /* The allocated size of the vector. */
666 static size_t wd_alloc;
667
668 int
669 chdir_count ()
670 {
671 if (wd_count == 0)
672 return wd_count;
673 return wd_count - 1;
674 }
675
676 /* DIR is the operand of a -C option; add it to vector of chdir targets,
677 and return the index of its location. */
678 int
679 chdir_arg (char const *dir)
680 {
681 if (wd_count == wd_alloc)
682 {
683 if (wd_alloc == 0)
684 {
685 wd_alloc = 2;
686 wd = xmalloc (sizeof *wd * wd_alloc);
687 }
688 else
689 wd = x2nrealloc (wd, &wd_alloc, sizeof *wd);
690
691 if (! wd_count)
692 {
693 wd[wd_count].name = ".";
694 wd[wd_count].err = -1;
695 wd_count++;
696 }
697 }
698
699 /* Optimize the common special case of the working directory,
700 or the working directory as a prefix. */
701 if (dir[0])
702 {
703 while (dir[0] == '.' && ISSLASH (dir[1]))
704 for (dir += 2; ISSLASH (*dir); dir++)
705 continue;
706 if (! dir[dir[0] == '.'])
707 return wd_count - 1;
708 }
709
710 wd[wd_count].name = dir;
711 wd[wd_count].err = -1;
712 return wd_count++;
713 }
714
715 /* Index of current directory. */
716 int chdir_current;
717
718 /* Change to directory I. If I is 0, change to the initial working
719 directory; otherwise, I must be a value returned by chdir_arg. */
720 void
721 chdir_do (int i)
722 {
723 if (chdir_current != i)
724 {
725 struct wd *prev = &wd[chdir_current];
726 struct wd *curr = &wd[i];
727
728 if (prev->err < 0)
729 {
730 prev->err = 0;
731 if (save_cwd (&prev->saved_cwd) != 0)
732 prev->err = errno;
733 else if (0 <= prev->saved_cwd.desc)
734 {
735 /* Make sure we still have at least one descriptor available. */
736 int fd1 = prev->saved_cwd.desc;
737 int fd2 = dup (fd1);
738 if (0 <= fd2)
739 close (fd2);
740 else if (errno == EMFILE)
741 {
742 /* Force restore_cwd to use chdir_long. */
743 close (fd1);
744 prev->saved_cwd.desc = -1;
745 prev->saved_cwd.name = xgetcwd ();
746 if (! prev->saved_cwd.name)
747 prev->err = errno;
748 }
749 else
750 prev->err = errno;
751 }
752 }
753
754 if (0 <= curr->err)
755 {
756 int err = curr->err;
757 if (err == 0 && restore_cwd (&curr->saved_cwd) != 0)
758 err = errno;
759 if (err)
760 FATAL_ERROR ((0, err, _("Cannot restore working directory")));
761 }
762 else
763 {
764 if (i && ! ISSLASH (curr->name[0]))
765 chdir_do (i - 1);
766 if (chdir (curr->name) != 0)
767 chdir_fatal (curr->name);
768 }
769
770 chdir_current = i;
771 }
772 }
773 \f
774 void
775 close_diag (char const *name)
776 {
777 if (ignore_failed_read_option)
778 close_warn (name);
779 else
780 close_error (name);
781 }
782
783 void
784 open_diag (char const *name)
785 {
786 if (ignore_failed_read_option)
787 open_warn (name);
788 else
789 open_error (name);
790 }
791
792 void
793 read_diag_details (char const *name, off_t offset, size_t size)
794 {
795 if (ignore_failed_read_option)
796 read_warn_details (name, offset, size);
797 else
798 read_error_details (name, offset, size);
799 }
800
801 void
802 readlink_diag (char const *name)
803 {
804 if (ignore_failed_read_option)
805 readlink_warn (name);
806 else
807 readlink_error (name);
808 }
809
810 void
811 savedir_diag (char const *name)
812 {
813 if (ignore_failed_read_option)
814 savedir_warn (name);
815 else
816 savedir_error (name);
817 }
818
819 void
820 seek_diag_details (char const *name, off_t offset)
821 {
822 if (ignore_failed_read_option)
823 seek_warn_details (name, offset);
824 else
825 seek_error_details (name, offset);
826 }
827
828 void
829 stat_diag (char const *name)
830 {
831 if (ignore_failed_read_option)
832 stat_warn (name);
833 else
834 stat_error (name);
835 }
836
837 void
838 file_removed_diag (const char *name, bool top_level,
839 void (*diagfn) (char const *name))
840 {
841 if (!top_level && errno == ENOENT)
842 {
843 WARNOPT (WARN_FILE_REMOVED,
844 (0, 0, _("%s: File removed before we read it"),
845 quotearg_colon (name)));
846 set_exit_status (TAREXIT_DIFFERS);
847 }
848 else
849 diagfn (name);
850 }
851
852 void
853 dir_removed_diag (const char *name, bool top_level,
854 void (*diagfn) (char const *name))
855 {
856 if (!top_level && errno == ENOENT)
857 {
858 WARNOPT (WARN_FILE_REMOVED,
859 (0, 0, _("%s: Directory removed before we read it"),
860 quotearg_colon (name)));
861 set_exit_status (TAREXIT_DIFFERS);
862 }
863 else
864 diagfn (name);
865 }
866
867 void
868 write_fatal_details (char const *name, ssize_t status, size_t size)
869 {
870 write_error_details (name, status, size);
871 fatal_exit ();
872 }
873
874 /* Fork, aborting if unsuccessful. */
875 pid_t
876 xfork (void)
877 {
878 pid_t p = fork ();
879 if (p == (pid_t) -1)
880 call_arg_fatal ("fork", _("child process"));
881 return p;
882 }
883
884 /* Create a pipe, aborting if unsuccessful. */
885 void
886 xpipe (int fd[2])
887 {
888 if (pipe (fd) < 0)
889 call_arg_fatal ("pipe", _("interprocess channel"));
890 }
891
892 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
893 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
894 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
895 locations. */
896
897 static inline void *
898 ptr_align (void *ptr, size_t alignment)
899 {
900 char *p0 = ptr;
901 char *p1 = p0 + alignment - 1;
902 return p1 - (size_t) p1 % alignment;
903 }
904
905 /* Return the address of a page-aligned buffer of at least SIZE bytes.
906 The caller should free *PTR when done with the buffer. */
907
908 void *
909 page_aligned_alloc (void **ptr, size_t size)
910 {
911 size_t alignment = getpagesize ();
912 size_t size1 = size + alignment;
913 if (size1 < size)
914 xalloc_die ();
915 *ptr = xmalloc (size1);
916 return ptr_align (*ptr, alignment);
917 }
918
919 \f
920
921 struct namebuf
922 {
923 char *buffer; /* directory, `/', and directory member */
924 size_t buffer_size; /* allocated size of name_buffer */
925 size_t dir_length; /* length of directory part in buffer */
926 };
927
928 namebuf_t
929 namebuf_create (const char *dir)
930 {
931 namebuf_t buf = xmalloc (sizeof (*buf));
932 buf->buffer_size = strlen (dir) + 2;
933 buf->buffer = xmalloc (buf->buffer_size);
934 strcpy (buf->buffer, dir);
935 buf->dir_length = strlen (buf->buffer);
936 if (!ISSLASH (buf->buffer[buf->dir_length - 1]))
937 buf->buffer[buf->dir_length++] = DIRECTORY_SEPARATOR;
938 return buf;
939 }
940
941 void
942 namebuf_free (namebuf_t buf)
943 {
944 free (buf->buffer);
945 free (buf);
946 }
947
948 char *
949 namebuf_name (namebuf_t buf, const char *name)
950 {
951 size_t len = strlen (name);
952 while (buf->dir_length + len + 1 >= buf->buffer_size)
953 buf->buffer = x2realloc (buf->buffer, &buf->buffer_size);
954 strcpy (buf->buffer + buf->dir_length, name);
955 return buf->buffer;
956 }
This page took 0.074133 seconds and 5 git commands to generate.