1 /*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 28 #include "cpio_platform.h" 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/types.h> 32 #include <archive.h> 33 #include <archive_entry.h> 34 35 #ifdef HAVE_SYS_MKDEV_H 36 #include <sys/mkdev.h> 37 #endif 38 #ifdef HAVE_SYS_STAT_H 39 #include <sys/stat.h> 40 #endif 41 #ifdef HAVE_SYS_TIME_H 42 #include <sys/time.h> 43 #endif 44 #ifdef HAVE_ERRNO_H 45 #include <errno.h> 46 #endif 47 #ifdef HAVE_FCNTL_H 48 #include <fcntl.h> 49 #endif 50 #ifdef HAVE_GRP_H 51 #include <grp.h> 52 #endif 53 #ifdef HAVE_LOCALE_H 54 #include <locale.h> 55 #endif 56 #ifdef HAVE_PWD_H 57 #include <pwd.h> 58 #endif 59 #ifdef HAVE_SIGNAL_H 60 #include <signal.h> 61 #endif 62 #ifdef HAVE_STDARG_H 63 #include <stdarg.h> 64 #endif 65 #ifdef HAVE_STDINT_H 66 #include <stdint.h> 67 #endif 68 #include <stdio.h> 69 #ifdef HAVE_STDLIB_H 70 #include <stdlib.h> 71 #endif 72 #ifdef HAVE_STRING_H 73 #include <string.h> 74 #endif 75 #ifdef HAVE_UNISTD_H 76 #include <unistd.h> 77 #endif 78 #ifdef HAVE_TIME_H 79 #include <time.h> 80 #endif 81 82 #include "cpio.h" 83 #include "err.h" 84 #include "line_reader.h" 85 #include "matching.h" 86 87 /* Fixed size of uname/gname caches. */ 88 #define name_cache_size 101 89 90 #ifndef O_BINARY 91 #define O_BINARY 0 92 #endif 93 94 struct name_cache { 95 int probes; 96 int hits; 97 size_t size; 98 struct { 99 id_t id; 100 char *name; 101 } cache[name_cache_size]; 102 }; 103 104 static int extract_data(struct archive *, struct archive *); 105 const char * cpio_i64toa(int64_t); 106 static const char *cpio_rename(const char *name); 107 static int entry_to_archive(struct cpio *, struct archive_entry *); 108 static int file_to_archive(struct cpio *, const char *); 109 static void free_cache(struct name_cache *cache); 110 static void list_item_verbose(struct cpio *, struct archive_entry *); 111 static void long_help(void); 112 static const char *lookup_gname(struct cpio *, gid_t gid); 113 static int lookup_gname_helper(struct cpio *, 114 const char **name, id_t gid); 115 static const char *lookup_uname(struct cpio *, uid_t uid); 116 static int lookup_uname_helper(struct cpio *, 117 const char **name, id_t uid); 118 static void mode_in(struct cpio *); 119 static void mode_list(struct cpio *); 120 static void mode_out(struct cpio *); 121 static void mode_pass(struct cpio *, const char *); 122 static const char *remove_leading_slash(const char *); 123 static int restore_time(struct cpio *, struct archive_entry *, 124 const char *, int fd); 125 static void usage(void); 126 static void version(void); 127 128 int 129 main(int argc, char *argv[]) 130 { 131 static char buff[16384]; 132 struct cpio _cpio; /* Allocated on stack. */ 133 struct cpio *cpio; 134 const char *errmsg; 135 int uid, gid; 136 int opt; 137 138 cpio = &_cpio; 139 memset(cpio, 0, sizeof(*cpio)); 140 cpio->buff = buff; 141 cpio->buff_size = sizeof(buff); 142 143 #if defined(HAVE_SIGACTION) && defined(SIGPIPE) 144 { /* Ignore SIGPIPE signals. */ 145 struct sigaction sa; 146 sigemptyset(&sa.sa_mask); 147 sa.sa_flags = 0; 148 sa.sa_handler = SIG_IGN; 149 sigaction(SIGPIPE, &sa, NULL); 150 } 151 #endif 152 153 /* Need lafe_progname before calling lafe_warnc. */ 154 if (*argv == NULL) 155 lafe_progname = "bsdcpio"; 156 else { 157 #if defined(_WIN32) && !defined(__CYGWIN__) 158 lafe_progname = strrchr(*argv, '\\'); 159 if (strrchr(*argv, '/') > lafe_progname) 160 #endif 161 lafe_progname = strrchr(*argv, '/'); 162 if (lafe_progname != NULL) 163 lafe_progname++; 164 else 165 lafe_progname = *argv; 166 } 167 #if HAVE_SETLOCALE 168 if (setlocale(LC_ALL, "") == NULL) 169 lafe_warnc(0, "Failed to set default locale"); 170 #endif 171 172 cpio->uid_override = -1; 173 cpio->gid_override = -1; 174 cpio->argv = argv; 175 cpio->argc = argc; 176 cpio->mode = '\0'; 177 cpio->verbose = 0; 178 cpio->compress = '\0'; 179 cpio->extract_flags = ARCHIVE_EXTRACT_NO_AUTODIR; 180 cpio->extract_flags |= ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 181 cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_SYMLINKS; 182 cpio->extract_flags |= ARCHIVE_EXTRACT_SECURE_NODOTDOT; 183 cpio->extract_flags |= ARCHIVE_EXTRACT_PERM; 184 cpio->extract_flags |= ARCHIVE_EXTRACT_FFLAGS; 185 cpio->extract_flags |= ARCHIVE_EXTRACT_ACL; 186 #if !defined(_WIN32) && !defined(__CYGWIN__) 187 if (geteuid() == 0) 188 cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; 189 #endif 190 cpio->bytes_per_block = 512; 191 cpio->filename = NULL; 192 193 while ((opt = cpio_getopt(cpio)) != -1) { 194 switch (opt) { 195 case '0': /* GNU convention: --null, -0 */ 196 cpio->option_null = 1; 197 break; 198 case 'A': /* NetBSD/OpenBSD */ 199 cpio->option_append = 1; 200 break; 201 case 'a': /* POSIX 1997 */ 202 cpio->option_atime_restore = 1; 203 break; 204 case 'B': /* POSIX 1997 */ 205 cpio->bytes_per_block = 5120; 206 break; 207 case 'C': /* NetBSD/OpenBSD */ 208 cpio->bytes_per_block = atoi(cpio->argument); 209 if (cpio->bytes_per_block <= 0) 210 lafe_errc(1, 0, "Invalid blocksize %s", cpio->argument); 211 break; 212 case 'c': /* POSIX 1997 */ 213 cpio->format = "odc"; 214 break; 215 case 'd': /* POSIX 1997 */ 216 cpio->extract_flags &= ~ARCHIVE_EXTRACT_NO_AUTODIR; 217 break; 218 case 'E': /* NetBSD/OpenBSD */ 219 lafe_include_from_file(&cpio->matching, 220 cpio->argument, cpio->option_null); 221 break; 222 case 'F': /* NetBSD/OpenBSD/GNU cpio */ 223 cpio->filename = cpio->argument; 224 break; 225 case 'f': /* POSIX 1997 */ 226 lafe_exclude(&cpio->matching, cpio->argument); 227 break; 228 case 'H': /* GNU cpio (also --format) */ 229 cpio->format = cpio->argument; 230 break; 231 case 'h': 232 long_help(); 233 break; 234 case 'I': /* NetBSD/OpenBSD */ 235 cpio->filename = cpio->argument; 236 break; 237 case 'i': /* POSIX 1997 */ 238 if (cpio->mode != '\0') 239 lafe_errc(1, 0, 240 "Cannot use both -i and -%c", cpio->mode); 241 cpio->mode = opt; 242 break; 243 case 'J': /* GNU tar, others */ 244 cpio->compress = opt; 245 break; 246 case 'j': /* GNU tar, others */ 247 cpio->compress = opt; 248 break; 249 case OPTION_INSECURE: 250 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_SYMLINKS; 251 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 252 break; 253 case 'L': /* GNU cpio */ 254 cpio->option_follow_links = 1; 255 break; 256 case 'l': /* POSIX 1997 */ 257 cpio->option_link = 1; 258 break; 259 case OPTION_LZMA: /* GNU tar, others */ 260 cpio->compress = opt; 261 break; 262 case 'm': /* POSIX 1997 */ 263 cpio->extract_flags |= ARCHIVE_EXTRACT_TIME; 264 break; 265 case 'n': /* GNU cpio */ 266 cpio->option_numeric_uid_gid = 1; 267 break; 268 case OPTION_NO_PRESERVE_OWNER: /* GNU cpio */ 269 cpio->extract_flags &= ~ARCHIVE_EXTRACT_OWNER; 270 break; 271 case 'O': /* GNU cpio */ 272 cpio->filename = cpio->argument; 273 break; 274 case 'o': /* POSIX 1997 */ 275 if (cpio->mode != '\0') 276 lafe_errc(1, 0, 277 "Cannot use both -o and -%c", cpio->mode); 278 cpio->mode = opt; 279 break; 280 case 'p': /* POSIX 1997 */ 281 if (cpio->mode != '\0') 282 lafe_errc(1, 0, 283 "Cannot use both -p and -%c", cpio->mode); 284 cpio->mode = opt; 285 cpio->extract_flags &= ~ARCHIVE_EXTRACT_SECURE_NODOTDOT; 286 break; 287 case OPTION_PRESERVE_OWNER: 288 cpio->extract_flags |= ARCHIVE_EXTRACT_OWNER; 289 break; 290 case OPTION_QUIET: /* GNU cpio */ 291 cpio->quiet = 1; 292 break; 293 case 'R': /* GNU cpio, also --owner */ 294 /* TODO: owner_parse should return uname/gname 295 * also; use that to set [ug]name_override. */ 296 errmsg = owner_parse(cpio->argument, &uid, &gid); 297 if (errmsg) { 298 lafe_warnc(-1, "%s", errmsg); 299 usage(); 300 } 301 if (uid != -1) { 302 cpio->uid_override = uid; 303 cpio->uname_override = NULL; 304 } 305 if (gid != -1) { 306 cpio->gid_override = gid; 307 cpio->gname_override = NULL; 308 } 309 break; 310 case 'r': /* POSIX 1997 */ 311 cpio->option_rename = 1; 312 break; 313 case 't': /* POSIX 1997 */ 314 cpio->option_list = 1; 315 break; 316 case 'u': /* POSIX 1997 */ 317 cpio->extract_flags 318 &= ~ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER; 319 break; 320 case 'v': /* POSIX 1997 */ 321 cpio->verbose++; 322 break; 323 case 'V': /* GNU cpio */ 324 cpio->dot++; 325 break; 326 case OPTION_VERSION: /* GNU convention */ 327 version(); 328 break; 329 #if 0 330 /* 331 * cpio_getopt() handles -W specially, so it's not 332 * available here. 333 */ 334 case 'W': /* Obscure, but useful GNU convention. */ 335 break; 336 #endif 337 case 'y': /* tar convention */ 338 cpio->compress = opt; 339 break; 340 case 'Z': /* tar convention */ 341 cpio->compress = opt; 342 break; 343 case 'z': /* tar convention */ 344 cpio->compress = opt; 345 break; 346 default: 347 usage(); 348 } 349 } 350 351 /* 352 * Sanity-check args, error out on nonsensical combinations. 353 */ 354 /* -t implies -i if no mode was specified. */ 355 if (cpio->option_list && cpio->mode == '\0') 356 cpio->mode = 'i'; 357 /* -t requires -i */ 358 if (cpio->option_list && cpio->mode != 'i') 359 lafe_errc(1, 0, "Option -t requires -i"); 360 /* -n requires -it */ 361 if (cpio->option_numeric_uid_gid && !cpio->option_list) 362 lafe_errc(1, 0, "Option -n requires -it"); 363 /* Can only specify format when writing */ 364 if (cpio->format != NULL && cpio->mode != 'o') 365 lafe_errc(1, 0, "Option --format requires -o"); 366 /* -l requires -p */ 367 if (cpio->option_link && cpio->mode != 'p') 368 lafe_errc(1, 0, "Option -l requires -p"); 369 /* -v overrides -V */ 370 if (cpio->dot && cpio->verbose) 371 cpio->dot = 0; 372 /* -v overrides -V */ 373 if (cpio->dot && cpio->verbose) 374 cpio->dot = 0; 375 /* TODO: Flag other nonsensical combinations. */ 376 377 switch (cpio->mode) { 378 case 'o': 379 /* TODO: Implement old binary format in libarchive, 380 use that here. */ 381 if (cpio->format == NULL) 382 cpio->format = "odc"; /* Default format */ 383 384 mode_out(cpio); 385 break; 386 case 'i': 387 while (*cpio->argv != NULL) { 388 lafe_include(&cpio->matching, *cpio->argv); 389 --cpio->argc; 390 ++cpio->argv; 391 } 392 if (cpio->option_list) 393 mode_list(cpio); 394 else 395 mode_in(cpio); 396 break; 397 case 'p': 398 if (*cpio->argv == NULL || **cpio->argv == '\0') 399 lafe_errc(1, 0, 400 "-p mode requires a target directory"); 401 mode_pass(cpio, *cpio->argv); 402 break; 403 default: 404 lafe_errc(1, 0, 405 "Must specify at least one of -i, -o, or -p"); 406 } 407 408 free_cache(cpio->gname_cache); 409 free_cache(cpio->uname_cache); 410 return (cpio->return_value); 411 } 412 413 static void 414 usage(void) 415 { 416 const char *p; 417 418 p = lafe_progname; 419 420 fprintf(stderr, "Brief Usage:\n"); 421 fprintf(stderr, " List: %s -it < archive\n", p); 422 fprintf(stderr, " Extract: %s -i < archive\n", p); 423 fprintf(stderr, " Create: %s -o < filenames > archive\n", p); 424 fprintf(stderr, " Help: %s --help\n", p); 425 exit(1); 426 } 427 428 static const char *long_help_msg = 429 "First option must be a mode specifier:\n" 430 " -i Input -o Output -p Pass\n" 431 "Common Options:\n" 432 " -v Verbose filenames -V one dot per file\n" 433 "Create: %p -o [options] < [list of files] > [archive]\n" 434 " -J,-y,-z,--lzma Compress archive with xz/bzip2/gzip/lzma\n" 435 " --format {odc|newc|ustar} Select archive format\n" 436 "List: %p -it < [archive]\n" 437 "Extract: %p -i [options] < [archive]\n"; 438 439 440 /* 441 * Note that the word 'bsdcpio' will always appear in the first line 442 * of output. 443 * 444 * In particular, /bin/sh scripts that need to test for the presence 445 * of bsdcpio can use the following template: 446 * 447 * if (cpio --help 2>&1 | grep bsdcpio >/dev/null 2>&1 ) then \ 448 * echo bsdcpio; else echo not bsdcpio; fi 449 */ 450 static void 451 long_help(void) 452 { 453 const char *prog; 454 const char *p; 455 456 prog = lafe_progname; 457 458 fflush(stderr); 459 460 p = (strcmp(prog,"bsdcpio") != 0) ? "(bsdcpio)" : ""; 461 printf("%s%s: manipulate archive files\n", prog, p); 462 463 for (p = long_help_msg; *p != '\0'; p++) { 464 if (*p == '%') { 465 if (p[1] == 'p') { 466 fputs(prog, stdout); 467 p++; 468 } else 469 putchar('%'); 470 } else 471 putchar(*p); 472 } 473 version(); 474 } 475 476 static void 477 version(void) 478 { 479 fprintf(stdout,"bsdcpio %s -- %s\n", 480 BSDCPIO_VERSION_STRING, 481 archive_version_string()); 482 exit(0); 483 } 484 485 static void 486 mode_out(struct cpio *cpio) 487 { 488 struct archive_entry *entry, *spare; 489 struct lafe_line_reader *lr; 490 const char *p; 491 int r; 492 493 if (cpio->option_append) 494 lafe_errc(1, 0, "Append mode not yet supported."); 495 496 cpio->archive_read_disk = archive_read_disk_new(); 497 if (cpio->archive_read_disk == NULL) 498 lafe_errc(1, 0, "Failed to allocate archive object"); 499 if (cpio->option_follow_links) 500 archive_read_disk_set_symlink_logical(cpio->archive_read_disk); 501 else 502 archive_read_disk_set_symlink_physical(cpio->archive_read_disk); 503 archive_read_disk_set_standard_lookup(cpio->archive_read_disk); 504 505 cpio->archive = archive_write_new(); 506 if (cpio->archive == NULL) 507 lafe_errc(1, 0, "Failed to allocate archive object"); 508 switch (cpio->compress) { 509 case 'J': 510 r = archive_write_set_compression_xz(cpio->archive); 511 break; 512 case OPTION_LZMA: 513 r = archive_write_set_compression_lzma(cpio->archive); 514 break; 515 case 'j': case 'y': 516 r = archive_write_set_compression_bzip2(cpio->archive); 517 break; 518 case 'z': 519 r = archive_write_set_compression_gzip(cpio->archive); 520 break; 521 case 'Z': 522 r = archive_write_set_compression_compress(cpio->archive); 523 break; 524 default: 525 r = archive_write_set_compression_none(cpio->archive); 526 break; 527 } 528 if (r < ARCHIVE_WARN) 529 lafe_errc(1, 0, "Requested compression not available"); 530 r = archive_write_set_format_by_name(cpio->archive, cpio->format); 531 if (r != ARCHIVE_OK) 532 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 533 archive_write_set_bytes_per_block(cpio->archive, cpio->bytes_per_block); 534 cpio->linkresolver = archive_entry_linkresolver_new(); 535 archive_entry_linkresolver_set_strategy(cpio->linkresolver, 536 archive_format(cpio->archive)); 537 538 /* 539 * The main loop: Copy each file into the output archive. 540 */ 541 r = archive_write_open_file(cpio->archive, cpio->filename); 542 if (r != ARCHIVE_OK) 543 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 544 lr = lafe_line_reader("-", cpio->option_null); 545 while ((p = lafe_line_reader_next(lr)) != NULL) 546 file_to_archive(cpio, p); 547 lafe_line_reader_free(lr); 548 549 /* 550 * The hardlink detection may have queued up a couple of entries 551 * that can now be flushed. 552 */ 553 entry = NULL; 554 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 555 while (entry != NULL) { 556 entry_to_archive(cpio, entry); 557 archive_entry_free(entry); 558 entry = NULL; 559 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 560 } 561 562 r = archive_write_close(cpio->archive); 563 if (cpio->dot) 564 fprintf(stderr, "\n"); 565 if (r != ARCHIVE_OK) 566 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 567 568 if (!cpio->quiet) { 569 int64_t blocks = 570 (archive_position_uncompressed(cpio->archive) + 511) 571 / 512; 572 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 573 blocks == 1 ? "block" : "blocks"); 574 } 575 archive_write_free(cpio->archive); 576 } 577 578 static const char * 579 remove_leading_slash(const char *p) 580 { 581 const char *rp; 582 583 /* Remove leading "//./" or "//?/" or "//?/UNC/" 584 * (absolute path prefixes used by Windows API) */ 585 if ((p[0] == '/' || p[0] == '\\') && 586 (p[1] == '/' || p[1] == '\\') && 587 (p[2] == '.' || p[2] == '?') && 588 (p[3] == '/' || p[3] == '\\')) 589 { 590 if (p[2] == '?' && 591 (p[4] == 'U' || p[4] == 'u') && 592 (p[5] == 'N' || p[5] == 'n') && 593 (p[6] == 'C' || p[6] == 'c') && 594 (p[7] == '/' || p[7] == '\\')) 595 p += 8; 596 else 597 p += 4; 598 } 599 do { 600 rp = p; 601 /* Remove leading drive letter from archives created 602 * on Windows. */ 603 if (((p[0] >= 'a' && p[0] <= 'z') || 604 (p[0] >= 'A' && p[0] <= 'Z')) && 605 p[1] == ':') { 606 p += 2; 607 } 608 /* Remove leading "/../", "//", etc. */ 609 while (p[0] == '/' || p[0] == '\\') { 610 if (p[1] == '.' && p[2] == '.' && 611 (p[3] == '/' || p[3] == '\\')) { 612 p += 3; /* Remove "/..", leave "/" 613 * for next pass. */ 614 } else 615 p += 1; /* Remove "/". */ 616 } 617 } while (rp != p); 618 return (p); 619 } 620 621 /* 622 * This is used by both out mode (to copy objects from disk into 623 * an archive) and pass mode (to copy objects from disk to 624 * an archive_write_disk "archive"). 625 */ 626 static int 627 file_to_archive(struct cpio *cpio, const char *srcpath) 628 { 629 const char *destpath; 630 struct archive_entry *entry, *spare; 631 size_t len; 632 int r; 633 634 /* 635 * Create an archive_entry describing the source file. 636 * 637 */ 638 entry = archive_entry_new(); 639 if (entry == NULL) 640 lafe_errc(1, 0, "Couldn't allocate entry"); 641 archive_entry_copy_sourcepath(entry, srcpath); 642 r = archive_read_disk_entry_from_file(cpio->archive_read_disk, 643 entry, -1, NULL); 644 if (r < ARCHIVE_FAILED) 645 lafe_errc(1, 0, "%s", 646 archive_error_string(cpio->archive_read_disk)); 647 if (r < ARCHIVE_OK) 648 lafe_warnc(0, "%s", 649 archive_error_string(cpio->archive_read_disk)); 650 if (r <= ARCHIVE_FAILED) { 651 cpio->return_value = 1; 652 return (r); 653 } 654 655 if (cpio->uid_override >= 0) { 656 archive_entry_set_uid(entry, cpio->uid_override); 657 archive_entry_set_uname(entry, cpio->uname_override); 658 } 659 if (cpio->gid_override >= 0) { 660 archive_entry_set_gid(entry, cpio->gid_override); 661 archive_entry_set_gname(entry, cpio->gname_override); 662 } 663 664 /* 665 * Generate a destination path for this entry. 666 * "destination path" is the name to which it will be copied in 667 * pass mode or the name that will go into the archive in 668 * output mode. 669 */ 670 destpath = srcpath; 671 if (cpio->destdir) { 672 len = strlen(cpio->destdir) + strlen(srcpath) + 8; 673 if (len >= cpio->pass_destpath_alloc) { 674 while (len >= cpio->pass_destpath_alloc) { 675 cpio->pass_destpath_alloc += 512; 676 cpio->pass_destpath_alloc *= 2; 677 } 678 free(cpio->pass_destpath); 679 cpio->pass_destpath = malloc(cpio->pass_destpath_alloc); 680 if (cpio->pass_destpath == NULL) 681 lafe_errc(1, ENOMEM, 682 "Can't allocate path buffer"); 683 } 684 strcpy(cpio->pass_destpath, cpio->destdir); 685 strcat(cpio->pass_destpath, remove_leading_slash(srcpath)); 686 destpath = cpio->pass_destpath; 687 } 688 if (cpio->option_rename) 689 destpath = cpio_rename(destpath); 690 if (destpath == NULL) 691 return (0); 692 archive_entry_copy_pathname(entry, destpath); 693 694 /* 695 * If we're trying to preserve hardlinks, match them here. 696 */ 697 spare = NULL; 698 if (cpio->linkresolver != NULL 699 && archive_entry_filetype(entry) != AE_IFDIR) { 700 archive_entry_linkify(cpio->linkresolver, &entry, &spare); 701 } 702 703 if (entry != NULL) { 704 r = entry_to_archive(cpio, entry); 705 archive_entry_free(entry); 706 if (spare != NULL) { 707 if (r == 0) 708 r = entry_to_archive(cpio, spare); 709 archive_entry_free(spare); 710 } 711 } 712 return (r); 713 } 714 715 static int 716 entry_to_archive(struct cpio *cpio, struct archive_entry *entry) 717 { 718 const char *destpath = archive_entry_pathname(entry); 719 const char *srcpath = archive_entry_sourcepath(entry); 720 int fd = -1; 721 ssize_t bytes_read; 722 int r; 723 724 /* Print out the destination name to the user. */ 725 if (cpio->verbose) 726 fprintf(stderr,"%s", destpath); 727 if (cpio->dot) 728 fprintf(stderr, "."); 729 730 /* 731 * Option_link only makes sense in pass mode and for 732 * regular files. Also note: if a link operation fails 733 * because of cross-device restrictions, we'll fall back 734 * to copy mode for that entry. 735 * 736 * TODO: Test other cpio implementations to see if they 737 * hard-link anything other than regular files here. 738 */ 739 if (cpio->option_link 740 && archive_entry_filetype(entry) == AE_IFREG) 741 { 742 struct archive_entry *t; 743 /* Save the original entry in case we need it later. */ 744 t = archive_entry_clone(entry); 745 if (t == NULL) 746 lafe_errc(1, ENOMEM, "Can't create link"); 747 /* Note: link(2) doesn't create parent directories, 748 * so we use archive_write_header() instead as a 749 * convenience. */ 750 archive_entry_set_hardlink(t, srcpath); 751 /* This is a straight link that carries no data. */ 752 archive_entry_set_size(t, 0); 753 r = archive_write_header(cpio->archive, t); 754 archive_entry_free(t); 755 if (r != ARCHIVE_OK) 756 lafe_warnc(archive_errno(cpio->archive), 757 "%s", archive_error_string(cpio->archive)); 758 if (r == ARCHIVE_FATAL) 759 exit(1); 760 #ifdef EXDEV 761 if (r != ARCHIVE_OK && archive_errno(cpio->archive) == EXDEV) { 762 /* Cross-device link: Just fall through and use 763 * the original entry to copy the file over. */ 764 lafe_warnc(0, "Copying file instead"); 765 } else 766 #endif 767 return (0); 768 } 769 770 /* 771 * Make sure we can open the file (if necessary) before 772 * trying to write the header. 773 */ 774 if (archive_entry_filetype(entry) == AE_IFREG) { 775 if (archive_entry_size(entry) > 0) { 776 fd = open(srcpath, O_RDONLY | O_BINARY); 777 if (fd < 0) { 778 lafe_warnc(errno, 779 "%s: could not open file", srcpath); 780 goto cleanup; 781 } 782 } 783 } else { 784 archive_entry_set_size(entry, 0); 785 } 786 787 r = archive_write_header(cpio->archive, entry); 788 789 if (r != ARCHIVE_OK) 790 lafe_warnc(archive_errno(cpio->archive), 791 "%s: %s", 792 srcpath, 793 archive_error_string(cpio->archive)); 794 795 if (r == ARCHIVE_FATAL) 796 exit(1); 797 798 if (r >= ARCHIVE_WARN && archive_entry_size(entry) > 0 && fd >= 0) { 799 bytes_read = read(fd, cpio->buff, cpio->buff_size); 800 while (bytes_read > 0) { 801 r = archive_write_data(cpio->archive, 802 cpio->buff, bytes_read); 803 if (r < 0) 804 lafe_errc(1, archive_errno(cpio->archive), 805 "%s", archive_error_string(cpio->archive)); 806 if (r < bytes_read) { 807 lafe_warnc(0, 808 "Truncated write; file may have grown while being archived."); 809 } 810 bytes_read = read(fd, cpio->buff, cpio->buff_size); 811 } 812 } 813 814 fd = restore_time(cpio, entry, srcpath, fd); 815 816 cleanup: 817 if (cpio->verbose) 818 fprintf(stderr,"\n"); 819 if (fd >= 0) 820 close(fd); 821 return (0); 822 } 823 824 static int 825 restore_time(struct cpio *cpio, struct archive_entry *entry, 826 const char *name, int fd) 827 { 828 #ifndef HAVE_UTIMES 829 static int warned = 0; 830 831 (void)cpio; /* UNUSED */ 832 (void)entry; /* UNUSED */ 833 (void)name; /* UNUSED */ 834 835 if (!warned) 836 lafe_warnc(0, "Can't restore access times on this platform"); 837 warned = 1; 838 return (fd); 839 #else 840 #if defined(_WIN32) && !defined(__CYGWIN__) 841 struct __timeval times[2]; 842 #else 843 struct timeval times[2]; 844 #endif 845 846 if (!cpio->option_atime_restore) 847 return (fd); 848 849 times[1].tv_sec = archive_entry_mtime(entry); 850 times[1].tv_usec = archive_entry_mtime_nsec(entry) / 1000; 851 852 times[0].tv_sec = archive_entry_atime(entry); 853 times[0].tv_usec = archive_entry_atime_nsec(entry) / 1000; 854 855 #if defined(HAVE_FUTIMES) && !defined(__CYGWIN__) 856 if (fd >= 0 && futimes(fd, times) == 0) 857 return (fd); 858 #endif 859 /* 860 * Some platform cannot restore access times if the file descriptor 861 * is still opened. 862 */ 863 if (fd >= 0) { 864 close(fd); 865 fd = -1; 866 } 867 868 #ifdef HAVE_LUTIMES 869 if (lutimes(name, times) != 0) 870 #else 871 if ((AE_IFLNK != archive_entry_filetype(entry)) 872 && utimes(name, times) != 0) 873 #endif 874 lafe_warnc(errno, "Can't update time for %s", name); 875 #endif 876 return (fd); 877 } 878 879 880 static void 881 mode_in(struct cpio *cpio) 882 { 883 struct archive *a; 884 struct archive_entry *entry; 885 struct archive *ext; 886 const char *destpath; 887 int r; 888 889 ext = archive_write_disk_new(); 890 if (ext == NULL) 891 lafe_errc(1, 0, "Couldn't allocate restore object"); 892 r = archive_write_disk_set_options(ext, cpio->extract_flags); 893 if (r != ARCHIVE_OK) 894 lafe_errc(1, 0, "%s", archive_error_string(ext)); 895 a = archive_read_new(); 896 if (a == NULL) 897 lafe_errc(1, 0, "Couldn't allocate archive object"); 898 archive_read_support_filter_all(a); 899 archive_read_support_format_all(a); 900 901 if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block)) 902 lafe_errc(1, archive_errno(a), 903 "%s", archive_error_string(a)); 904 for (;;) { 905 r = archive_read_next_header(a, &entry); 906 if (r == ARCHIVE_EOF) 907 break; 908 if (r != ARCHIVE_OK) { 909 lafe_errc(1, archive_errno(a), 910 "%s", archive_error_string(a)); 911 } 912 if (lafe_excluded(cpio->matching, archive_entry_pathname(entry))) 913 continue; 914 if (cpio->option_rename) { 915 destpath = cpio_rename(archive_entry_pathname(entry)); 916 archive_entry_set_pathname(entry, destpath); 917 } else 918 destpath = archive_entry_pathname(entry); 919 if (destpath == NULL) 920 continue; 921 if (cpio->verbose) 922 fprintf(stderr, "%s\n", destpath); 923 if (cpio->dot) 924 fprintf(stderr, "."); 925 if (cpio->uid_override >= 0) 926 archive_entry_set_uid(entry, cpio->uid_override); 927 if (cpio->gid_override >= 0) 928 archive_entry_set_gid(entry, cpio->gid_override); 929 r = archive_write_header(ext, entry); 930 if (r != ARCHIVE_OK) { 931 fprintf(stderr, "%s: %s\n", 932 archive_entry_pathname(entry), 933 archive_error_string(ext)); 934 } else if (!archive_entry_size_is_set(entry) 935 || archive_entry_size(entry) > 0) { 936 r = extract_data(a, ext); 937 if (r != ARCHIVE_OK) 938 cpio->return_value = 1; 939 } 940 } 941 r = archive_read_close(a); 942 if (cpio->dot) 943 fprintf(stderr, "\n"); 944 if (r != ARCHIVE_OK) 945 lafe_errc(1, 0, "%s", archive_error_string(a)); 946 r = archive_write_close(ext); 947 if (r != ARCHIVE_OK) 948 lafe_errc(1, 0, "%s", archive_error_string(ext)); 949 if (!cpio->quiet) { 950 int64_t blocks = (archive_position_uncompressed(a) + 511) 951 / 512; 952 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 953 blocks == 1 ? "block" : "blocks"); 954 } 955 archive_read_free(a); 956 archive_write_free(ext); 957 exit(cpio->return_value); 958 } 959 960 /* 961 * Exits if there's a fatal error. Returns ARCHIVE_OK 962 * if everything is kosher. 963 */ 964 static int 965 extract_data(struct archive *ar, struct archive *aw) 966 { 967 int r; 968 size_t size; 969 const void *block; 970 int64_t offset; 971 972 for (;;) { 973 r = archive_read_data_block(ar, &block, &size, &offset); 974 if (r == ARCHIVE_EOF) 975 return (ARCHIVE_OK); 976 if (r != ARCHIVE_OK) { 977 lafe_warnc(archive_errno(ar), 978 "%s", archive_error_string(ar)); 979 exit(1); 980 } 981 r = archive_write_data_block(aw, block, size, offset); 982 if (r != ARCHIVE_OK) { 983 lafe_warnc(archive_errno(aw), 984 "%s", archive_error_string(aw)); 985 return (r); 986 } 987 } 988 } 989 990 static void 991 mode_list(struct cpio *cpio) 992 { 993 struct archive *a; 994 struct archive_entry *entry; 995 int r; 996 997 a = archive_read_new(); 998 if (a == NULL) 999 lafe_errc(1, 0, "Couldn't allocate archive object"); 1000 archive_read_support_filter_all(a); 1001 archive_read_support_format_all(a); 1002 1003 if (archive_read_open_file(a, cpio->filename, cpio->bytes_per_block)) 1004 lafe_errc(1, archive_errno(a), 1005 "%s", archive_error_string(a)); 1006 for (;;) { 1007 r = archive_read_next_header(a, &entry); 1008 if (r == ARCHIVE_EOF) 1009 break; 1010 if (r != ARCHIVE_OK) { 1011 lafe_errc(1, archive_errno(a), 1012 "%s", archive_error_string(a)); 1013 } 1014 if (lafe_excluded(cpio->matching, archive_entry_pathname(entry))) 1015 continue; 1016 if (cpio->verbose) 1017 list_item_verbose(cpio, entry); 1018 else 1019 fprintf(stdout, "%s\n", archive_entry_pathname(entry)); 1020 } 1021 r = archive_read_close(a); 1022 if (r != ARCHIVE_OK) 1023 lafe_errc(1, 0, "%s", archive_error_string(a)); 1024 if (!cpio->quiet) { 1025 int64_t blocks = (archive_position_uncompressed(a) + 511) 1026 / 512; 1027 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 1028 blocks == 1 ? "block" : "blocks"); 1029 } 1030 archive_read_free(a); 1031 exit(0); 1032 } 1033 1034 /* 1035 * Display information about the current file. 1036 * 1037 * The format here roughly duplicates the output of 'ls -l'. 1038 * This is based on SUSv2, where 'tar tv' is documented as 1039 * listing additional information in an "unspecified format," 1040 * and 'pax -l' is documented as using the same format as 'ls -l'. 1041 */ 1042 static void 1043 list_item_verbose(struct cpio *cpio, struct archive_entry *entry) 1044 { 1045 char size[32]; 1046 char date[32]; 1047 char uids[16], gids[16]; 1048 const char *uname, *gname; 1049 FILE *out = stdout; 1050 const char *fmt; 1051 time_t mtime; 1052 static time_t now; 1053 1054 if (!now) 1055 time(&now); 1056 1057 if (cpio->option_numeric_uid_gid) { 1058 /* Format numeric uid/gid for display. */ 1059 strcpy(uids, cpio_i64toa(archive_entry_uid(entry))); 1060 uname = uids; 1061 strcpy(gids, cpio_i64toa(archive_entry_gid(entry))); 1062 gname = gids; 1063 } else { 1064 /* Use uname if it's present, else lookup name from uid. */ 1065 uname = archive_entry_uname(entry); 1066 if (uname == NULL) 1067 uname = lookup_uname(cpio, (uid_t)archive_entry_uid(entry)); 1068 /* Use gname if it's present, else lookup name from gid. */ 1069 gname = archive_entry_gname(entry); 1070 if (gname == NULL) 1071 gname = lookup_gname(cpio, (uid_t)archive_entry_gid(entry)); 1072 } 1073 1074 /* Print device number or file size. */ 1075 if (archive_entry_filetype(entry) == AE_IFCHR 1076 || archive_entry_filetype(entry) == AE_IFBLK) { 1077 snprintf(size, sizeof(size), "%lu,%lu", 1078 (unsigned long)archive_entry_rdevmajor(entry), 1079 (unsigned long)archive_entry_rdevminor(entry)); 1080 } else { 1081 strcpy(size, cpio_i64toa(archive_entry_size(entry))); 1082 } 1083 1084 /* Format the time using 'ls -l' conventions. */ 1085 mtime = archive_entry_mtime(entry); 1086 #if defined(_WIN32) && !defined(__CYGWIN__) 1087 /* Windows' strftime function does not support %e format. */ 1088 if (mtime - now > 365*86400/2 1089 || mtime - now < -365*86400/2) 1090 fmt = cpio->day_first ? "%d %b %Y" : "%b %d %Y"; 1091 else 1092 fmt = cpio->day_first ? "%d %b %H:%M" : "%b %d %H:%M"; 1093 #else 1094 if (abs(mtime - now) > (365/2)*86400) 1095 fmt = cpio->day_first ? "%e %b %Y" : "%b %e %Y"; 1096 else 1097 fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M"; 1098 #endif 1099 strftime(date, sizeof(date), fmt, localtime(&mtime)); 1100 1101 fprintf(out, "%s%3d %-8s %-8s %8s %12s %s", 1102 archive_entry_strmode(entry), 1103 archive_entry_nlink(entry), 1104 uname, gname, size, date, 1105 archive_entry_pathname(entry)); 1106 1107 /* Extra information for links. */ 1108 if (archive_entry_hardlink(entry)) /* Hard link */ 1109 fprintf(out, " link to %s", archive_entry_hardlink(entry)); 1110 else if (archive_entry_symlink(entry)) /* Symbolic link */ 1111 fprintf(out, " -> %s", archive_entry_symlink(entry)); 1112 fprintf(out, "\n"); 1113 } 1114 1115 static void 1116 mode_pass(struct cpio *cpio, const char *destdir) 1117 { 1118 struct lafe_line_reader *lr; 1119 const char *p; 1120 int r; 1121 1122 /* Ensure target dir has a trailing '/' to simplify path surgery. */ 1123 cpio->destdir = malloc(strlen(destdir) + 8); 1124 strcpy(cpio->destdir, destdir); 1125 if (destdir[strlen(destdir) - 1] != '/') 1126 strcat(cpio->destdir, "/"); 1127 1128 cpio->archive = archive_write_disk_new(); 1129 if (cpio->archive == NULL) 1130 lafe_errc(1, 0, "Failed to allocate archive object"); 1131 r = archive_write_disk_set_options(cpio->archive, cpio->extract_flags); 1132 if (r != ARCHIVE_OK) 1133 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 1134 cpio->linkresolver = archive_entry_linkresolver_new(); 1135 archive_write_disk_set_standard_lookup(cpio->archive); 1136 1137 cpio->archive_read_disk = archive_read_disk_new(); 1138 if (cpio->archive_read_disk == NULL) 1139 lafe_errc(1, 0, "Failed to allocate archive object"); 1140 if (cpio->option_follow_links) 1141 archive_read_disk_set_symlink_logical(cpio->archive_read_disk); 1142 else 1143 archive_read_disk_set_symlink_physical(cpio->archive_read_disk); 1144 archive_read_disk_set_standard_lookup(cpio->archive_read_disk); 1145 1146 lr = lafe_line_reader("-", cpio->option_null); 1147 while ((p = lafe_line_reader_next(lr)) != NULL) 1148 file_to_archive(cpio, p); 1149 lafe_line_reader_free(lr); 1150 1151 archive_entry_linkresolver_free(cpio->linkresolver); 1152 r = archive_write_close(cpio->archive); 1153 if (cpio->dot) 1154 fprintf(stderr, "\n"); 1155 if (r != ARCHIVE_OK) 1156 lafe_errc(1, 0, "%s", archive_error_string(cpio->archive)); 1157 1158 if (!cpio->quiet) { 1159 int64_t blocks = 1160 (archive_position_uncompressed(cpio->archive) + 511) 1161 / 512; 1162 fprintf(stderr, "%lu %s\n", (unsigned long)blocks, 1163 blocks == 1 ? "block" : "blocks"); 1164 } 1165 1166 archive_write_free(cpio->archive); 1167 } 1168 1169 /* 1170 * Prompt for a new name for this entry. Returns a pointer to the 1171 * new name or NULL if the entry should not be copied. This 1172 * implements the semantics defined in POSIX.1-1996, which specifies 1173 * that an input of '.' means the name should be unchanged. GNU cpio 1174 * treats '.' as a literal new name. 1175 */ 1176 static const char * 1177 cpio_rename(const char *name) 1178 { 1179 static char buff[1024]; 1180 FILE *t; 1181 char *p, *ret; 1182 #if defined(_WIN32) && !defined(__CYGWIN__) 1183 FILE *to; 1184 1185 t = fopen("CONIN$", "r"); 1186 if (t == NULL) 1187 return (name); 1188 to = fopen("CONOUT$", "w"); 1189 if (to == NULL) 1190 return (name); 1191 fprintf(to, "%s (Enter/./(new name))? ", name); 1192 fclose(to); 1193 #else 1194 t = fopen("/dev/tty", "r+"); 1195 if (t == NULL) 1196 return (name); 1197 fprintf(t, "%s (Enter/./(new name))? ", name); 1198 fflush(t); 1199 #endif 1200 1201 p = fgets(buff, sizeof(buff), t); 1202 fclose(t); 1203 if (p == NULL) 1204 /* End-of-file is a blank line. */ 1205 return (NULL); 1206 1207 while (*p == ' ' || *p == '\t') 1208 ++p; 1209 if (*p == '\n' || *p == '\0') 1210 /* Empty line. */ 1211 return (NULL); 1212 if (*p == '.' && p[1] == '\n') 1213 /* Single period preserves original name. */ 1214 return (name); 1215 ret = p; 1216 /* Trim the final newline. */ 1217 while (*p != '\0' && *p != '\n') 1218 ++p; 1219 /* Overwrite the final \n with a null character. */ 1220 *p = '\0'; 1221 return (ret); 1222 } 1223 1224 static void 1225 free_cache(struct name_cache *cache) 1226 { 1227 size_t i; 1228 1229 if (cache != NULL) { 1230 for (i = 0; i < cache->size; i++) 1231 free(cache->cache[i].name); 1232 free(cache); 1233 } 1234 } 1235 1236 /* 1237 * Lookup uname/gname from uid/gid, return NULL if no match. 1238 */ 1239 static const char * 1240 lookup_name(struct cpio *cpio, struct name_cache **name_cache_variable, 1241 int (*lookup_fn)(struct cpio *, const char **, id_t), id_t id) 1242 { 1243 char asnum[16]; 1244 struct name_cache *cache; 1245 const char *name; 1246 int slot; 1247 1248 1249 if (*name_cache_variable == NULL) { 1250 *name_cache_variable = malloc(sizeof(struct name_cache)); 1251 if (*name_cache_variable == NULL) 1252 lafe_errc(1, ENOMEM, "No more memory"); 1253 memset(*name_cache_variable, 0, sizeof(struct name_cache)); 1254 (*name_cache_variable)->size = name_cache_size; 1255 } 1256 1257 cache = *name_cache_variable; 1258 cache->probes++; 1259 1260 slot = id % cache->size; 1261 if (cache->cache[slot].name != NULL) { 1262 if (cache->cache[slot].id == id) { 1263 cache->hits++; 1264 return (cache->cache[slot].name); 1265 } 1266 free(cache->cache[slot].name); 1267 cache->cache[slot].name = NULL; 1268 } 1269 1270 if (lookup_fn(cpio, &name, id) == 0) { 1271 if (name == NULL || name[0] == '\0') { 1272 /* If lookup failed, format it as a number. */ 1273 snprintf(asnum, sizeof(asnum), "%u", (unsigned)id); 1274 name = asnum; 1275 } 1276 cache->cache[slot].name = strdup(name); 1277 if (cache->cache[slot].name != NULL) { 1278 cache->cache[slot].id = id; 1279 return (cache->cache[slot].name); 1280 } 1281 /* 1282 * Conveniently, NULL marks an empty slot, so 1283 * if the strdup() fails, we've just failed to 1284 * cache it. No recovery necessary. 1285 */ 1286 } 1287 return (NULL); 1288 } 1289 1290 static const char * 1291 lookup_uname(struct cpio *cpio, uid_t uid) 1292 { 1293 return (lookup_name(cpio, &cpio->uname_cache, 1294 &lookup_uname_helper, (id_t)uid)); 1295 } 1296 1297 static int 1298 lookup_uname_helper(struct cpio *cpio, const char **name, id_t id) 1299 { 1300 struct passwd *pwent; 1301 1302 (void)cpio; /* UNUSED */ 1303 1304 errno = 0; 1305 pwent = getpwuid((uid_t)id); 1306 if (pwent == NULL) { 1307 *name = NULL; 1308 if (errno != 0 && errno != ENOENT) 1309 lafe_warnc(errno, "getpwuid(%d) failed", id); 1310 return (errno); 1311 } 1312 1313 *name = pwent->pw_name; 1314 return (0); 1315 } 1316 1317 static const char * 1318 lookup_gname(struct cpio *cpio, gid_t gid) 1319 { 1320 return (lookup_name(cpio, &cpio->gname_cache, 1321 &lookup_gname_helper, (id_t)gid)); 1322 } 1323 1324 static int 1325 lookup_gname_helper(struct cpio *cpio, const char **name, id_t id) 1326 { 1327 struct group *grent; 1328 1329 (void)cpio; /* UNUSED */ 1330 1331 errno = 0; 1332 grent = getgrgid((gid_t)id); 1333 if (grent == NULL) { 1334 *name = NULL; 1335 if (errno != 0) 1336 lafe_warnc(errno, "getgrgid(%d) failed", id); 1337 return (errno); 1338 } 1339 1340 *name = grent->gr_name; 1341 return (0); 1342 } 1343 1344 /* 1345 * It would be nice to just use printf() for formatting large numbers, 1346 * but the compatibility problems are a big headache. Hence the 1347 * following simple utility function. 1348 */ 1349 const char * 1350 cpio_i64toa(int64_t n0) 1351 { 1352 /* 2^64 =~ 1.8 * 10^19, so 20 decimal digits suffice. 1353 * We also need 1 byte for '-' and 1 for '\0'. 1354 */ 1355 static char buff[22]; 1356 int64_t n = n0 < 0 ? -n0 : n0; 1357 char *p = buff + sizeof(buff); 1358 1359 *--p = '\0'; 1360 do { 1361 *--p = '0' + (int)(n % 10); 1362 n /= 10; 1363 } while (n > 0); 1364 if (n0 < 0) 1365 *--p = '-'; 1366 return p; 1367 } 1368