Project/Real_code/gnu/coreutils-5.0/src/ls.c
static void
print_dir (const char *name, const char *realname)
{
  register DIR *dirp;
  register struct dirent *next;
  register uintmax_t total_blocks = 0;
  static int first = 1;

  errno = 0;
  dirp = opendir (name);
  if (!dirp)
    {
      error (0, errno, "%s", quotearg_colon (name));
      exit_status = 1;
      return;
    }

  if (LOOP_DETECT)
    {
      struct stat dir_stat;
      int fd = dirfd (dirp);

      /* If dirfd failed, endure the overhead of using stat.  */
      if ((0 <= fd
	   ? fstat (fd, &dir_stat)
	   : stat (name, &dir_stat)) < 0)
	{
	  error (0, errno, _("cannot determine device and inode of %s"),
		 quotearg_colon (name));
	  exit_status = 1;
	  return;
	}

      /* If we've already visited this dev/inode pair, warn that
	 we've found a loop, and do not process this directory.  */
      if (visit_dir (dir_stat.st_dev, dir_stat.st_ino))
	{
	  error (0, 0, _("not listing already-listed directory: %s"),
		 quotearg_colon (name));
	  return;
	}

      DEV_INO_PUSH (dir_stat.st_dev, dir_stat.st_ino);
    }

  /* Read the directory entries, and insert the subfiles into the `files'
     table.  */

  clear_files ();

  while (1)
    {
      /* Set errno to zero so we can distinguish between a readdir failure
	 and when readdir simply finds that there are no more entries.  */
      errno = 0;
      if ((next = readdir (dirp)) == NULL)
	{
	  if (errno)
	    {
	      /* Save/restore errno across closedir call.  */
	      int e = errno;
	      closedir (dirp);
	      errno = e;

	      /* Arrange to give a diagnostic after exiting this loop.  */
	      dirp = NULL;
	    }
	  break;
	}

      if (file_interesting (next))
	{
	  enum filetype type = unknown;

#if HAVE_STRUCT_DIRENT_D_TYPE
	  if (next->d_type == DT_BLK
	      || next->d_type == DT_CHR
	      || next->d_type == DT_DIR
	      || next->d_type == DT_FIFO
	      || next->d_type == DT_LNK
	      || next->d_type == DT_REG
	      || next->d_type == DT_SOCK)
	    type = next->d_type;
#endif
	  total_blocks += gobble_file (next->d_name, type, 0, name);
	}
    }

  if (dirp == NULL || CLOSEDIR (dirp))
    {
      error (0, errno, _("reading directory %s"), quotearg_colon (name));
      exit_status = 1;
      /* Don't return; print whatever we got. */
    }

  /* Sort the directory contents.  */
  sort_files ();

  /* If any member files are subdirectories, perhaps they should have their
     contents listed rather than being mentioned here as files.  */

  if (recursive)
    extract_dirs_from_files (name, 1);

  if (recursive || print_dir_name)
    {
      if (!first)
	DIRED_PUTCHAR ('\n');
      first = 0;
      DIRED_INDENT ();
      PUSH_CURRENT_DIRED_POS (&subdired_obstack);
      dired_pos += quote_name (stdout, realname ? realname : name,
			       dirname_quoting_options, NULL);
      PUSH_CURRENT_DIRED_POS (&subdired_obstack);
      DIRED_FPUTS_LITERAL (":\n", stdout);
    }

  if (format == long_format || print_block_size)
    {
      const char *p;
      char buf[LONGEST_HUMAN_READABLE + 1];

      DIRED_INDENT ();
      p = _("total");
      DIRED_FPUTS (p, stdout, strlen (p));
      DIRED_PUTCHAR (' ');
      p = human_readable (total_blocks, buf, human_output_opts,
			  ST_NBLOCKSIZE, output_block_size);
      DIRED_FPUTS (p, stdout, strlen (p));
      DIRED_PUTCHAR ('\n');
    }

  if (files_index)
    print_current_files ();
}
/* Read directory `name', and list the files in it.
   If `realname' is nonzero, print its name instead of `name';
   this is used for symbolic links to directories. */
Read directory `name', and list the files in it.
If `realname' is nonzero, print its name instead of `name';
this is used for symbolic links to directories.
Read directory ` name ' and list the files in it . If ` realname ' is nonzero print its name instead of ` name ' ; this is used for symbolic links to directories .
static void
print_dir (const char *name, const char *realname)
{
  register DIR *dirp;
  register struct dirent *next;
  register uintmax_t total_blocks = 0;
  static int first = 1;
  errno = 0;
  dirp = opendir (name);
  if (!dirp)
    {
      error (0, errno, "%s", quotearg_colon (name));
      exit_status = 1;
      return;
    }
  if (LOOP_DETECT)
    {
      struct stat dir_stat;
      int fd = dirfd (dirp);
      if ((0 <= fd
	   ? fstat (fd, &dir_stat)
	   : stat (name, &dir_stat)) < 0)
	{
	  error (0, errno, _("cannot determine device and inode of %s"),
		 quotearg_colon (name));
	  exit_status = 1;
	  return;
	}
      if (visit_dir (dir_stat.st_dev, dir_stat.st_ino))
	{
	  error (0, 0, _("not listing already-listed directory: %s"),
		 quotearg_colon (name));
	  return;
	}
      DEV_INO_PUSH (dir_stat.st_dev, dir_stat.st_ino);
    }
  clear_files ();
  while (1)
    {
      errno = 0;
      if ((next = readdir (dirp)) == NULL)
	{
	  if (errno)
	    {
	      int e = errno;
	      closedir (dirp);
	      errno = e;
	      dirp = NULL;
	    }
	  break;
	}
      if (file_interesting (next))
	{
	  enum filetype type = unknown;
#if HAVE_STRUCT_DIRENT_D_TYPE
	  if (next->d_type == DT_BLK
	      || next->d_type == DT_CHR
	      || next->d_type == DT_DIR
	      || next->d_type == DT_FIFO
	      || next->d_type == DT_LNK
	      || next->d_type == DT_REG
	      || next->d_type == DT_SOCK)
	    type = next->d_type;
#endif
	  total_blocks += gobble_file (next->d_name, type, 0, name);
	}
    }
  if (dirp == NULL || CLOSEDIR (dirp))
    {
      error (0, errno, _("reading directory %s"), quotearg_colon (name));
      exit_status = 1;
    }
  sort_files ();
  if (recursive)
    extract_dirs_from_files (name, 1);
  if (recursive || print_dir_name)
    {
      if (!first)
	DIRED_PUTCHAR ('\n');
      first = 0;
      DIRED_INDENT ();
      PUSH_CURRENT_DIRED_POS (&subdired_obstack);
      dired_pos += quote_name (stdout, realname ? realname : name,
			       dirname_quoting_options, NULL);
      PUSH_CURRENT_DIRED_POS (&subdired_obstack);
      DIRED_FPUTS_LITERAL (":\n", stdout);
    }
  if (format == long_format || print_block_size)
    {
      const char *p;
      char buf[LONGEST_HUMAN_READABLE + 1];
      DIRED_INDENT ();
      p = _("total");
      DIRED_FPUTS (p, stdout, strlen (p));
      DIRED_PUTCHAR (' ');
      p = human_readable (total_blocks, buf, human_output_opts,
			  ST_NBLOCKSIZE, output_block_size);
      DIRED_FPUTS (p, stdout, strlen (p));
      DIRED_PUTCHAR ('\n');
    }
  if (files_index)
    print_current_files ();
}
static void print_dir ( const char * name , const char * realname ) { register DIR * dirp ; register struct dirent * next ; register uintmax_t total_blocks = 0 ; static int first = 1 ; errno = 0 ; dirp = opendir ( name ) ; if ( ! dirp ) { error ( 0 , errno , " " , quotearg_colon ( name ) ) ; exit_status = 1 ; return ; } if ( LOOP_DETECT ) { struct stat dir_stat ; int fd = dirfd ( dirp ) ; if ( ( 0 <= fd ? fstat ( fd , & dir_stat ) : stat ( name , & dir_stat ) ) < 0 ) { error ( 0 , errno , _ ( " " ) , quotearg_colon ( name ) ) ; exit_status = 1 ; return ; } if ( visit_dir ( dir_stat . st_dev , dir_stat . st_ino ) ) { error ( 0 , 0 , _ ( " " ) , quotearg_colon ( name ) ) ; return ; } DEV_INO_PUSH ( dir_stat . st_dev , dir_stat . st_ino ) ; } clear_files ( ) ; while ( 1 ) { errno = 0 ; if ( ( next = readdir ( dirp ) ) == NULL ) { if ( errno ) { int e = errno ; closedir ( dirp ) ; errno = e ; dirp = NULL ; } break ; } if ( file_interesting ( next ) ) { enum filetype type = unknown ; #if HAVE_STRUCT_DIRENT_D_TYPE if ( next -> d_type == DT_BLK || next -> d_type == DT_CHR || next -> d_type == DT_DIR || next -> d_type == DT_FIFO || next -> d_type == DT_LNK || next -> d_type == DT_REG || next -> d_type == DT_SOCK ) type = next -> d_type ; #endif total_blocks += gobble_file ( next -> d_name , type , 0 , name ) ; } } if ( dirp == NULL || CLOSEDIR ( dirp ) ) { error ( 0 , errno , _ ( " " ) , quotearg_colon ( name ) ) ; exit_status = 1 ; } sort_files ( ) ; if ( recursive ) extract_dirs_from_files ( name , 1 ) ; if ( recursive || print_dir_name ) { if ( ! first ) DIRED_PUTCHAR ( ' \n ' ) ; first = 0 ; DIRED_INDENT ( ) ; PUSH_CURRENT_DIRED_POS ( & subdired_obstack ) ; dired_pos += quote_name ( stdout , realname ? realname : name , dirname_quoting_options , NULL ) ; PUSH_CURRENT_DIRED_POS ( & subdired_obstack ) ; DIRED_FPUTS_LITERAL ( " \n " , stdout ) ; } if ( format == long_format || print_block_size ) { const char * p ; char buf [ LONGEST_HUMAN_READABLE + 1 ] ; DIRED_INDENT ( ) ; p = _ ( " " ) ; DIRED_FPUTS ( p , stdout , strlen ( p ) ) ; DIRED_PUTCHAR ( ' ' ) ; p = human_readable ( total_blocks , buf , human_output_opts , ST_NBLOCKSIZE , output_block_size ) ; DIRED_FPUTS ( p , stdout , strlen ( p ) ) ; DIRED_PUTCHAR ( ' \n ' ) ; } if ( files_index ) print_current_files ( ) ; }
Read directory `name', and list the files in it.
Read directory ` name ' and list the files in it .
/* If dirfd failed, endure the overhead of using stat.  */ /* If we've already visited this dev/inode pair, warn that
	 we've found a loop, and do not process this directory.  */ /* Read the directory entries, and insert the subfiles into the `files'
     table.  */ /* Set errno to zero so we can distinguish between a readdir failure
	 and when readdir simply finds that there are no more entries.  */ /* Save/restore errno across closedir call.  */ /* Arrange to give a diagnostic after exiting this loop.  */ /* Don't return; print whatever we got. */ /* Sort the directory contents.  */ /* If any member files are subdirectories, perhaps they should have their
     contents listed rather than being mentioned here as files.  */