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

📄 names.c

📁 gnu tar 源码包。 tar 软件是 Unix 系统下的一个打包软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Various processing of names.   Copyright (C) 1988, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2001,   2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.   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 <hash.h>#include <quotearg.h>#include "common.h"/* User and group names.  */struct group *getgrnam ();struct passwd *getpwnam ();#if ! HAVE_DECL_GETPWUIDstruct passwd *getpwuid ();#endif#if ! HAVE_DECL_GETGRGIDstruct group *getgrgid ();#endif/* Make sure you link with the proper libraries if you are running the   Yellow Peril (thanks for the good laugh, Ian J.!), or, euh... NIS.   This code should also be modified for non-UNIX systems to do something   reasonable.  */static char *cached_uname;static char *cached_gname;static uid_t cached_uid;	/* valid only if cached_uname is not empty */static gid_t cached_gid;	/* valid only if cached_gname is not empty *//* These variables are valid only if nonempty.  */static char *cached_no_such_uname;static char *cached_no_such_gname;/* These variables are valid only if nonzero.  It's not worth optimizing   the case for weird systems where 0 is not a valid uid or gid.  */static uid_t cached_no_such_uid;static gid_t cached_no_such_gid;static void register_individual_file (char const *name);/* Given UID, find the corresponding UNAME.  */voiduid_to_uname (uid_t uid, char **uname){  struct passwd *passwd;  if (uid != 0 && uid == cached_no_such_uid)    {      *uname = xstrdup ("");      return;    }  if (!cached_uname || uid != cached_uid)    {      passwd = getpwuid (uid);      if (passwd)	{	  cached_uid = uid;	  assign_string (&cached_uname, passwd->pw_name);	}      else	{	  cached_no_such_uid = uid;	  *uname = xstrdup ("");	  return;	}    }  *uname = xstrdup (cached_uname);}/* Given GID, find the corresponding GNAME.  */voidgid_to_gname (gid_t gid, char **gname){  struct group *group;  if (gid != 0 && gid == cached_no_such_gid)    {      *gname = xstrdup ("");      return;    }  if (!cached_gname || gid != cached_gid)    {      group = getgrgid (gid);      if (group)	{	  cached_gid = gid;	  assign_string (&cached_gname, group->gr_name);	}      else	{	  cached_no_such_gid = gid;	  *gname = xstrdup ("");	  return;	}    }  *gname = xstrdup (cached_gname);}/* Given UNAME, set the corresponding UID and return 1, or else, return 0.  */intuname_to_uid (char const *uname, uid_t *uidp){  struct passwd *passwd;  if (cached_no_such_uname      && strcmp (uname, cached_no_such_uname) == 0)    return 0;  if (!cached_uname      || uname[0] != cached_uname[0]      || strcmp (uname, cached_uname) != 0)    {      passwd = getpwnam (uname);      if (passwd)	{	  cached_uid = passwd->pw_uid;	  assign_string (&cached_uname, passwd->pw_name);	}      else	{	  assign_string (&cached_no_such_uname, uname);	  return 0;	}    }  *uidp = cached_uid;  return 1;}/* Given GNAME, set the corresponding GID and return 1, or else, return 0.  */intgname_to_gid (char const *gname, gid_t *gidp){  struct group *group;  if (cached_no_such_gname      && strcmp (gname, cached_no_such_gname) == 0)    return 0;  if (!cached_gname      || gname[0] != cached_gname[0]      || strcmp (gname, cached_gname) != 0)    {      group = getgrnam (gname);      if (group)	{	  cached_gid = group->gr_gid;	  assign_string (&cached_gname, gname);	}      else	{	  assign_string (&cached_no_such_gname, gname);	  return 0;	}    }  *gidp = cached_gid;  return 1;}/* Names from the command call.  */static struct name *namelist;	/* first name in list, if any */static struct name **nametail = &namelist;	/* end of name list *//* File name arguments are processed in two stages: first a    name_array (see below) is filled, then the names from it   are moved into the namelist.   This awkward process is needed only to implement --same-order option,   which is meant to help process large archives on machines with   limited memory.  With this option on, namelist contains at most one   entry, which diminishes the memory consumption.      However, I very much doubt if we still need this -- Sergey *//* A name_array element contains entries of three types: */#define NELT_NAME  0   /* File name */#define NELT_CHDIR 1   /* Change directory request */#define NELT_FMASK 2   /* Change fnmatch options request */struct name_elt        /* A name_array element. */{  char type;           /* Element type, see NELT_* constants above */  union  {    const char *name;  /* File or directory name */    int matching_flags;/* fnmatch options if type == NELT_FMASK */   } v;};static struct name_elt *name_array;  /* store an array of names */static size_t allocated_names;	 /* how big is the array? */static size_t names;		 /* how many entries does it have? */static size_t name_index;	 /* how many of the entries have we scanned? *//* Check the size of name_array, reallocating it as necessary.  */static voidcheck_name_alloc (){  if (names == allocated_names)    {      if (allocated_names == 0)	allocated_names = 10; /* Set initial allocation */      name_array = x2nrealloc (name_array, &allocated_names,			       sizeof (name_array[0]));    }}/* Add to name_array the file NAME with fnmatch options MATCHING_FLAGS */voidname_add_name (const char *name, int matching_flags){  static int prev_flags = 0; /* FIXME: Or EXCLUDE_ANCHORED? */  struct name_elt *ep;  check_name_alloc ();  ep = &name_array[names++];  if (prev_flags != matching_flags)    {      ep->type = NELT_FMASK;      ep->v.matching_flags = matching_flags;      prev_flags = matching_flags;      check_name_alloc ();      ep = &name_array[names++];    }  ep->type = NELT_NAME;  ep->v.name = name;}/* Add to name_array a chdir request for the directory NAME */voidname_add_dir (const char *name){  struct name_elt *ep;  check_name_alloc ();  ep = &name_array[names++];  ep->type = NELT_CHDIR;  ep->v.name = name;}  /* Names from external name file.  */static char *name_buffer;	/* buffer to hold the current file name */static size_t name_buffer_length; /* allocated length of name_buffer *//* Set up to gather file names for tar.  They can either come from a   file or were saved from decoding arguments.  */voidname_init (void){  name_buffer = xmalloc (NAME_FIELD_SIZE + 2);  name_buffer_length = NAME_FIELD_SIZE;}voidname_term (void){  free (name_buffer);  free (name_array);}static int matching_flags; /* exclude_fnmatch options *//* Get the next NELT_NAME element from name_array.  Result is in   static storage and can't be relied upon across two calls.   If CHANGE_DIRS is true, treat any entries of type NELT_CHDIR as   the request to change to the given directory.  If filename_terminator   is NUL, CHANGE_DIRS is effectively always false.   Entries of type NELT_FMASK cause updates of the matching_flags   value. */struct name_elt *name_next_elt (int change_dirs){  static struct name_elt entry;  const char *source;  char *cursor;  if (filename_terminator == '\0')    change_dirs = 0;  while (name_index != names)    {      struct name_elt *ep;      size_t source_len;            ep = &name_array[name_index++];      if (ep->type == NELT_FMASK)	{	  matching_flags = ep->v.matching_flags;	  continue;	}            source = ep->v.name;      source_len = strlen (source);      if (name_buffer_length < source_len)	{	  do	    {	      name_buffer_length *= 2;	      if (! name_buffer_length)		xalloc_die ();	    }	  while (name_buffer_length < source_len);	  free (name_buffer);	  name_buffer = xmalloc (name_buffer_length + 2);	}      strcpy (name_buffer, source);      /* Zap trailing slashes.  */      cursor = name_buffer + strlen (name_buffer) - 1;      while (cursor > name_buffer && ISSLASH (*cursor))	*cursor-- = '\0';      if (change_dirs && ep->type == NELT_CHDIR)	{	  if (chdir (name_buffer) < 0)	    chdir_fatal (name_buffer);	}      else	{	  if (unquote_option)	    unquote_string (name_buffer);	  if (incremental_option)	    register_individual_file (name_buffer);	  entry.type = ep->type;	  entry.v.name = name_buffer;	  return &entry;	}    }  return NULL;}const char *name_next (int change_dirs){  struct name_elt *nelt = name_next_elt (change_dirs);  return nelt ? nelt->v.name : NULL;}/* Gather names in a list for scanning.  Could hash them later if we   really care.   If the names are already sorted to match the archive, we just read   them one by one.  name_gather reads the first one, and it is called   by name_match as appropriate to read the next ones.  At EOF, the   last name read is just left in the buffer.  This option lets users   of small machines extract an arbitrary number of files by doing   "tar t" and editing down the list of files.  */voidname_gather (void){  /* Buffer able to hold a single name.  */  static struct name *buffer;  static size_t allocated_size;  struct name_elt *ep;  if (same_order_option)    {      static int change_dir;      if (allocated_size == 0)	{	  allocated_size = offsetof (struct name, name) + NAME_FIELD_SIZE + 1;	  buffer = xmalloc (allocated_size);	  /* FIXME: This memset is overkill, and ugly...  */	  memset (buffer, 0, allocated_size);	}            while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)	change_dir = chdir_arg (xstrdup (ep->v.name));      if (ep)	{	  size_t needed_size;	  	  buffer->length = strlen (ep->v.name);	  needed_size = offsetof (struct name, name) + buffer->length + 1;	  if (allocated_size < needed_size)	    {	      do		{		  allocated_size *= 2;		  if (! allocated_size)		    xalloc_die ();		}	      while (allocated_size < needed_size);	      buffer = xrealloc (buffer, allocated_size);	    }	  buffer->change_dir = change_dir;	  strcpy (buffer->name, ep->v.name);	  buffer->next = 0;	  buffer->found_count = 0;	  buffer->matching_flags = matching_flags;	  	  namelist = buffer;	  nametail = &namelist->next;	}      else if (change_dir)	addname (0, change_dir);    }  else    {      /* Non sorted names -- read them all in.  */      int change_dir = 0;      for (;;)	{	  int change_dir0 = change_dir;	  while ((ep = name_next_elt (0)) && ep->type == NELT_CHDIR)	    change_dir = chdir_arg (xstrdup (ep->v.name));	  if (ep)	    addname (ep->v.name, change_dir);	  else	    {	      if (change_dir != change_dir0)		addname (0, change_dir);	      break;	    }	}    }}/*  Add a name to the namelist.  */struct name *addname (char const *string, int change_dir){  size_t length = string ? strlen (string) : 0;  struct name *name = xmalloc (offsetof (struct name, name) + length + 1);  if (string)    strcpy (name->name, string);  else    name->name[0] = 0;  name->next = NULL;  name->length = length;  name->found_count = 0;  name->matching_flags = matching_flags;  name->change_dir = change_dir;  name->dir_contents = NULL;  *nametail = name;  nametail = &name->next;  return name;}/* Find a match for FILE_NAME (whose string length is LENGTH) in the name   list.  */static struct name *namelist_match (char const *file_name, size_t length){  struct name *p;  for (p = namelist; p; p = p->next)    {      if (p->name[0]	  && exclude_fnmatch (p->name, file_name, p->matching_flags))	return p;    }  return NULL;}/* Return true if and only if name FILE_NAME (from an archive) matches any   name from the namelist.  */boolname_match (const char *file_name){  size_t length = strlen (file_name);  while (1)    {      struct name *cursor = namelist;      if (!cursor)	return true;            if (cursor->name[0] == 0)

⌨️ 快捷键说明

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