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

📄 general.c

📁 android-w.song.android.widget
💻 C
📖 第 1 页 / 共 2 页
字号:
/* general.c -- Stuff that is used by all files. *//* Copyright (C) 1987-2009 Free Software Foundation, Inc.   This file is part of GNU Bash, the Bourne Again SHell.   Bash 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 of the License, or   (at your option) any later version.   Bash 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 Bash.  If not, see <http://www.gnu.org/licenses/>.*/#include "config.h"#include "bashtypes.h"#ifndef _MINIX#  include <sys/param.h>#endif#include "posixstat.h"#if defined (HAVE_UNISTD_H)#  include <unistd.h>#endif#include "filecntl.h"#include "bashansi.h"#include <stdio.h>#include "chartypes.h"#include <errno.h>#include "bashintl.h"#include "shell.h"#include "test.h"#include <tilde/tilde.h>#if !defined (errno)extern int errno;#endif /* !errno */extern int expand_aliases;extern int interactive_comments;extern int check_hashed_filenames;extern int source_uses_path;extern int source_searches_cwd;static char *bash_special_tilde_expansions __P((char *));static int unquoted_tilde_word __P((const char *));static void initialize_group_array __P((void));/* A standard error message to use when getcwd() returns NULL. */const char * const bash_getcwd_errstr = N_("getcwd: cannot access parent directories");/* Do whatever is necessary to initialize `Posix mode'. */voidposix_initialize (on)     int on;{  /* Things that should be turned on when posix mode is enabled. */  if (on != 0)    {      interactive_comments = source_uses_path = expand_aliases = 1;      source_searches_cwd = 0;    }  /* Things that should be turned on when posix mode is disabled. */  if (on == 0)    {      source_searches_cwd = 1;      expand_aliases = interactive_shell;    }}/* **************************************************************** *//*								    *//*  Functions to convert to and from and display non-standard types *//*								    *//* **************************************************************** */#if defined (RLIMTYPE)RLIMTYPEstring_to_rlimtype (s)     char *s;{  RLIMTYPE ret;  int neg;  ret = 0;  neg = 0;  while (s && *s && whitespace (*s))    s++;  if (s && (*s == '-' || *s == '+'))    {      neg = *s == '-';      s++;    }  for ( ; s && *s && DIGIT (*s); s++)    ret = (ret * 10) + TODIGIT (*s);  return (neg ? -ret : ret);}voidprint_rlimtype (n, addnl)     RLIMTYPE n;     int addnl;{  char s[INT_STRLEN_BOUND (RLIMTYPE) + 1], *p;  p = s + sizeof(s);  *--p = '\0';  if (n < 0)    {      do	*--p = '0' - n % 10;      while ((n /= 10) != 0);      *--p = '-';    }  else    {      do	*--p = '0' + n % 10;      while ((n /= 10) != 0);    }  printf ("%s%s", p, addnl ? "\n" : "");}#endif /* RLIMTYPE *//* **************************************************************** *//*								    *//*		       Input Validation Functions		    *//*								    *//* **************************************************************** *//* Return non-zero if all of the characters in STRING are digits. */intall_digits (string)     char *string;{  register char *s;  for (s = string; *s; s++)    if (DIGIT (*s) == 0)      return (0);  return (1);}/* Return non-zero if the characters pointed to by STRING constitute a   valid number.  Stuff the converted number into RESULT if RESULT is   not null. */intlegal_number (string, result)     const char *string;     intmax_t *result;{  intmax_t value;  char *ep;  if (result)    *result = 0;  errno = 0;  value = strtoimax (string, &ep, 10);  if (errno || ep == string)    return 0;	/* errno is set on overflow or underflow */  /* Skip any trailing whitespace, since strtoimax does not. */  while (whitespace (*ep))    ep++;  /* If *string is not '\0' but *ep is '\0' on return, the entire string     is valid. */  if (string && *string && *ep == '\0')    {      if (result)	*result = value;      /* The SunOS4 implementation of strtol() will happily ignore	 overflow conditions, so this cannot do overflow correctly	 on those systems. */      return 1;    }      return (0);}/* Return 1 if this token is a legal shell `identifier'; that is, it consists   solely of letters, digits, and underscores, and does not begin with a   digit. */intlegal_identifier (name)     char *name;{  register char *s;  unsigned char c;  if (!name || !(c = *name) || (legal_variable_starter (c) == 0))    return (0);  for (s = name + 1; (c = *s) != 0; s++)    {      if (legal_variable_char (c) == 0)	return (0);    }  return (1);}/* Make sure that WORD is a valid shell identifier, i.e.   does not contain a dollar sign, nor is quoted in any way.  Nor   does it consist of all digits.  If CHECK_WORD is non-zero,   the word is checked to ensure that it consists of only letters,   digits, and underscores. */intcheck_identifier (word, check_word)     WORD_DESC *word;     int check_word;{  if ((word->flags & (W_HASDOLLAR|W_QUOTED)) || all_digits (word->word))    {      internal_error (_("`%s': not a valid identifier"), word->word);      return (0);    }  else if (check_word && legal_identifier (word->word) == 0)    {      internal_error (_("`%s': not a valid identifier"), word->word);      return (0);    }  else    return (1);}/* Return 1 if STRING comprises a valid alias name.  The shell accepts   essentially all characters except those which must be quoted to the   parser (which disqualifies them from alias expansion anyway) and `/'. */intlegal_alias_name (string, flags)     char *string;     int flags;{  register char *s;  for (s = string; *s; s++)    if (shellbreak (*s) || shellxquote (*s) || shellexp (*s) || (*s == '/'))      return 0;  return 1;}/* Returns non-zero if STRING is an assignment statement.  The returned value   is the index of the `=' sign. */intassignment (string, flags)     const char *string;     int flags;{  register unsigned char c;  register int newi, indx;  c = string[indx = 0];#if defined (ARRAY_VARS)  if ((legal_variable_starter (c) == 0) && (flags == 0 || c != '[')) /* ] */#else  if (legal_variable_starter (c) == 0)#endif    return (0);  while (c = string[indx])    {      /* The following is safe.  Note that '=' at the start of a word	 is not an assignment statement. */      if (c == '=')	return (indx);#if defined (ARRAY_VARS)      if (c == '[')	{	  newi = skipsubscript (string, indx, 0);	  if (string[newi++] != ']')	    return (0);	  if (string[newi] == '+' && string[newi+1] == '=')	    return (newi + 1);	  return ((string[newi] == '=') ? newi : 0);	}#endif /* ARRAY_VARS */      /* Check for `+=' */      if (c == '+' && string[indx+1] == '=')	return (indx + 1);      /* Variable names in assignment statements may contain only letters,	 digits, and `_'. */      if (legal_variable_char (c) == 0)	return (0);      indx++;    }  return (0);}/* **************************************************************** *//*								    *//*	     Functions to manage files and file descriptors	    *//*								    *//* **************************************************************** *//* A function to unset no-delay mode on a file descriptor.  Used in shell.c   to unset it on the fd passed as stdin.  Should be called on stdin if   readline gets an EAGAIN or EWOULDBLOCK when trying to read input. */#if !defined (O_NDELAY)#  if defined (FNDELAY)#    define O_NDELAY FNDELAY#  endif#endif /* O_NDELAY *//* Make sure no-delay mode is not set on file descriptor FD. */intsh_unset_nodelay_mode (fd)     int fd;{  int flags, bflags;  if ((flags = fcntl (fd, F_GETFL, 0)) < 0)    return -1;  bflags = 0;  /* This is defined to O_NDELAY in filecntl.h if O_NONBLOCK is not present     and O_NDELAY is defined. */#ifdef O_NONBLOCK  bflags |= O_NONBLOCK;#endif#ifdef O_NDELAY  bflags |= O_NDELAY;#endif  if (flags & bflags)    {      flags &= ~bflags;      return (fcntl (fd, F_SETFL, flags));    }  return 0;}/* Return 1 if file descriptor FD is valid; 0 otherwise. */intsh_validfd (fd)     int fd;{  return (fcntl (fd, F_GETFD, 0) >= 0);}intfd_ispipe (fd)     int fd;{  errno = 0;  if (lseek ((fd), 0L, SEEK_CUR) < 0)    return (errno == ESPIPE);  return 0;}/* There is a bug in the NeXT 2.1 rlogind that causes opens   of /dev/tty to fail. */#if defined (__BEOS__)/* On BeOS, opening in non-blocking mode exposes a bug in BeOS, so turn it   into a no-op.  This should probably go away in the future. */#  undef O_NONBLOCK#  define O_NONBLOCK 0#endif /* __BEOS__ */voidcheck_dev_tty (){  int tty_fd;  char *tty;  tty_fd = open ("/dev/tty", O_RDWR|O_NONBLOCK);  if (tty_fd < 0)    {      tty = (char *)ttyname (fileno (stdin));      if (tty == 0)	return;      tty_fd = open (tty, O_RDWR|O_NONBLOCK);    }  close (tty_fd);}/* Return 1 if PATH1 and PATH2 are the same file.  This is kind of   expensive.  If non-NULL STP1 and STP2 point to stat structures   corresponding to PATH1 and PATH2, respectively. */intsame_file (path1, path2, stp1, stp2)     char *path1, *path2;     struct stat *stp1, *stp2;{  struct stat st1, st2;  if (stp1 == NULL)    {      if (stat (path1, &st1) != 0)	return (0);      stp1 = &st1;    }  if (stp2 == NULL)    {      if (stat (path2, &st2) != 0)	return (0);      stp2 = &st2;    }  return ((stp1->st_dev == stp2->st_dev) && (stp1->st_ino == stp2->st_ino));}/* Move FD to a number close to the maximum number of file descriptors   allowed in the shell process, to avoid the user stepping on it with   redirection and causing us extra work.  If CHECK_NEW is non-zero,   we check whether or not the file descriptors are in use before   duplicating FD onto them.  MAXFD says where to start checking the   file descriptors.  If it's less than 20, we get the maximum value   available from getdtablesize(2). */intmove_to_high_fd (fd, check_new, maxfd)     int fd, check_new, maxfd;{  int script_fd, nfds, ignore;  if (maxfd < 20)    {      nfds = getdtablesize ();      if (nfds <= 0)	nfds = 20;      if (nfds > HIGH_FD_MAX)	nfds = HIGH_FD_MAX;		/* reasonable maximum */    }  else    nfds = maxfd;  for (nfds--; check_new && nfds > 3; nfds--)    if (fcntl (nfds, F_GETFD, &ignore) == -1)      break;  if (nfds > 3 && fd != nfds && (script_fd = dup2 (fd, nfds)) != -1)    {      if (check_new == 0 || fd != fileno (stderr))	/* don't close stderr */	close (fd);      return (script_fd);    }  /* OK, we didn't find one less than our artificial maximum; return the     original file descriptor. */  return (fd);} /* Return non-zero if the characters from SAMPLE are not all valid   characters to be found in the first line of a shell script.  We   check up to the first newline, or SAMPLE_LEN, whichever comes first.   All of the characters must be printable or whitespace. */intcheck_binary_file (sample, sample_len)     char *sample;     int sample_len;{  register int i;  unsigned char c;  for (i = 0; i < sample_len; i++)    {      c = sample[i];      if (c == '\n')	return (0);      if (c == '\0')	return (1);    }  return (0);}/* **************************************************************** *//*								    *//*		    Functions to manipulate pipes		    *//*								    *//* **************************************************************** */intsh_openpipe (pv)     int *pv;{  int r;  if ((r = pipe (pv)) < 0)    return r;  pv[0] = move_to_high_fd (pv[0], 1, 64);  pv[1] = move_to_high_fd (pv[1], 1, 64);  return 0;  }intsh_closepipe (pv)     int *pv;{  if (pv[0] >= 0)    close (pv[0]);  if (pv[1] >= 0)    close (pv[1]);  pv[0] = pv[1] = -1;  return 0;}/* **************************************************************** *//*								    *//*		    Functions to inspect pathnames		    *//*								    *//* **************************************************************** */intfile_exists (fn)     char *fn;{  struct stat sb;  return (stat (fn, &sb) == 0);}intfile_isdir (fn)     char *fn;{  struct stat sb;  return ((stat (fn, &sb) == 0) && S_ISDIR (sb.st_mode));}intfile_iswdir (fn)     char *fn;{  return (file_isdir (fn) && sh_eaccess (fn, W_OK) == 0);}/* Return 1 if STRING is "." or "..", optionally followed by a directory   separator */intdot_or_dotdot (string)     const char *string;{  if (string == 0 || *string == '\0' || *string != '.')    return (0);  /* string[0] == '.' */  if (PATHSEP(string[1]) || (string[1] == '.' && PATHSEP(string[2])))    return (1);  return (0);}/* Return 1 if STRING contains an absolute pathname, else 0.  Used by `cd'   to decide whether or not to look up a directory name in $CDPATH. */intabsolute_pathname (string)     const char *string;

⌨️ 快捷键说明

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