bsdunzip.c (e64fe029e9d3ce476e77a478318e0c3cd201ff08) bsdunzip.c (64884e0d4ce7ed57c970e1b34f93e3754c656900)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009, 2010 Joerg Sonnenberger <joerg@NetBSD.org>
5 * Copyright (c) 2007-2008 Dag-Erling Smørgrav
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 26 unchanged lines hidden (view full) ---

35 * behaviour for the purpose of implementing the -n, -o, -L and -a
36 * options.
37 */
38
39#include "bsdunzip_platform.h"
40
41#ifdef HAVE_SYS_QUEUE_H
42#include <sys/queue.h>
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009, 2010 Joerg Sonnenberger <joerg@NetBSD.org>
5 * Copyright (c) 2007-2008 Dag-Erling Smørgrav
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 26 unchanged lines hidden (view full) ---

35 * behaviour for the purpose of implementing the -n, -o, -L and -a
36 * options.
37 */
38
39#include "bsdunzip_platform.h"
40
41#ifdef HAVE_SYS_QUEUE_H
42#include <sys/queue.h>
43#else
44#include "la_queue.h"
43#endif
44#ifdef HAVE_SYS_STAT_H
45#include <sys/stat.h>
46#endif
47
48#ifdef HAVE_CTYPE_H
49#include <ctype.h>
50#endif

--- 14 unchanged lines hidden (view full) ---

65#include <stdlib.h>
66#endif
67#ifdef HAVE_STRING_H
68#include <string.h>
69#endif
70#ifdef HAVE_UNISTD_H
71#include <unistd.h>
72#endif
45#endif
46#ifdef HAVE_SYS_STAT_H
47#include <sys/stat.h>
48#endif
49
50#ifdef HAVE_CTYPE_H
51#include <ctype.h>
52#endif

--- 14 unchanged lines hidden (view full) ---

67#include <stdlib.h>
68#endif
69#ifdef HAVE_STRING_H
70#include <string.h>
71#endif
72#ifdef HAVE_UNISTD_H
73#include <unistd.h>
74#endif
75#if ((!defined(HAVE_UTIMENSAT) && defined(HAVE_LUTIMES)) || \
76 (!defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES)))
77#ifdef HAVE_SYS_TIME_H
78#include <sys/time.h>
79#endif
80#endif
73
74#include <archive.h>
75#include <archive_entry.h>
76#include "passphrase.h"
77#include "err.h"
78
79/* command-line options */
80static int a_opt; /* convert EOL */
81static int C_opt; /* match case-insensitively */
82static int c_opt; /* extract to stdout */
83static const char *d_arg; /* directory */
84static int f_opt; /* update existing files only */
81
82#include <archive.h>
83#include <archive_entry.h>
84#include "passphrase.h"
85#include "err.h"
86
87/* command-line options */
88static int a_opt; /* convert EOL */
89static int C_opt; /* match case-insensitively */
90static int c_opt; /* extract to stdout */
91static const char *d_arg; /* directory */
92static int f_opt; /* update existing files only */
93static char *O_arg; /* encoding */
85static int j_opt; /* junk directories */
86static int L_opt; /* lowercase names */
87static int n_opt; /* never overwrite */
88static int o_opt; /* always overwrite */
89static int p_opt; /* extract to stdout, quiet */
90static char *P_arg; /* passphrase */
91static int q_opt; /* quiet */
92static int t_opt; /* test */

--- 530 unchanged lines hidden (view full) ---

623 * Extract a regular file.
624 */
625static void
626extract_file(struct archive *a, struct archive_entry *e, char **path)
627{
628 int mode;
629 struct timespec mtime;
630 struct stat sb;
94static int j_opt; /* junk directories */
95static int L_opt; /* lowercase names */
96static int n_opt; /* never overwrite */
97static int o_opt; /* always overwrite */
98static int p_opt; /* extract to stdout, quiet */
99static char *P_arg; /* passphrase */
100static int q_opt; /* quiet */
101static int t_opt; /* test */

--- 530 unchanged lines hidden (view full) ---

632 * Extract a regular file.
633 */
634static void
635extract_file(struct archive *a, struct archive_entry *e, char **path)
636{
637 int mode;
638 struct timespec mtime;
639 struct stat sb;
631 struct timespec ts[2];
632 int fd, check, text;
633 const char *linkname;
640 int fd, check, text;
641 const char *linkname;
642#if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMENS)
643 struct timespec ts[2];
644#endif
645#if ((!defined(HAVE_UTIMENSAT) && defined(HAVE_LUTIMES)) || \
646 (!defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES)))
647 struct timeval times[2];
648#endif
634
635 mode = archive_entry_mode(e) & 0777;
636 if (mode == 0)
637 mode = 0644;
638 mtime.tv_sec = archive_entry_mtime(e);
639 mtime.tv_nsec = archive_entry_mtime_nsec(e);
640
641 /* look for existing file of same name */

