⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tar.c

📁 gnu tar 源码包。 tar 软件是 Unix 系统下的一个打包软件
💻 C
📖 第 1 页 / 共 5 页
字号:
/* A tar (tape archiver) program.   Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,   2001, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.   Written by John Gilmore, starting 1985-08-25.   This program is free software; you can redistribute it and/or modify it   under the terms of the GNU General Public License as published by the   Free Software Foundation; either version 3, or (at your option) any later   version.   This program is distributed in the hope that it will be useful, but   WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General   Public License for more details.   You should have received a copy of the GNU General Public License along   with this program; if not, write to the Free Software Foundation, Inc.,   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */#include <system.h>#include <fnmatch.h>#include <argp.h>#include <argp-namefrob.h>#include <argp-fmtstream.h>#include <signal.h>#if ! defined SIGCHLD && defined SIGCLD# define SIGCHLD SIGCLD#endif/* The following causes "common.h" to produce definitions of all the global   variables, rather than just "extern" declarations of them.  GNU tar does   depend on the system loader to preset all GLOBAL variables to neutral (or   zero) values; explicit initialization is usually not done.  */#define GLOBAL#include "common.h"#include <argmatch.h>#include <closeout.h>#include <configmake.h>#include <exitfail.h>#include <getdate.h>#include <rmt.h>#include <rmt-command.h>#include <prepargs.h>#include <quotearg.h>#include <version-etc.h>#include <xstrtol.h>#include <stdopen.h>/* Local declarations.  */#ifndef DEFAULT_ARCHIVE_FORMAT# define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT#endif#ifndef DEFAULT_ARCHIVE# define DEFAULT_ARCHIVE "tar.out"#endif#ifndef DEFAULT_BLOCKING# define DEFAULT_BLOCKING 20#endif/* Miscellaneous.  *//* Name of option using stdin.  */static const char *stdin_used_by;/* Doesn't return if stdin already requested.  */voidrequest_stdin (const char *option){  if (stdin_used_by)    USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),		  stdin_used_by, option));  stdin_used_by = option;}extern int rpmatch (char const *response);/* Returns true if and only if the user typed an affirmative response.  */intconfirm (const char *message_action, const char *message_name){  static FILE *confirm_file;  static int confirm_file_EOF;  bool status = false;  if (!confirm_file)    {      if (archive == 0 || stdin_used_by)	{	  confirm_file = fopen (TTY_NAME, "r");	  if (! confirm_file)	    open_fatal (TTY_NAME);	}      else	{	  request_stdin ("-w");	  confirm_file = stdin;	}    }  fprintf (stdlis, "%s %s?", message_action, quote (message_name));  fflush (stdlis);  if (!confirm_file_EOF)    {      char *response = NULL;      size_t response_size = 0;      if (getline (&response, &response_size, confirm_file) < 0)	confirm_file_EOF = 1;      else	status = rpmatch (response) > 0;      free (response);    }  if (confirm_file_EOF)    {      fputc ('\n', stdlis);      fflush (stdlis);    }  return status;}static struct fmttab {  char const *name;  enum archive_format fmt;} const fmttab[] = {  { "v7",      V7_FORMAT },  { "oldgnu",  OLDGNU_FORMAT },  { "ustar",   USTAR_FORMAT },  { "posix",   POSIX_FORMAT },#if 0 /* not fully supported yet */  { "star",    STAR_FORMAT },#endif  { "gnu",     GNU_FORMAT },  { "pax",     POSIX_FORMAT }, /* An alias for posix */  { NULL,      0 }};static voidset_archive_format (char const *name){  struct fmttab const *p;  for (p = fmttab; strcmp (p->name, name) != 0; )    if (! (++p)->name)      USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),		    quotearg_colon (name)));  archive_format = p->fmt;}const char *archive_format_string (enum archive_format fmt){  struct fmttab const *p;  for (p = fmttab; p->name; p++)    if (p->fmt == fmt)      return p->name;  return "unknown?";}#define FORMAT_MASK(n) (1<<(n))static voidassert_format(unsigned fmt_mask){  if ((FORMAT_MASK (archive_format) & fmt_mask) == 0)    USAGE_ERROR ((0, 0,		  _("GNU features wanted on incompatible archive format")));}const char *subcommand_string (enum subcommand c){  switch (c)    {    case UNKNOWN_SUBCOMMAND:      return "unknown?";    case APPEND_SUBCOMMAND:      return "-r";    case CAT_SUBCOMMAND:      return "-A";    case CREATE_SUBCOMMAND:      return "-c";    case DELETE_SUBCOMMAND:      return "-D";    case DIFF_SUBCOMMAND:      return "-d";    case EXTRACT_SUBCOMMAND:      return "-x";    case LIST_SUBCOMMAND:      return "-t";    case UPDATE_SUBCOMMAND:      return "-u";    default:      abort ();    }}voidtar_list_quoting_styles (argp_fmtstream_t fs, char *prefix){  int i;  for (i = 0; quoting_style_args[i]; i++)    argp_fmtstream_printf (fs, "%s%s\n", prefix, quoting_style_args[i]);}voidtar_set_quoting_style (char *arg){  int i;  for (i = 0; quoting_style_args[i]; i++)    if (strcmp (arg, quoting_style_args[i]) == 0)      {	set_quoting_style (NULL, i);	return;      }  FATAL_ERROR ((0, 0,		_("Unknown quoting style `%s'. Try `%s --quoting-style=help' to get a list."), arg, program_invocation_short_name));}/* Options.  */enum{  ANCHORED_OPTION = CHAR_MAX + 1,  ATIME_PRESERVE_OPTION,  BACKUP_OPTION,  CHECK_DEVICE_OPTION,  CHECKPOINT_OPTION,  CHECKPOINT_ACTION_OPTION,  DELAY_DIRECTORY_RESTORE_OPTION,  HARD_DEREFERENCE_OPTION,  DELETE_OPTION,  EXCLUDE_CACHES_OPTION,  EXCLUDE_CACHES_UNDER_OPTION,  EXCLUDE_CACHES_ALL_OPTION,  EXCLUDE_OPTION,  EXCLUDE_TAG_OPTION,  EXCLUDE_TAG_UNDER_OPTION,  EXCLUDE_TAG_ALL_OPTION,  EXCLUDE_VCS_OPTION,  FORCE_LOCAL_OPTION,  GROUP_OPTION,  HANG_OPTION,  IGNORE_CASE_OPTION,  IGNORE_COMMAND_ERROR_OPTION,  IGNORE_FAILED_READ_OPTION,  INDEX_FILE_OPTION,  KEEP_NEWER_FILES_OPTION,  LZMA_OPTION,  MODE_OPTION,  MTIME_OPTION,  NEWER_MTIME_OPTION,  NO_ANCHORED_OPTION,  NO_CHECK_DEVICE_OPTION,  NO_DELAY_DIRECTORY_RESTORE_OPTION,  NO_IGNORE_CASE_OPTION,  NO_IGNORE_COMMAND_ERROR_OPTION,  NO_OVERWRITE_DIR_OPTION,  NO_QUOTE_CHARS_OPTION,  NO_RECURSION_OPTION,  NO_SAME_OWNER_OPTION,  NO_SAME_PERMISSIONS_OPTION,  NO_UNQUOTE_OPTION,  NO_WILDCARDS_MATCH_SLASH_OPTION,  NO_WILDCARDS_OPTION,  NULL_OPTION,  NUMERIC_OWNER_OPTION,  OCCURRENCE_OPTION,  OLD_ARCHIVE_OPTION,  ONE_FILE_SYSTEM_OPTION,  OVERWRITE_DIR_OPTION,  OVERWRITE_OPTION,  OWNER_OPTION,  PAX_OPTION,  POSIX_OPTION,  PRESERVE_OPTION,  QUOTE_CHARS_OPTION,  QUOTING_STYLE_OPTION,  RECORD_SIZE_OPTION,  RECURSION_OPTION,  RECURSIVE_UNLINK_OPTION,  REMOVE_FILES_OPTION,  RESTRICT_OPTION,  RMT_COMMAND_OPTION,  RSH_COMMAND_OPTION,  SAME_OWNER_OPTION,  SHOW_DEFAULTS_OPTION,  SHOW_OMITTED_DIRS_OPTION,  SHOW_TRANSFORMED_NAMES_OPTION,  SPARSE_VERSION_OPTION,  STRIP_COMPONENTS_OPTION,  SUFFIX_OPTION,  TEST_LABEL_OPTION,  TOTALS_OPTION,  TO_COMMAND_OPTION,  TRANSFORM_OPTION,  UNQUOTE_OPTION,  USAGE_OPTION,  USE_COMPRESS_PROGRAM_OPTION,  UTC_OPTION,  VERSION_OPTION,  VOLNO_FILE_OPTION,  WILDCARDS_MATCH_SLASH_OPTION,  WILDCARDS_OPTION};const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION;const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";static char const doc[] = N_("\GNU `tar' saves many files together into a single tape or disk archive, \and can restore individual files from the archive.\n\\n\Examples:\n\  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.\n\  tar -tvf archive.tar         # List all files in archive.tar verbosely.\n\  tar -xf archive.tar          # Extract all files from archive.tar.\n")"\v"N_("The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\  none, off       never make backups\n\  t, numbered     make numbered backups\n\  nil, existing   numbered if numbered backups exist, simple otherwise\n\  never, simple   always make simple backups\n");/* NOTE:   Available option letters are DEIJQY and eqy. Consider the following   assignments:   [For Solaris tar compatibility =/= Is it important at all?]   e  exit immediately with a nonzero exit status if unexpected errors occur   E  use extended headers (--format=posix)   [q  alias for --occurrence=1 =/= this would better be used for quiet?]   [I  same as T =/= will harm star compatibility]   y  per-file gzip compression   Y  per-block gzip compression */static struct argp_option options[] = {#define GRID 10  {NULL, 0, NULL, 0,   N_("Main operation mode:"), GRID },  {"list", 't', 0, 0,   N_("list the contents of an archive"), GRID+1 },  {"extract", 'x', 0, 0,   N_("extract files from an archive"), GRID+1 },  {"get", 0, 0, OPTION_ALIAS, NULL, GRID+1 },  {"create", 'c', 0, 0,   N_("create a new archive"), GRID+1 },  {"diff", 'd', 0, 0,   N_("find differences between archive and file system"), GRID+1 },  {"compare", 0, 0, OPTION_ALIAS, NULL, GRID+1 },  {"append", 'r', 0, 0,   N_("append files to the end of an archive"), GRID+1 },  {"update", 'u', 0, 0,   N_("only append files newer than copy in archive"), GRID+1 },  {"catenate", 'A', 0, 0,   N_("append tar files to an archive"), GRID+1 },  {"concatenate", 0, 0, OPTION_ALIAS, NULL, GRID+1 },  {"delete", DELETE_OPTION, 0, 0,   N_("delete from the archive (not on mag tapes!)"), GRID+1 },  {"test-label", TEST_LABEL_OPTION, NULL, 0,   N_("test the archive volume label and exit"), GRID+1 },#undef GRID#define GRID 20  {NULL, 0, NULL, 0,   N_("Operation modifiers:"), GRID },  {"sparse", 'S', 0, 0,   N_("handle sparse files efficiently"), GRID+1 },  {"sparse-version", SPARSE_VERSION_OPTION, N_("MAJOR[.MINOR]"), 0,   N_("set version of the sparse format to use (implies --sparse)"), GRID+1},  {"incremental", 'G', 0, 0,   N_("handle old GNU-format incremental backup"), GRID+1 },  {"listed-incremental", 'g', N_("FILE"), 0,   N_("handle new GNU-format incremental backup"), GRID+1 },  {"ignore-failed-read", IGNORE_FAILED_READ_OPTION, 0, 0,   N_("do not exit with nonzero on unreadable files"), GRID+1 },  {"occurrence", OCCURRENCE_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,   N_("process only the NUMBERth occurrence of each file in the archive;"      " this option is valid only in conjunction with one of the subcommands"      " --delete, --diff, --extract or --list and when a list of files"      " is given either on the command line or via the -T option;"      " NUMBER defaults to 1"), GRID+1 },  {"seek", 'n', NULL, 0,   N_("archive is seekable"), GRID+1 },  {"no-check-device", NO_CHECK_DEVICE_OPTION, NULL, 0,   N_("do not check device numbers when creating incremental archives"),   GRID+1 },  {"check-device", CHECK_DEVICE_OPTION, NULL, 0,   N_("check device numbers when creating incremental archives (default)"),   GRID+1 },#undef GRID#define GRID 30  {NULL, 0, NULL, 0,   N_("Overwrite control:"), GRID },  {"verify", 'W', 0, 0,   N_("attempt to verify the archive after writing it"), GRID+1 },  {"remove-files", REMOVE_FILES_OPTION, 0, 0,   N_("remove files after adding them to the archive"), GRID+1 },  {"keep-old-files", 'k', 0, 0,   N_("don't replace existing files when extracting"), GRID+1 },  {"keep-newer-files", KEEP_NEWER_FILES_OPTION, 0, 0,   N_("don't replace existing files that are newer than their archive copies"), GRID+1 },  {"overwrite", OVERWRITE_OPTION, 0, 0,   N_("overwrite existing files when extracting"), GRID+1 },  {"unlink-first", 'U', 0, 0,   N_("remove each file prior to extracting over it"), GRID+1 },  {"recursive-unlink", RECURSIVE_UNLINK_OPTION, 0, 0,   N_("empty hierarchies prior to extracting directory"), GRID+1 },  {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION, 0, 0,   N_("preserve metadata of existing directories"), GRID+1 },  {"overwrite-dir", OVERWRITE_DIR_OPTION, 0, 0,   N_("overwrite metadata of existing directories when extracting (default)"),   GRID+1 },#undef GRID#define GRID 40  {NULL, 0, NULL, 0,   N_("Select output stream:"), GRID },  {"to-stdout", 'O', 0, 0,   N_("extract files to standard output"), GRID+1 },  {"to-command", TO_COMMAND_OPTION, N_("COMMAND"), 0,   N_("pipe extracted files to another program"), GRID+1 },  {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION, 0, 0,   N_("ignore exit codes of children"), GRID+1 },  {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION, 0, 0,   N_("treat non-zero exit codes of children as error"), GRID+1 },#undef GRID#define GRID 50  {NULL, 0, NULL, 0,   N_("Handling of file attributes:"), GRID },  {"owner", OWNER_OPTION, N_("NAME"), 0,   N_("force NAME as owner for added files"), GRID+1 },  {"group", GROUP_OPTION, N_("NAME"), 0,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -