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

📄 ls.c

📁 Linux.Programming.by example 的源代码绝对经典
💻 C
📖 第 1 页 / 共 5 页
字号:
static int print_with_color;enum color_type  {    color_never,		/* 0: default or --color=never */    color_always,		/* 1: --color=always */    color_if_tty		/* 2: --color=tty */  };enum Dereference_symlink  {    DEREF_UNDEFINED = 1,    DEREF_NEVER,    DEREF_COMMAND_LINE_ARGUMENTS,	/* -H */    DEREF_COMMAND_LINE_SYMLINK_TO_DIR,	/* the default, in certain cases */    DEREF_ALWAYS			/* -L */  };enum indicator_no  {    C_LEFT, C_RIGHT, C_END, C_NORM, C_FILE, C_DIR, C_LINK, C_FIFO, C_SOCK,    C_BLK, C_CHR, C_MISSING, C_ORPHAN, C_EXEC, C_DOOR  };static const char *const indicator_name[]=  {    "lc", "rc", "ec", "no", "fi", "di", "ln", "pi", "so",    "bd", "cd", "mi", "or", "ex", "do", NULL  };struct color_ext_type  {    struct bin_str ext;		/* The extension we're looking for */    struct bin_str seq;		/* The sequence to output when we do */    struct color_ext_type *next;	/* Next in list */  };static struct bin_str color_indicator[] =  {    { LEN_STR_PAIR ("\033[") },		/* lc: Left of color sequence */    { LEN_STR_PAIR ("m") },		/* rc: Right of color sequence */    { 0, NULL },			/* ec: End color (replaces lc+no+rc) */    { LEN_STR_PAIR ("0") },		/* no: Normal */    { LEN_STR_PAIR ("0") },		/* fi: File: default */    { LEN_STR_PAIR ("01;34") },		/* di: Directory: bright blue */    { LEN_STR_PAIR ("01;36") },		/* ln: Symlink: bright cyan */    { LEN_STR_PAIR ("33") },		/* pi: Pipe: yellow/brown */    { LEN_STR_PAIR ("01;35") },		/* so: Socket: bright magenta */    { LEN_STR_PAIR ("01;33") },		/* bd: Block device: bright yellow */    { LEN_STR_PAIR ("01;33") },		/* cd: Char device: bright yellow */    { 0, NULL },			/* mi: Missing file: undefined */    { 0, NULL },			/* or: Orphanned symlink: undefined */    { LEN_STR_PAIR ("01;32") },		/* ex: Executable: bright green */    { LEN_STR_PAIR ("01;35") }		/* do: Door: bright magenta */  };/* FIXME: comment  */static struct color_ext_type *color_ext_list = NULL;/* Buffer for color sequences */static char *color_buf;/* Nonzero means to check for orphaned symbolic link, for displaying   colors.  */static int check_symlink_color;/* Nonzero means mention the inode number of each file.  -i  */static int print_inode;/* What to do with symbolic links.  Affected by -d, -F, -H, -l (and   other options that imply -l), and -L.  */static enum Dereference_symlink dereference;/* Nonzero means when a directory is found, display info on its   contents.  -R  */static int recursive;/* Nonzero means when an argument is a directory name, display info   on it itself.  -d  */static int immediate_dirs;/* Nonzero means don't omit files whose names start with `.'.  -A */static int all_files;/* Nonzero means don't omit files `.' and `..'   This flag implies `all_files'.  -a  */static int really_all_files;/* A linked list of shell-style globbing patterns.  If a non-argument   file name matches any of these patterns, it is omitted.   Controlled by -I.  Multiple -I options accumulate.   The -B option adds `*~' and `.*~' to this list.  */struct ignore_pattern  {    const char *pattern;    struct ignore_pattern *next;  };static struct ignore_pattern *ignore_patterns;/* Nonzero means output nongraphic chars in file names as `?'.   (-q, --hide-control-chars)   qmark_funny_chars and the quoting style (-Q, --quoting-style=WORD) are   independent.  The algorithm is: first, obey the quoting style to get a   string representing the file name;  then, if qmark_funny_chars is set,   replace all nonprintable chars in that string with `?'.  It's necessary   to replace nonprintable chars even in quoted strings, because we don't   want to mess up the terminal if control chars get sent to it, and some   quoting methods pass through control chars as-is.  */static int qmark_funny_chars;/* Quoting options for file and dir name output.  */static struct quoting_options *filename_quoting_options;static struct quoting_options *dirname_quoting_options;/* The number of chars per hardware tab stop.  Setting this to zero   inhibits the use of TAB characters for separating columns.  -T */static int tabsize;/* Nonzero means we are listing the working directory because no   non-option arguments were given. */static int dir_defaulted;/* Nonzero means print each directory name before listing it. */static int print_dir_name;/* The line length to use for breaking lines in many-per-line format.   Can be set with -w.  */static int line_length;/* If nonzero, the file listing format requires that stat be called on   each file. */static int format_needs_stat;/* Similar to `format_needs_stat', but set if only the file type is   needed.  */static int format_needs_type;/* strftime formats for non-recent and recent files, respectively, in   -l output.  */static char const *long_time_format[2] =  {    /* strftime format for non-recent files (older than 6 months), in       -l output when --time-style=locale is specified.  This should       contain the year, month and day (at least), in an order that is       understood by people in your locale's territory.       Please try to keep the number of used screen columns small,       because many people work in windows with only 80 columns.  But       make this as wide as the other string below, for recent files.  */    N_("%b %e  %Y"),    /* strftime format for recent files (younger than 6 months), in       -l output when --time-style=locale is specified.  This should       contain the month, day and time (at least), in an order that is       understood by people in your locale's territory.       Please try to keep the number of used screen columns small,       because many people work in windows with only 80 columns.  But       make this as wide as the other string above, for non-recent files.  */    N_("%b %e %H:%M")  };/* The exit status to use if we don't get any fatal errors. */static int exit_status;/* For long options that have no equivalent short option, use a   non-character as a pseudo short option, starting with CHAR_MAX + 1.  */enum{  AUTHOR_OPTION = CHAR_MAX + 1,  BLOCK_SIZE_OPTION,  COLOR_OPTION,  DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION,  FORMAT_OPTION,  FULL_TIME_OPTION,  INDICATOR_STYLE_OPTION,  QUOTING_STYLE_OPTION,  SHOW_CONTROL_CHARS_OPTION,  SI_OPTION,  SORT_OPTION,  TIME_OPTION,  TIME_STYLE_OPTION};static struct option const long_options[] ={  {"all", no_argument, 0, 'a'},  {"escape", no_argument, 0, 'b'},  {"directory", no_argument, 0, 'd'},  {"dired", no_argument, 0, 'D'},  {"full-time", no_argument, 0, FULL_TIME_OPTION},  {"human-readable", no_argument, 0, 'h'},  {"inode", no_argument, 0, 'i'},  {"kilobytes", no_argument, 0, 'k'}, /* long form is obsolescent */  {"numeric-uid-gid", no_argument, 0, 'n'},  {"no-group", no_argument, 0, 'G'},  {"hide-control-chars", no_argument, 0, 'q'},  {"reverse", no_argument, 0, 'r'},  {"size", no_argument, 0, 's'},  {"width", required_argument, 0, 'w'},  {"almost-all", no_argument, 0, 'A'},  {"ignore-backups", no_argument, 0, 'B'},  {"classify", no_argument, 0, 'F'},  {"file-type", no_argument, 0, 'p'},  {"si", no_argument, 0, SI_OPTION},  {"dereference-command-line", no_argument, 0, 'H'},  {"dereference-command-line-symlink-to-dir", no_argument, 0,   DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION},  {"ignore", required_argument, 0, 'I'},  {"indicator-style", required_argument, 0, INDICATOR_STYLE_OPTION},  {"dereference", no_argument, 0, 'L'},  {"literal", no_argument, 0, 'N'},  {"quote-name", no_argument, 0, 'Q'},  {"quoting-style", required_argument, 0, QUOTING_STYLE_OPTION},  {"recursive", no_argument, 0, 'R'},  {"format", required_argument, 0, FORMAT_OPTION},  {"show-control-chars", no_argument, 0, SHOW_CONTROL_CHARS_OPTION},  {"sort", required_argument, 0, SORT_OPTION},  {"tabsize", required_argument, 0, 'T'},  {"time", required_argument, 0, TIME_OPTION},  {"time-style", required_argument, 0, TIME_STYLE_OPTION},  {"color", optional_argument, 0, COLOR_OPTION},  {"block-size", required_argument, 0, BLOCK_SIZE_OPTION},  {"author", no_argument, 0, AUTHOR_OPTION},  {GETOPT_HELP_OPTION_DECL},  {GETOPT_VERSION_OPTION_DECL},  {NULL, 0, NULL, 0}};static char const *const format_args[] ={  "verbose", "long", "commas", "horizontal", "across",  "vertical", "single-column", 0};static enum format const format_types[] ={  long_format, long_format, with_commas, horizontal, horizontal,  many_per_line, one_per_line};static char const *const sort_args[] ={  "none", "time", "size", "extension", "version", 0};static enum sort_type const sort_types[] ={  sort_none, sort_time, sort_size, sort_extension, sort_version};static char const *const time_args[] ={  "atime", "access", "use", "ctime", "status", 0};static enum time_type const time_types[] ={  time_atime, time_atime, time_atime, time_ctime, time_ctime};static char const *const color_args[] ={  /* force and none are for compatibility with another color-ls version */  "always", "yes", "force",  "never", "no", "none",  "auto", "tty", "if-tty", 0};static enum color_type const color_types[] ={  color_always, color_always, color_always,  color_never, color_never, color_never,  color_if_tty, color_if_tty, color_if_tty};/* Information about filling a column.  */struct column_info{  int valid_len;  int line_len;  int *col_arr;};/* Array with information about column filledness.  */static struct column_info *column_info;/* Maximum number of columns ever possible for this display.  */static int max_idx;/* The minimum width of a colum is 3: 1 character for the name and 2   for the separating white space.  */#define MIN_COLUMN_WIDTH	3/* This zero-based index is used solely with the --dired option.   When that option is in effect, this counter is incremented for each   character of output generated by this program so that the beginning   and ending indices (in that output) of every file name can be recorded   and later output themselves.  */static size_t dired_pos;#define DIRED_PUTCHAR(c) do {putchar ((c)); ++dired_pos;} while (0)/* Write S to STREAM and increment DIRED_POS by S_LEN.  */#define DIRED_FPUTS(s, stream, s_len) \    do {fputs ((s), (stream)); dired_pos += s_len;} while (0)/* Like DIRED_FPUTS, but for use when S is a literal string.  */#define DIRED_FPUTS_LITERAL(s, stream) \    do {fputs ((s), (stream)); dired_pos += sizeof((s)) - 1;} while (0)#define DIRED_INDENT()							\    do									\      {									\	if (dired)							\	  DIRED_FPUTS_LITERAL ("  ", stdout);				\      }									\    while (0)/* With --dired, store pairs of beginning and ending indices of filenames.  */static struct obstack dired_obstack;/* With --dired, store pairs of beginning and ending indices of any   directory names that appear as headers (just before `total' line)   for lists of directory entries.  Such directory names are seen when   listing hierarchies using -R and when a directory is listed with at   least one other command line argument.  */static struct obstack subdired_obstack;/* Save the current index on the specified obstack, OBS.  */#define PUSH_CURRENT_DIRED_POS(obs)					\  do									\    {									\      if (dired)							\	obstack_grow ((obs), &dired_pos, sizeof (dired_pos));		\    }									\  while (0)/* With -R, this stack is used to help detect directory cycles.   The device/inode pairs on this stack mirror the pairs in the   active_dir_set hash table.  */static struct obstack dev_ino_obstack;/* Push a pair onto the device/inode stack.  */#define DEV_INO_PUSH(Dev, Ino)						\  do									\    {									\      struct dev_ino *di;						\      obstack_blank (&dev_ino_obstack, sizeof (struct dev_ino));	\      di = -1 + (struct dev_ino *) obstack_next_free (&dev_ino_obstack); \      di->st_dev = (Dev);						\      di->st_ino = (Ino);						\    }									\  while (0)/* Pop a dev/ino struct off the global dev_ino_obstack   and return that struct.  */static struct dev_inodev_ino_pop (void){  assert (sizeof (struct dev_ino) <= obstack_object_size (&dev_ino_obstack));  obstack_blank (&dev_ino_obstack, -(int) (sizeof (struct dev_ino)));  return *(struct dev_ino*) obstack_next_free (&dev_ino_obstack);}#define ASSERT_MATCHING_DEV_INO(Name, Di)	\  do						\    {						\      struct stat sb;				\      assert (Name);				\      assert (0 <= stat (Name, &sb));		\      assert (sb.st_dev == Di.st_dev);		\      assert (sb.st_ino == Di.st_ino);		\    }						\  while (0)/* Write to standard output PREFIX, followed by the quoting style and   a space-separated list of the integers stored in OS all on one line.  */static voiddired_dump_obstack (const char *prefix, struct obstack *os){  int n_pos;  n_pos = obstack_object_size (os) / sizeof (dired_pos);  if (n_pos > 0)    {      int i;      size_t *pos;      pos = (size_t *) obstack_finish (os);      fputs (prefix, stdout);      for (i = 0; i < n_pos; i++)	printf (" %lu", (unsigned long) pos[i]);      putchar ('\n');    }}static unsigned intdev_ino_hash (void const *x, unsigned int table_size){  struct dev_ino const *p = x;  return (uintmax_t) p->st_ino % table_size;}static booldev_ino_compare (void const *x, void const *y){  struct dev_ino const *a = x;  struct dev_ino const *b = y;  return SAME_INODE (*a, *b) ? true : false;}static voiddev_ino_free (void *x){  free (x);}/* Add the device/inode pair (P->st_dev/P->st_ino) to the set of   active directories.  Return nonzero if there is already a matching   entry in the table.  Otherwise, return zero.  */static intvisit_dir (dev_t dev, ino_t ino){  struct dev_ino *ent;  struct dev_ino *ent_from_table;  int found_match;  ent = XMALLOC (struct dev_ino, 1);  ent->st_ino = ino;  ent->st_dev = dev;  /* Attempt to insert this entry into the table.  */  ent_from_table = hash_insert (active_dir_set, ent);  if (ent_from_table == NULL)    {      /* Insertion failed due to lack of memory.  */      xalloc_die ();    }  found_match = (ent_from_table != ent);  if (found_match)    {      /* ent was not inserted, so free it.  */      free (ent);    }  return found_match;}static voidfree_pending_ent (struct pending *p){  if (p->name)    free (p->name);  if (p->realname)    free (p->realname);  free (p);}static voidrestore_default_color (void){  if (put_indicator_direct (&color_indicator[C_LEFT]) == 0)    put_indicator_direct (&color_indicator[C_RIGHT]);}/* Upon interrupt, suspend, hangup, etc. ensure that the   terminal text color is restored to the default.  */static voidsighandler (int sig){#ifndef SA_NOCLDSTOP  signal (sig, SIG_IGN);#endif  restore_default_color ();  /* SIGTSTP is special, since the application can receive that signal more     than once.  In this case, don't set the signal handler to the default.     Instead, just raise the uncatchable SIGSTOP.  */  if (sig == SIGTSTP)    {      sig = SIGSTOP;    }  else

⌨️ 快捷键说明

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