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