--- 37 unchanged lines hidden (view full) ---

679 if (check == -1)
680 return; /* do not overwrite */
681 }
682 } else {
683 if (f_opt)
684 return;
685 }
686
649
650 mode = archive_entry_mode(e) & 0777;
651 if (mode == 0)
652 mode = 0644;
653 mtime.tv_sec = archive_entry_mtime(e);
654 mtime.tv_nsec = archive_entry_mtime_nsec(e);
655
656 /* look for existing file of same name */

--- 37 unchanged lines hidden (view full) ---

694 if (check == -1)
695 return; /* do not overwrite */
696 }
697 } else {
698 if (f_opt)
699 return;
700 }
701
702#if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMENS)
687 ts[0].tv_sec = 0;
688 ts[0].tv_nsec = UTIME_NOW;
689 ts[1] = mtime;
703 ts[0].tv_sec = 0;
704 ts[0].tv_nsec = UTIME_NOW;
705 ts[1] = mtime;
706#endif
707#if ((!defined(HAVE_UTIMENSAT) && defined(HAVE_LUTIMES)) || \
708 (!defined(HAVE_FUTIMENS) && defined(HAVE_FUTIMES)))
709 times[0].tv_sec = 0;
710 times[0].tv_usec = -1;
711 times[1].tv_sec = mtime.tv_sec;
712 times[1].tv_usec = mtime.tv_nsec / 1000;
713#endif
690
691 /* process symlinks */
692 linkname = archive_entry_symlink(e);
693 if (linkname != NULL) {
694 if (symlink(linkname, *path) != 0)
695 error("symlink('%s')", *path);
696 info(" extracting: %s -> %s\n", *path, linkname);
714
715 /* process symlinks */
716 linkname = archive_entry_symlink(e);
717 if (linkname != NULL) {
718 if (symlink(linkname, *path) != 0)
719 error("symlink('%s')", *path);
720 info(" extracting: %s -> %s\n", *path, linkname);
721#ifdef HAVE_LCHMOD
697 if (lchmod(*path, mode) != 0)
698 warning("Cannot set mode for '%s'", *path);
722 if (lchmod(*path, mode) != 0)
723 warning("Cannot set mode for '%s'", *path);
724#endif
699 /* set access and modification time */
725 /* set access and modification time */
726#if defined(HAVE_UTIMENSAT)
700 if (utimensat(AT_FDCWD, *path, ts, AT_SYMLINK_NOFOLLOW) != 0)
701 warning("utimensat('%s')", *path);
727 if (utimensat(AT_FDCWD, *path, ts, AT_SYMLINK_NOFOLLOW) != 0)
728 warning("utimensat('%s')", *path);
729#elif defined(HAVE_LUTIMES)
730 gettimeofday(&times[0], NULL);
731 if (lutimes(*path, times) != 0)
732 warning("lutimes('%s')", *path);
733#endif
702 return;
703 }
704
705 if ((fd = open(*path, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0)
706 error("open('%s')", *path);
707
708 info(" extracting: %s", *path);
709
710 text = extract2fd(a, *path, fd);
711
712 if (tty)
713 info(" \b\b");
714 if (text)
715 info(" (text)");
716 info("\n");
717
718 /* set access and modification time */
734 return;
735 }
736
737 if ((fd = open(*path, O_RDWR|O_CREAT|O_TRUNC, mode)) < 0)
738 error("open('%s')", *path);
739
740 info(" extracting: %s", *path);
741
742 text = extract2fd(a, *path, fd);
743
744 if (tty)
745 info(" \b\b");
746 if (text)
747 info(" (text)");
748 info("\n");
749
750 /* set access and modification time */
751#if defined(HAVE_FUTIMENS)
719 if (futimens(fd, ts) != 0)
720 error("futimens('%s')", *path);
752 if (futimens(fd, ts) != 0)
753 error("futimens('%s')", *path);
754#elif defined(HAVE_FUTIMES)
755 gettimeofday(&times[0], NULL);
756 if (futimes(fd, times) != 0)
757 error("futimes('%s')", *path);
758#endif
721 if (close(fd) != 0)
722 error("close('%s')", *path);
723}
724
725/*
726 * Extract a zipfile entry: first perform some sanity checks to ensure
727 * that it is either a directory or a regular file and that the path is
728 * not absolute and does not try to break out of the current directory;

--- 227 unchanged lines hidden (view full) ---

956 int ret;
957 uintmax_t total_size, file_count, error_count;
958
959 if ((a = archive_read_new()) == NULL)
960 error("archive_read_new failed");
961
962 ac(archive_read_support_format_zip(a));
963
759 if (close(fd) != 0)
760 error("close('%s')", *path);
761}
762
763/*
764 * Extract a zipfile entry: first perform some sanity checks to ensure
765 * that it is either a directory or a regular file and that the path is
766 * not absolute and does not try to break out of the current directory;

--- 227 unchanged lines hidden (view full) ---

994 int ret;
995 uintmax_t total_size, file_count, error_count;
996
997 if ((a = archive_read_new()) == NULL)
998 error("archive_read_new failed");
999
1000 ac(archive_read_support_format_zip(a));
1001
1002 if (O_arg)
1003 ac(archive_read_set_format_option(a, "zip", "hdrcharset", O_arg));
1004
964 if (P_arg)
965 archive_read_add_passphrase(a, P_arg);
966 else
967 archive_read_set_passphrase_callback(a, NULL,
968 &passphrase_callback);
969
970 ac(archive_read_open_filename(a, fn, 8192));
971

--- 66 unchanged lines hidden (view full) ---

1038 }
1039}
1040
1041static void
1042usage(void)
1043{
1044
1045 fprintf(stderr,
1005 if (P_arg)
1006 archive_read_add_passphrase(a, P_arg);
1007 else
1008 archive_read_set_passphrase_callback(a, NULL,
1009 &passphrase_callback);
1010
1011 ac(archive_read_open_filename(a, fn, 8192));
1012

--- 66 unchanged lines hidden (view full) ---

1079 }
1080}
1081
1082static void
1083usage(void)
1084{
1085
1086 fprintf(stderr,
1046"Usage: unzip [-aCcfjLlnopqtuvyZ1] [-d dir] [-x pattern] [-P password] zipfile\n"
1087"Usage: unzip [-aCcfjLlnopqtuvyZ1] [{-O|-I} encoding] [-d dir] [-x pattern] [-P password] zipfile\n"
1047" [member ...]\n");
1048 exit(EXIT_FAILURE);
1049}
1050
1051static int
1052getopts(int argc, char *argv[])
1053{
1054 int opt;
1055
1088" [member ...]\n");
1089 exit(EXIT_FAILURE);
1090}
1091
1092static int
1093getopts(int argc, char *argv[])
1094{
1095 int opt;
1096
1056 optreset = optind = 1;
1057 while ((opt = getopt(argc, argv, "aCcd:fjLlnopP:qtuvx:yZ1")) != -1)
1097 optind = 1;
1098#ifdef HAVE_GETOPT_OPTRESET
1099 optreset = 1;
1100#endif
1101 while ((opt = getopt(argc, argv, "aCcd:fI:jLlnO:opP:qtuvx:yZ1")) != -1)
1058 switch (opt) {
1059 case '1':
1060 Z1_opt = 1;
1061 break;
1062 case 'a':
1063 a_opt = 1;
1064 break;
1065 case 'C':
1066 C_opt = 1;
1067 break;
1068 case 'c':
1069 c_opt = 1;
1070 break;
1071 case 'd':
1072 d_arg = optarg;
1073 break;
1074 case 'f':
1075 f_opt = 1;
1076 break;
1102 switch (opt) {
1103 case '1':
1104 Z1_opt = 1;
1105 break;
1106 case 'a':
1107 a_opt = 1;
1108 break;
1109 case 'C':
1110 C_opt = 1;
1111 break;
1112 case 'c':
1113 c_opt = 1;
1114 break;
1115 case 'd':
1116 d_arg = optarg;
1117 break;
1118 case 'f':
1119 f_opt = 1;
1120 break;
1121 case 'I':
1122 case 'O':
1123 O_arg = optarg;
1124 break;
1077 case 'j':
1078 j_opt = 1;
1079 break;
1080 case 'L':
1081 L_opt = 1;
1082 break;
1083 case 'l':
1084 if (v_opt == 0)

--- 102 unchanged lines hidden ---
1125 case 'j':
1126 j_opt = 1;
1127 break;
1128 case 'L':
1129 L_opt = 1;
1130 break;
1131 case 'l':
1132 if (v_opt == 0)

--- 102 unchanged lines hidden ---