1 /* Miscellaneous functions, not really specific to GNU tar.
3 Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2012 Free Software Foundation, Inc.
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
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.
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. */
20 #define COMMON_INLINE _GL_EXTERN_INLINE
26 #include <unlinkdir.h>
29 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
30 # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
34 /* Handling strings. */
36 /* Assign STRING to a copy of VALUE if not zero, or to zero. If
37 STRING was nonzero, it is freed first. */
39 assign_string (char **string
, const char *value
)
42 *string
= value
? xstrdup (value
) : 0;
46 /* This function is currently unused; perhaps it should be removed? */
48 /* Allocate a copy of the string quoted as in C, and returns that. If
49 the string does not have to be quoted, it returns a null pointer.
50 The allocated copy should normally be freed with free() after the
51 caller is done with it.
53 This is used in one context only: generating the directory file in
54 incremental dumps. The quoted string is not intended for human
55 consumption; it is intended only for unquote_string. The quoting
56 is locale-independent, so that users needn't worry about locale
57 when reading directory files. This means that we can't use
58 quotearg, as quotearg is locale-dependent and is meant for human
61 quote_copy_string (const char *string
)
63 const char *source
= string
;
64 char *destination
= 0;
70 int character
= *source
++;
77 size_t length
= (source
- string
) - 1;
80 buffer
= xmalloc (length
+ 2 + 2 * strlen (source
) + 1);
81 memcpy (buffer
, string
, length
);
82 destination
= buffer
+ length
;
84 *destination
++ = '\\';
85 *destination
++ = character
== '\\' ? '\\' : 'n';
90 *destination
++ = character
;
103 /* Takes a quoted C string (like those produced by quote_copy_string)
104 and turns it back into the un-quoted original. This is done in
105 place. Returns 0 only if the string was not properly quoted, but
106 completes the unquoting anyway.
108 This is used for reading the saved directory file in incremental
109 dumps. It is used for decoding old 'N' records (demangling names).
110 But also, it is used for decoding file arguments, would they come
111 from the shell or a -T file, and for decoding the --exclude
114 unquote_string (char *string
)
117 char *source
= string
;
118 char *destination
= string
;
120 /* Escape sequences other than \\ and \n are no longer generated by
121 quote_copy_string, but accept them for backwards compatibility,
122 and also because unquote_string is used for purposes other than
123 parsing the output of quote_copy_string. */
130 *destination
++ = '\\';
135 *destination
++ = '\a';
140 *destination
++ = '\b';
145 *destination
++ = '\f';
150 *destination
++ = '\n';
155 *destination
++ = '\r';
160 *destination
++ = '\t';
165 *destination
++ = '\v';
170 *destination
++ = 0177;
183 int value
= *source
++ - '0';
185 if (*source
< '0' || *source
> '7')
187 *destination
++ = value
;
190 value
= value
* 8 + *source
++ - '0';
191 if (*source
< '0' || *source
> '7')
193 *destination
++ = value
;
196 value
= value
* 8 + *source
++ - '0';
197 *destination
++ = value
;
203 *destination
++ = '\\';
205 *destination
++ = *source
++;
208 else if (source
!= destination
)
209 *destination
++ = *source
++;
211 source
++, destination
++;
213 if (source
!= destination
)
218 /* Zap trailing slashes. */
220 zap_slashes (char *name
)
224 if (!name
|| *name
== 0)
226 q
= name
+ strlen (name
) - 1;
227 while (q
> name
&& ISSLASH (*q
))
232 /* Normalize FILE_NAME by removing redundant slashes and "."
233 components, including redundant trailing slashes. Leave ".."
234 alone, as it may be significant in the presence of symlinks and on
235 platforms where "/.." != "/". Destructive version: modifies its
238 normalize_filename_x (char *file_name
)
240 char *name
= file_name
+ FILE_SYSTEM_PREFIX_LEN (file_name
);
245 /* Don't squeeze leading "//" to "/", on hosts where they're distinct. */
246 name
+= (DOUBLE_SLASH_IS_DISTINCT_ROOT
247 && ISSLASH (*name
) && ISSLASH (name
[1]) && ! ISSLASH (name
[2]));
249 /* Omit redundant leading "." components. */
250 for (q
= p
= name
; (*p
= *q
) == '.' && ISSLASH (q
[1]); p
+= !*q
)
251 for (q
+= 2; ISSLASH (*q
); q
++)
254 /* Copy components from Q to P, omitting redundant slashes and
255 internal "." components. */
256 while ((*p
++ = c
= *q
++) != '\0')
258 while (ISSLASH (q
[*q
== '.']))
259 q
+= (*q
== '.') + 1;
261 /* Omit redundant trailing "." component and slash. */
264 p
-= p
[-2] == '.' && ISSLASH (p
[-3]);
265 p
-= 2 < p
- name
&& ISSLASH (p
[-2]);
270 /* Normalize NAME by removing redundant slashes and "." components,
271 including redundant trailing slashes. Return a normalized
272 newly-allocated copy. */
275 normalize_filename (const char *name
)
279 if (IS_RELATIVE_FILE_NAME (name
))
281 /* Set COPY to the absolute file name if possible.
283 FIXME: There should be no need to get the absolute file name.
284 getcwd is slow, it might fail, and it does not necessarily
285 return a canonical name even when it succeeds. Perhaps we
286 can use dev+ino pairs instead of names? */
290 size_t copylen
= strlen (copy
);
291 bool need_separator
= ! (DOUBLE_SLASH_IS_DISTINCT_ROOT
292 && copylen
== 2 && ISSLASH (copy
[1]));
293 copy
= xrealloc (copy
, copylen
+ need_separator
+ strlen (name
) + 1);
294 copy
[copylen
] = DIRECTORY_SEPARATOR
;
295 strcpy (copy
+ copylen
+ need_separator
, name
);
298 WARN ((0, errno
, _("Cannot get working directory")));
302 copy
= xstrdup (name
);
303 normalize_filename_x (copy
);
309 replace_prefix (char **pname
, const char *samp
, size_t slen
,
310 const char *repl
, size_t rlen
)
313 size_t nlen
= strlen (name
);
314 if (nlen
> slen
&& memcmp (name
, samp
, slen
) == 0 && ISSLASH (name
[slen
]))
318 name
= xrealloc (name
, nlen
- slen
+ rlen
+ 1);
321 memmove (name
+ rlen
, name
+ slen
, nlen
- slen
+ 1);
322 memcpy (name
, repl
, rlen
);
327 /* Handling numbers. */
329 /* Convert VALUE, which is converted from a system integer type whose
330 minimum value is MINVAL and maximum MINVAL, to an decimal
331 integer string. Use the storage in BUF and return a pointer to the
332 converted string. If VALUE is converted from a negative integer in
333 the range MINVAL .. -1, represent it with a string representation
334 of the negative integer, using leading '-'. */
335 #if ! (INTMAX_MAX <= UINTMAX_MAX / 2)
336 # error "strtosysint accepts uintmax_t to represent intmax_t"
339 sysinttostr (uintmax_t value
, intmax_t minval
, uintmax_t maxval
,
340 char buf
[SYSINT_BUFSIZE
])
343 return umaxtostr (value
, buf
);
346 intmax_t i
= value
- minval
;
347 return imaxtostr (i
+ minval
, buf
);
351 /* Convert a prefix of the string ARG to a system integer type whose
352 minimum value is MINVAL and maximum MAXVAL. If MINVAL is negative,
353 negative integers MINVAL .. -1 are assumed to be represented using
354 leading '-' in the usual way. If the represented value exceeds
355 INTMAX_MAX, return a negative integer V such that (uintmax_t) V
356 yields the represented value. If ARGLIM is nonnull, store into
357 *ARGLIM a pointer to the first character after the prefix.
359 This is the inverse of sysinttostr.
361 On a normal return, set errno = 0.
362 On conversion error, return 0 and set errno = EINVAL.
363 On overflow, return an extreme value and set errno = ERANGE. */
364 #if ! (INTMAX_MAX <= UINTMAX_MAX)
365 # error "strtosysint accepts uintmax_t to represent nonnegative intmax_t"
368 strtosysint (char const *arg
, char **arglim
, intmax_t minval
, uintmax_t maxval
)
371 if (maxval
<= INTMAX_MAX
)
373 if (ISDIGIT (arg
[*arg
== '-']))
375 intmax_t i
= strtoimax (arg
, arglim
, 10);
376 intmax_t imaxval
= maxval
;
377 if (minval
<= i
&& i
<= imaxval
)
380 return i
< minval
? minval
: maxval
;
387 uintmax_t i
= strtoumax (arg
, arglim
, 10);
389 return represent_uintmax (i
);
399 /* Output fraction and trailing digits appropriate for a nanoseconds
400 count equal to NS, but don't output unnecessary '.' or trailing
404 code_ns_fraction (int ns
, char *p
)
423 p
[--i
] = '0' + ns
% 10;
432 code_timespec (struct timespec t
, char sbuf
[TIMESPEC_STRSIZE_BOUND
])
437 bool negative
= s
< 0;
439 /* ignore invalid values of ns */
440 if (BILLION
<= ns
|| ns
< 0)
443 if (negative
&& ns
!= 0)
449 np
= umaxtostr (negative
? - (uintmax_t) s
: (uintmax_t) s
, sbuf
+ 1);
452 code_ns_fraction (ns
, sbuf
+ UINTMAX_STRSIZE_BOUND
);
457 decode_timespec (char const *arg
, char **arg_lim
, bool parse_fraction
)
459 time_t s
= TYPE_MINIMUM (time_t);
462 bool negative
= *arg
== '-';
465 if (! ISDIGIT (arg
[negative
]))
473 intmax_t i
= strtoimax (arg
, arg_lim
, 10);
474 if (TYPE_SIGNED (time_t) ? TYPE_MINIMUM (time_t) <= i
: 0 <= i
)
481 uintmax_t i
= strtoumax (arg
, arg_lim
, 10);
482 if (i
<= TYPE_MAXIMUM (time_t))
491 if (parse_fraction
&& *p
== '.')
494 bool trailing_nonzero
= false;
496 while (ISDIGIT (*++p
))
497 if (digits
< LOG10_BILLION
)
498 digits
++, ns
= 10 * ns
+ (*p
- '0');
500 trailing_nonzero
|= *p
!= '0';
502 while (digits
< LOG10_BILLION
)
507 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
508 I.e., truncate time stamps towards minus infinity while
509 converting them to internal form. */
510 ns
+= trailing_nonzero
;
513 if (s
== TYPE_MINIMUM (time_t))
528 *arg_lim
= (char *) p
;
536 /* Saved names in case backup needs to be undone. */
537 static char *before_backup_name
;
538 static char *after_backup_name
;
540 /* Return 1 if FILE_NAME is obviously "." or "/". */
542 must_be_dot_or_slash (char const *file_name
)
544 file_name
+= FILE_SYSTEM_PREFIX_LEN (file_name
);
546 if (ISSLASH (file_name
[0]))
549 if (ISSLASH (file_name
[1]))
551 else if (file_name
[1] == '.'
552 && ISSLASH (file_name
[2 + (file_name
[2] == '.')]))
553 file_name
+= 2 + (file_name
[2] == '.');
555 return ! file_name
[1];
559 while (file_name
[0] == '.' && ISSLASH (file_name
[1]))
562 while (ISSLASH (*file_name
))
566 return ! file_name
[0] || (file_name
[0] == '.' && ! file_name
[1]);
570 /* Some implementations of rmdir let you remove '.' or '/'.
571 Report an error with errno set to zero for obvious cases of this;
572 otherwise call rmdir. */
574 safer_rmdir (const char *file_name
)
576 if (must_be_dot_or_slash (file_name
))
582 return unlinkat (chdir_fd
, file_name
, AT_REMOVEDIR
);
585 /* Remove FILE_NAME, returning 1 on success. If FILE_NAME is a directory,
586 then if OPTION is RECURSIVE_REMOVE_OPTION is set remove FILE_NAME
587 recursively; otherwise, remove it only if it is empty. If FILE_NAME is
588 a directory that cannot be removed (e.g., because it is nonempty)
589 and if OPTION is WANT_DIRECTORY_REMOVE_OPTION, then return -1.
590 Return 0 on error, with errno set; if FILE_NAME is obviously the working
591 directory return zero with errno set to zero. */
593 remove_any_file (const char *file_name
, enum remove_option option
)
595 /* Try unlink first if we cannot unlink directories, as this saves
596 us a system call in the common case where we're removing a
598 bool try_unlink_first
= cannot_unlink_dir ();
600 if (try_unlink_first
)
602 if (unlinkat (chdir_fd
, file_name
, 0) == 0)
605 /* POSIX 1003.1-2001 requires EPERM when attempting to unlink a
606 directory without appropriate privileges, but many Linux
607 kernels return the more-sensible EISDIR. */
608 if (errno
!= EPERM
&& errno
!= EISDIR
)
612 if (safer_rmdir (file_name
) == 0)
618 return !try_unlink_first
&& unlinkat (chdir_fd
, file_name
, 0) == 0;
622 #if defined ENOTEMPTY && ENOTEMPTY != EEXIST
627 case ORDINARY_REMOVE_OPTION
:
630 case WANT_DIRECTORY_REMOVE_OPTION
:
633 case RECURSIVE_REMOVE_OPTION
:
635 char *directory
= savedir (file_name
);
642 for (entry
= directory
;
643 (entrylen
= strlen (entry
)) != 0;
644 entry
+= entrylen
+ 1)
646 char *file_name_buffer
= new_name (file_name
, entry
);
647 int r
= remove_any_file (file_name_buffer
,
648 RECURSIVE_REMOVE_OPTION
);
650 free (file_name_buffer
);
661 return safer_rmdir (file_name
) == 0;
670 /* Check if FILE_NAME already exists and make a backup of it right now.
671 Return success (nonzero) only if the backup is either unneeded, or
672 successful. For now, directories are considered to never need
673 backup. If THIS_IS_THE_ARCHIVE is nonzero, this is the archive and
674 so, we do not have to backup block or character devices, nor remote
677 maybe_backup_file (const char *file_name
, bool this_is_the_archive
)
679 struct stat file_stat
;
681 assign_string (&before_backup_name
, file_name
);
683 /* A run situation may exist between Emacs or other GNU programs trying to
684 make a backup for the same file simultaneously. If theoretically
685 possible, real problems are unlikely. Doing any better would require a
686 convention, GNU-wide, for all programs doing backups. */
688 assign_string (&after_backup_name
, 0);
690 /* Check if we really need to backup the file. */
692 if (this_is_the_archive
&& _remdev (file_name
))
695 if (deref_stat (file_name
, &file_stat
) != 0)
700 stat_error (file_name
);
704 if (S_ISDIR (file_stat
.st_mode
))
707 if (this_is_the_archive
708 && (S_ISBLK (file_stat
.st_mode
) || S_ISCHR (file_stat
.st_mode
)))
711 after_backup_name
= find_backup_file_name (file_name
, backup_type
);
712 if (! after_backup_name
)
715 if (renameat (chdir_fd
, before_backup_name
, chdir_fd
, after_backup_name
)
719 fprintf (stdlis
, _("Renaming %s to %s\n"),
720 quote_n (0, before_backup_name
),
721 quote_n (1, after_backup_name
));
726 /* The backup operation failed. */
728 ERROR ((0, e
, _("%s: Cannot rename to %s"),
729 quotearg_colon (before_backup_name
),
730 quote_n (1, after_backup_name
)));
731 assign_string (&after_backup_name
, 0);
736 /* Try to restore the recently backed up file to its original name.
737 This is usually only needed after a failed extraction. */
739 undo_last_backup (void)
741 if (after_backup_name
)
743 if (renameat (chdir_fd
, after_backup_name
, chdir_fd
, before_backup_name
)
747 ERROR ((0, e
, _("%s: Cannot rename to %s"),
748 quotearg_colon (after_backup_name
),
749 quote_n (1, before_backup_name
)));
752 fprintf (stdlis
, _("Renaming %s back to %s\n"),
753 quote_n (0, after_backup_name
),
754 quote_n (1, before_backup_name
));
755 assign_string (&after_backup_name
, 0);
759 /* Apply either stat or lstat to (NAME, BUF), depending on the
760 presence of the --dereference option. NAME is relative to the
761 most-recent argument to chdir_do. */
763 deref_stat (char const *name
, struct stat
*buf
)
765 return fstatat (chdir_fd
, name
, buf
, fstatat_flags
);
768 /* Read from FD into the buffer BUF with COUNT bytes. Attempt to fill
769 BUF. Wait until input is available; this matters because files are
770 opened O_NONBLOCK for security reasons, and on some file systems
771 this can cause read to fail with errno == EAGAIN. Return the
772 actual number of bytes read, zero for EOF, or
773 SAFE_READ_ERROR upon error. */
775 blocking_read (int fd
, void *buf
, size_t count
)
777 size_t bytes
= safe_read (fd
, buf
, count
);
779 #if defined F_SETFL && O_NONBLOCK
780 if (bytes
== SAFE_READ_ERROR
&& errno
== EAGAIN
)
782 int flags
= fcntl (fd
, F_GETFL
);
783 if (0 <= flags
&& flags
& O_NONBLOCK
784 && fcntl (fd
, F_SETFL
, flags
& ~O_NONBLOCK
) != -1)
785 bytes
= safe_read (fd
, buf
, count
);
792 /* Write to FD from the buffer BUF with COUNT bytes. Do a full write.
793 Wait until an output buffer is available; this matters because
794 files are opened O_NONBLOCK for security reasons, and on some file
795 systems this can cause write to fail with errno == EAGAIN. Return
796 the actual number of bytes written, setting errno if that is less
799 blocking_write (int fd
, void const *buf
, size_t count
)
801 size_t bytes
= full_write (fd
, buf
, count
);
803 #if defined F_SETFL && O_NONBLOCK
804 if (bytes
< count
&& errno
== EAGAIN
)
806 int flags
= fcntl (fd
, F_GETFL
);
807 if (0 <= flags
&& flags
& O_NONBLOCK
808 && fcntl (fd
, F_SETFL
, flags
& ~O_NONBLOCK
) != -1)
810 char const *buffer
= buf
;
811 bytes
+= full_write (fd
, buffer
+ bytes
, count
- bytes
);
819 /* Set FD's (i.e., assuming the working directory is PARENTFD, FILE's)
820 access time to ATIME. */
822 set_file_atime (int fd
, int parentfd
, char const *file
, struct timespec atime
)
824 struct timespec ts
[2];
826 ts
[1].tv_nsec
= UTIME_OMIT
;
827 return fdutimensat (fd
, parentfd
, file
, ts
, fstatat_flags
);
830 /* A description of a working directory. */
833 /* The directory's name. */
836 /* If nonzero, the file descriptor of the directory, or AT_FDCWD if
837 the working directory. If zero, the directory needs to be opened
842 /* A vector of chdir targets. wd[0] is the initial working directory. */
843 static struct wd
*wd
;
845 /* The number of working directories in the vector. */
846 static size_t wd_count
;
848 /* The allocated size of the vector. */
849 static size_t wd_alloc
;
851 /* The maximum number of chdir targets with open directories.
852 Don't make it too large, as many operating systems have a small
853 limit on the number of open file descriptors. Also, the current
854 implementation does not scale well. */
855 enum { CHDIR_CACHE_SIZE
= 16 };
857 /* Indexes into WD of chdir targets with open file descriptors, sorted
858 most-recently used first. Zero indexes are unused. */
859 static int wdcache
[CHDIR_CACHE_SIZE
];
861 /* Number of nonzero entries in WDCACHE. */
862 static size_t wdcache_count
;
872 /* DIR is the operand of a -C option; add it to vector of chdir targets,
873 and return the index of its location. */
875 chdir_arg (char const *dir
)
877 if (wd_count
== wd_alloc
)
882 wd
= xmalloc (sizeof *wd
* wd_alloc
);
885 wd
= x2nrealloc (wd
, &wd_alloc
, sizeof *wd
);
889 wd
[wd_count
].name
= ".";
890 wd
[wd_count
].fd
= AT_FDCWD
;
895 /* Optimize the common special case of the working directory,
896 or the working directory as a prefix. */
899 while (dir
[0] == '.' && ISSLASH (dir
[1]))
900 for (dir
+= 2; ISSLASH (*dir
); dir
++)
902 if (! dir
[dir
[0] == '.'])
906 wd
[wd_count
].name
= dir
;
911 /* Index of current directory. */
914 /* Value suitable for use as the first argument to openat, and in
915 similar locations for fstatat, etc. This is an open file
916 descriptor, or AT_FDCWD if the working directory is current. It is
917 valid until the next invocation of chdir_do. */
918 int chdir_fd
= AT_FDCWD
;
920 /* Change to directory I, in a virtual way. This does not actually
921 invoke chdir; it merely sets chdir_fd to an int suitable as the
922 first argument for openat, etc. If I is 0, change to the initial
923 working directory; otherwise, I must be a value returned by
928 if (chdir_current
!= i
)
930 struct wd
*curr
= &wd
[i
];
935 if (! IS_ABSOLUTE_FILE_NAME (curr
->name
))
937 fd
= openat (chdir_fd
, curr
->name
,
938 open_searchdir_flags
& ~ O_NOFOLLOW
);
940 open_fatal (curr
->name
);
944 /* Add I to the cache, tossing out the lowest-ranking entry if the
946 if (wdcache_count
< CHDIR_CACHE_SIZE
)
947 wdcache
[wdcache_count
++] = i
;
950 struct wd
*stale
= &wd
[wdcache
[CHDIR_CACHE_SIZE
- 1]];
951 if (close (stale
->fd
) != 0)
952 close_diag (stale
->name
);
954 wdcache
[CHDIR_CACHE_SIZE
- 1] = i
;
960 /* Move the i value to the front of the cache. This is
961 O(CHDIR_CACHE_SIZE), but the cache is small. */
963 int prev
= wdcache
[0];
964 for (ci
= 1; prev
!= i
; ci
++)
966 int cur
= wdcache
[ci
];
981 close_diag (char const *name
)
983 if (ignore_failed_read_option
)
990 open_diag (char const *name
)
992 if (ignore_failed_read_option
)
999 read_diag_details (char const *name
, off_t offset
, size_t size
)
1001 if (ignore_failed_read_option
)
1002 read_warn_details (name
, offset
, size
);
1004 read_error_details (name
, offset
, size
);
1008 readlink_diag (char const *name
)
1010 if (ignore_failed_read_option
)
1011 readlink_warn (name
);
1013 readlink_error (name
);
1017 savedir_diag (char const *name
)
1019 if (ignore_failed_read_option
)
1020 savedir_warn (name
);
1022 savedir_error (name
);
1026 seek_diag_details (char const *name
, off_t offset
)
1028 if (ignore_failed_read_option
)
1029 seek_warn_details (name
, offset
);
1031 seek_error_details (name
, offset
);
1035 stat_diag (char const *name
)
1037 if (ignore_failed_read_option
)
1044 file_removed_diag (const char *name
, bool top_level
,
1045 void (*diagfn
) (char const *name
))
1047 if (!top_level
&& errno
== ENOENT
)
1049 WARNOPT (WARN_FILE_REMOVED
,
1050 (0, 0, _("%s: File removed before we read it"),
1051 quotearg_colon (name
)));
1052 set_exit_status (TAREXIT_DIFFERS
);
1059 write_fatal_details (char const *name
, ssize_t status
, size_t size
)
1061 write_error_details (name
, status
, size
);
1065 /* Fork, aborting if unsuccessful. */
1070 if (p
== (pid_t
) -1)
1071 call_arg_fatal ("fork", _("child process"));
1075 /* Create a pipe, aborting if unsuccessful. */
1080 call_arg_fatal ("pipe", _("interprocess channel"));
1083 /* Return PTR, aligned upward to the next multiple of ALIGNMENT.
1084 ALIGNMENT must be nonzero. The caller must arrange for ((char *)
1085 PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
1088 static inline void *
1089 ptr_align (void *ptr
, size_t alignment
)
1092 char *p1
= p0
+ alignment
- 1;
1093 return p1
- (size_t) p1
% alignment
;
1096 /* Return the address of a page-aligned buffer of at least SIZE bytes.
1097 The caller should free *PTR when done with the buffer. */
1100 page_aligned_alloc (void **ptr
, size_t size
)
1102 size_t alignment
= getpagesize ();
1103 size_t size1
= size
+ alignment
;
1106 *ptr
= xmalloc (size1
);
1107 return ptr_align (*ptr
, alignment
);
1114 char *buffer
; /* directory, '/', and directory member */
1115 size_t buffer_size
; /* allocated size of name_buffer */
1116 size_t dir_length
; /* length of directory part in buffer */
1120 namebuf_create (const char *dir
)
1122 namebuf_t buf
= xmalloc (sizeof (*buf
));
1123 buf
->buffer_size
= strlen (dir
) + 2;
1124 buf
->buffer
= xmalloc (buf
->buffer_size
);
1125 strcpy (buf
->buffer
, dir
);
1126 buf
->dir_length
= strlen (buf
->buffer
);
1127 if (!ISSLASH (buf
->buffer
[buf
->dir_length
- 1]))
1128 buf
->buffer
[buf
->dir_length
++] = DIRECTORY_SEPARATOR
;
1133 namebuf_free (namebuf_t buf
)
1140 namebuf_name (namebuf_t buf
, const char *name
)
1142 size_t len
= strlen (name
);
1143 while (buf
->dir_length
+ len
+ 1 >= buf
->buffer_size
)
1144 buf
->buffer
= x2realloc (buf
->buffer
, &buf
->buffer_size
);
1145 strcpy (buf
->buffer
+ buf
->dir_length
, name
);