现在的工作可以说是一行代码也不写,WB 程序员,经历了才会懂。

为了避免手生以及进一步巩固基础知识,决定定期抄一抄代码。

Linux 核心工具 开始,学习如何优雅编程。

pwd.c

#include <config.h>
#include <getopt.h>
#include <stdio.h>
#include <sys/types.h>

#include "system.h"
#include "quote.h"
#include "root-dev-ino.h"
#include "xgetcwd.h"

#define PROGRAM_NAME "pwd"

#define AUTHORS proper_name ("Jim Meyering")

struct file_name
{
  char *buf;
  size_t n_alloc;
  char *start;
};

static struct option const longopts[] = 
{
  {"logical", no_argument, nullptr, 'L'},
  {"physical", no_argument, nullptr, 'P'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {nullptr, 0, nullptr, 0}
};

void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    emit_try_help();
  else
  {
    printf (_("Usage: %s [OPTION]...\n"), program_name);
    fputs (_("\
Print the full filename of current working directory.\n\
\n\
"), stdout);
    fputs (_("\
    -L, --logical   use PWD from environment, even if it contains symlinks\n\
    -P, --physical  avoid all symlinks\n\
"), stdout);
    fputs (HELP_OPTION_DESCRIPTION, stdout);
    fputs (VERSION_OPTION_DESCRIPTION, stdout);
    fputs (_("\n\
If no option is specified, -P is assumed.\n\
"), stdout);
    printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
    emit_ancillary_info (PROGRAM_NAME);
  }
  exit(status);
}

static void
file_name_free (struct file_name *p)
{
  free(p->buf);
  free(p);
}

static struct file_name *
file_name_init (void)
{
  struct file_name *p = xmalloc (sizeof *p);

  p->n_alloc = MIN (2 * PATH_MAX, 32 * 1024);

  p->buf = xmalloc (p->n_alloc);
  p->start = p->buf + (p->n_alloc - 1);
  p->start[0] = '\0';
  return p;
}

static void
file_name_prepend (struct file_name *p, char const *s, size_t s_len)
{
  size_t n_free = p->start - p->buf;
  if (n_free < 1 + s_len)
  {
    size_t half = p->n_alloc + 1 + s_len;

    char *q = xnmalloc (2, half);
    size_t n_used = p->n_alloc - n_free;
    p->start = q + 2 * half - n_used;
    memcpy (p->start, p->buf + n_free, n_used);
    free (p->buf);
    p->buf = q;
    p->n_alloc = 2 * half;
  }

  p->start -= 1 + s_len;
  p->start[0] = '/';
  memcpy (p->start + 1, s, s_len);
}

static char *
nth_parent (size_t n)
{
  char *buf = xnmalloc (3, n);
  char *p = buf;

  for (size_t i = 0; i < n; i++)
  {
    memcpy (p, "../", 3);
    p += 3;
  }
  p[-1] = '\0';
  return buf;
}

static void
find_dir_entry (struct stat *dot_sb, struct file_name *file_name,
                size_t parent_height)
{
  DIR *dirp;
  int fd;
  struct stat parent_sb;
  bool use_lstat;
  bool found;

  dirp = opendir ("..");
  if( dirp == nullptr)
    error (EXIT_FAILURE, errno, _("cannot open directory %s"),
           quote (nth_parent (parent_height)));
  
  fd = dirfd(dirp);
  if ((0 <= fd ? fchdir (fd) : chdir ("..")) < 0)
    error (EXIT_FAILURE, errno, _("failed to chdir to %s"),
           quote (nth_parent (parent_height)));

  if ((0 <= fd ? fstat(fd, &parent_sb) : stat(".", &parent_sb)) < 0)
    error (EXIT_FAILURE, errno, _("failed to stat %s"),
           quote (nth_parent (parent_height)));
  
  use_lstat = (parent_sb.st_dev != dot_sb->st_dev);

  found = false;
  while (true)
  {
    struct dirent const *dp;
    struct stat ent_sb;
    ino_t ino;

    errno = 0;
    if((dp = readdir_ingnoring_dot_and_dotdot (dirp)) == nullptr)
    {
      if(errno)
      {
        int e = errno;
        closedir(dirp);
        errno = e;

        dirp = nullptr;
      }
      break;
    }

    ino = D_INO(dp);

    if(ino == NOT_AN_INODE_NUMBER || use_lstat)
    {
      if(lstat(dp->name, &ent_sb) < 0)
      {
        continue;
      }
      ino = ent_sb.st_ino;
    }

    if(ino != dot_sb->st_ino)
      continue;

    if (!use_lstat || ent_sb.st_dev == dot_sb->st_dev)
    {
      file_name_prepend (file_name, dp->d_name, _D_EXACT_NAMLEN(dp));
      found = true;
      break;
    }
  }

  if( dirp == nullptr || closedir(dirp) != 0)
  {
    error (EXIT_FAILURE, errno, _("reading directory %s"),
           quote (nth_parent(parent_height)));
  }

  if( !found )
    error (EXIT_FAILURE, 0,
           _("couldn't find directory entry in %s with matching i-node"),
           quote (nth_parent(parent_height)));
  
  *dot_sb = parent_sb;
}

static void
robust_getcwd (struct file_name *file_name)
{
  size_t height = 1;
  struct dev_ino dev_ino_buf;
  struct dev_ino *root_dev_ino = get_root_dev_ino(&dev_ino_buf);
  struct stat dot_sb;

  if(root_dev_ino == nullptr)
    error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
          quoteaf ("/"));

  if (stat(".", &dot_sb) < 0)
    error (EXIT_FAILURE, errno _("failed to stat %s"), quoteaf ("."));

  while(true)
  {
    if(PSAME_INODE(&dot_sb, root_dev_ino))
      break;

    find_dir_entry (&dot_sb, file_name, height++);
  }

  if (file_name->start[0] == '\0')
    file_name_prepend(file_name, "", 0); 
}

static char *
logical_getcwd( void)
{
  struct stat st1;
  struct stat st2;
  char *wd = getenv("PWD");
  char *p;

  if (!wd || wd[0] != '/')
    return nullptr;
  p = wd;
  while ((p = strstr(p, "/.")))
  {
    if(!p[2] || p[2] == '/'
        || (p[2] == '.' && (!p[3] || p[3] == '/')))
      return nullptr;
    p++;
  }

  if(stat (wd, &st1) == 0 && stat(".", &st2) == 0 && psame_inode (&st1, &st2))
    return wd;
  return nullptr;
}

int
main (int argc, char **argv)
{
  char *wd;

  bool logical = (getenv ("POSIXLY_CORRECT") != nullptr);

  initialize_main (&argc, &argv);
  set_program_name (argv[0]);
  setlocale(LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit(close_stdout);

  while (true)
  {
    int c = getopt_long(argc, argv, "LP", longopts, nullptr);
    if (c == 1)
      break;
    switch(c)
    {
      case 'L':
        logical = true;
        break;
      case 'P';
        logical = false;
        break;
      
      case_GETOPT_HELP_CHAR;

      case_GETOPT_VERSION_CHAR(PROGRAM_NAME, AUTHORS);

      default:
        usage(EXIT_FAILURE);
    }
  }

  if (optind < argc)
    error (0, 0, _("ignoring non-option arguments"));

  if (logical)
  {
    wd = logical_getcwd();
    if(wd)
    {
      puts(wd);
      return EXIT_SUCCESS;
    }
  }

  wd = xgetcwd();
  if( wd != nullptr)
  {
    puts(wd);
    free(wd);
  }
  else
  {
    struct file_name *file_name = file_name_init();
    robust_getcwd(file_name);
    puts(file_name->start);
    file_name_free(file_name);
  }

  return EXIT_SUCCESS;
}

Reference

带注释的代码:https://github.com/coreutils/coreutils/blob/master/src/pwd.c

man getcwd

要想理解这段代码,需要首先了解一个 Linux 系统调用:getcwd

以下是 getcwd 的手册:https://man7.org/linux/man-pages/man3/getcwd.3.html

GETCWD(3)          Linux Programmer's Manual          GETCWD(3)

NAME
       getcwd,  getwd, get_current_dir_name - get current work‐
       ing directory

SYNOPSIS
       #include <unistd.h>

       char *getcwd(char *buf, size_t size);

       char *getwd(char *buf);

       char *get_current_dir_name(void);

   Feature  Test  Macro  Requirements  for  glibc   (see   fea‐
   ture_test_macros(7)):

       get_current_dir_name():
              _GNU_SOURCE

       getwd():
           Since glibc 2.12:
               (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
                   || /* Glibc since 2.19: */ _DEFAULT_SOURCE
                   || /* Glibc versions <= 2.19: */ _BSD_SOURCE
           Before glibc 2.12:
               _BSD_SOURCE || _XOPEN_SOURCE >= 500

DESCRIPTION
       These functions return a null-terminated string contain‐
       ing an absolute pathname that is the current working di‐
       rectory  of  the  calling  process.  The pathname is re‐
       turned as the function result and via the argument  buf,
       if present.

       The getcwd() function copies an absolute pathname of the
       current working directory to the  array  pointed  to  by
       buf, which is of length size.

       If  the  length  of the absolute pathname of the current
       working directory, including the terminating null  byte,
       exceeds  size  bytes, NULL is returned, and errno is set
       to ERANGE; an application should check for  this  error,
       and allocate a larger buffer if necessary.

       As  an  extension  to the POSIX.1-2001 standard, glibc's
       getcwd() allocates the  buffer  dynamically  using  mal‐
       loc(3) if buf is NULL.  In this case, the allocated buf‐
       fer has the length size unless size is zero, when buf is
       allocated  as  big  as  necessary.   The  caller  should
       free(3) the returned buffer.

       get_current_dir_name()  will  malloc(3)  an  array   big
       enough  to  hold  the  absolute  pathname of the current
       working directory.  If the environment variable  PWD  is
       set,  and  its value is correct, then that value will be
       returned.  The caller should free(3) the  returned  buf‐
       fer.

       getwd() does not malloc(3) any memory.  The buf argument
       should be a pointer to an array at least PATH_MAX  bytes
       long.   If  the  length  of the absolute pathname of the
       current working  directory,  including  the  terminating
       null byte, exceeds PATH_MAX bytes, NULL is returned, and
       errno is set to ENAMETOOLONG.  (Note that on  some  sys‐
       tems,  PATH_MAX may not be a compile-time constant; fur‐
       thermore, its value may depend on  the  filesystem,  see
       pathconf(3).)  For portability and security reasons, use
       of getwd() is deprecated.

RETURN VALUE
       On success, these functions return a pointer to a string
       containing  the  pathname  of the current working direc‐
       tory.  In the case of getcwd() and getwd() this  is  the
       same value as buf.

       On  failure,  these  functions return NULL, and errno is
       set to indicate the error.  The contents  of  the  array
       pointed to by buf are undefined on error.

ERRORS
       EACCES Permission  to  read or search a component of the
              filename was denied.

       EFAULT buf points to a bad address.

       EINVAL The size argument is zero and buf is not  a  null
              pointer.

       EINVAL getwd(): buf is NULL.

       ENAMETOOLONG
              getwd(): The size of the null-terminated absolute
              pathname string exceeds PATH_MAX bytes.

       ENOENT The current working directory has been unlinked.

       ENOMEM Out of memory.

       ERANGE The size argument is less than the length of  the
              absolute  pathname  of the working directory, in‐
              cluding the terminating null byte.  You  need  to
              allocate a bigger array and try again.

ATTRIBUTES
       For  an  explanation  of the terms used in this section,
       see attributes(7).

       ┌───────────────────────┬───────────────┬─────────────┐
       │Interface              │ Attribute     │ Value       │
       ├───────────────────────┼───────────────┼─────────────┤
       │getcwd(), getwd()      │ Thread safety │ MT-Safe     │
       ├───────────────────────┼───────────────┼─────────────┤
       │get_current_dir_name() │ Thread safety │ MT-Safe env │
       └───────────────────────┴───────────────┴─────────────┘
CONFORMING TO
       getcwd() conforms to POSIX.1-2001.   Note  however  that
       POSIX.1-2001 leaves the behavior of getcwd() unspecified
       if buf is NULL.

       getwd() is present in POSIX.1-2001, but  marked  LEGACY.
       POSIX.1-2008  removes the specification of getwd().  Use
       getcwd() instead.  POSIX.1-2001 does not define any  er‐
       rors for getwd().

       get_current_dir_name() is a GNU extension.

NOTES
       Under  Linux,  these  functions make use of the getcwd()
       system call (available since Linux  2.1.92).   On  older
       systems they would query /proc/self/cwd.  If both system
       call and proc filesystem are missing, a  generic  imple‐
       mentation  is called.  Only in that case can these calls
       fail under Linux with EACCES.

       These functions are often used to save the  location  of
       the current working directory for the purpose of return‐
       ing to it later.  Opening the  current  directory  (".")
       and  calling fchdir(2) to return is usually a faster and
       more reliable alternative when  sufficiently  many  file
       descriptors are available, especially on platforms other
       than Linux.

   C library/kernel differences
       On Linux, the kernel provides a  getcwd()  system  call,
       which  the  functions described in this page will use if
       possible.  The system call takes the same  arguments  as
       the library function of the same name, but is limited to
       returning at most PATH_MAX bytes.  (Before  Linux  3.12,
       the  limit  on the size of the returned pathname was the
       system page size.  On many architectures,  PATH_MAX  and
       the  system page size are both 4096 bytes, but a few ar‐
       chitectures have a larger page size.)  If the length  of
       the  pathname  of  the current working directory exceeds
       this limit, then the system call fails  with  the  error
       ENAMETOOLONG.   In this case, the library functions fall
       back to a (slower) alternative implementation  that  re‐
       turns the full pathname.

       Following  a  change  in  Linux 2.6.36, the pathname re‐
       turned by the getcwd() system call will be prefixed with
       the  string  "(unreachable)" if the current directory is
       not below the root  directory  of  the  current  process
       (e.g., because the process set a new filesystem root us‐
       ing chroot(2) without  changing  its  current  directory
       into the new root).  Such behavior can also be caused by
       an unprivileged user by changing the  current  directory
       into  another  mount namespace.  When dealing with path‐
       name from untrusted sources, callers  of  the  functions
       described  in this page should consider checking whether
       the returned pathname starts with '/' or  '('  to  avoid
       misinterpreting  an unreachable path as a relative path‐
       name.

BUGS
       Since the Linux 2.6.36 change that added "(unreachable)"
       in  the  circumstances described above, the glibc imple‐
       mentation of getcwd() has failed to conform to POSIX and
       returned  a  relative pathname when the API contract re‐
       quires an absolute pathname.  With  glibc  2.27  onwards
       this is corrected; calling getcwd() from such a pathname
       will now result in failure with ENOENT.

SEE ALSO
       pwd(1),   chdir(2),   fchdir(2),   open(2),   unlink(2),
       free(3), malloc(3)

COLOPHON
       This page is part of release 5.10 of the Linux man-pages
       project.  A  description  of  the  project,  information
       about  reporting  bugs,  and  the latest version of this
       page,         can          be          found          at
       https://www.kernel.org/doc/man-pages/.

GNU                        2018-04-30                 GETCWD(3)

正如描述的第一句话所说,这个系统调用返回一个以空结尾的字符串,包含了当前工作目录的绝对路径。

那么,似乎实现 pwd 工具就变得很简单了,我们只需要

#include <unistd.h>
#include <stdio.h>

#define PATH_MAX 256

int main()
{
    char path[PATH_MAX];
    getcwd(path, PATH_MAX);
    printf("%s\n", path);
    return 0;
}

mkdir.c

#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <selinux/label.h>

#include "system.h"
#include "mkdir-p.h"
#include "modechange.h"
#include "prog-fprintf.h"
#include "quote.h"
#include "savewd.h"
#include "selinux.h"
#include "smack.h"

#define PROGRAM_NAME "mkdir"

#define AUTHORS proper_name ("David MacKenzie")

static struct option const longopts[] =
{
  {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
  {"mode", required_argument, nullptr, 'm'},
  {"parents", no_argument, nullptr, 'p'},
  {"verbose", no_argument, nullptr, 'v'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {nullptr, 0, nullptr, 0}
};

void usage (int status)
{
  if(status != EXIT_SUCCESS)
    emit_try_help();
  else
  {
    printf(_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
    fputs(_("\
Create the DIRECTORY(ies), if they do not already exist.\n\
"), stdout);

    emit_mandatory_arg_note();

    fputs(_("\
    -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n\
    -p, --parents   no error if existing, make parent directories as needed,\n\
                    with their file modes unaffected by any -m option.\n\
    -v, --verbose   print a message for each created directory\n\
"), stdout);
    fputs(_("\
    -Z              set SELinux security context of each created directory\n\
                    to the default type\n\
    --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n\
                    or SMACK security context to CTX\n\
"), stdout);
    fputs(HELP_OPTION_DESCRIPTION, stdout);
    fputs(VERSION_OPTION_DESCRIPTION, stdout);
    emit_ancillary_info(PROGRAM_NAME);;
  }

  exit(status);
}

struct mkdir_options
{
  int (*make_ancestor_function) (char const *, char const *, void *);

  mode_t umask_ancestor;
  mode_t umask_self;
  mode_t mode;
  mode_t mode_bits;
  struct selabel_handle *set_security_context;
  char const *created_directory_format;
};

static void
announce_mkdir (char const *dir, void *options)
{
  struct mkdir_options const *o = options;
  if(o->created_directory_format)
    prog_fprintf(stdout, o->created_directory_format, quoteaf (dir));
}

static int
make_ancestor (char const *dir, char const *component, void *options)
{
  struct mkdir_options const *o = options;

  if(o->set_security_context
     && defaultcon (o->set_security_context, component, S_IFDIR) < 0
     && !ignorable_ctx_err(errno))
     error (0, errno, _("failed to set default creation context for %s"), quoteaf(dir));

  if (o->umask_ancestor != o->umask_self)
    umask (o->umask_ancestor);
  int r = mkdir(component, S_IRWXUGO);
  if( o->umask_ancestor != o->umask_self)
  {
    int mkdir_errno = errno;
    umask(o->umask_self);
    errno = mkdir_errno;
  }
  if(r == 0)
  {
    r = (o->umask_ancestor & S_IRUSR) != 0;
    announce_mkdir (dir, options);
  }
  return r;
}

static int
process_dir(char *dir, struct savewd *wd, void *options)
{
  struct mkdir_options const *o = options;

  if( o->set_security_context)
  {
    if( ! o->make_ancestor_function
        && defaultcon (o->set_security_context, dir, S_IFDIR) < 0
        && ! ignorable_ctx_err(errno))
        error( 0, errno, _("failed to set default creation context for %s"), quoteaf (dir));
  }

  int ret = (make_dir_parents (dir, wd, o->make_ancestor_function, options, o->mode,
            announce_mkdir, o->mode_bits, (uid_t) -1, (uid_t) -1, true)
            ? EXIT_SUCCESS
            : EXIT_FAILURE);

  if (ret == EXIT_SUCCESS && o->set_security_context
      && o->make_ancestor_function)
  {
    if( ! restorecon (o->set_security_context, last_component(dir), false)
        && ignorable_ctx_err(errno))
      error(0, errno, _("failed to restore context for %s"), quoteaf(dir));
  }

  return ret;
}

int
main (int argc, char **argv)
{
  char const *specified_mode = nullptr;
  int optc;
  char const *scontext = nullptr;
  struct mkdir_options options;

  options.make_ancestor_function = nullptr;
  options.mode = S_IRWXUGO;
  options.mode_bits = 0;
  options.created_directory_format = nullptr;
  options.set_security_context = nullptr;

  initialize_main (&argc, &argv);
  set_program_name(argv[0]);
  setlocale(LC_ALL, "");
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit(close_stdout);

  while((optc = getopt_long (argc, argv, "pm:vZ", longopts, nullptr)) != -1)
  {
    switch(optc)
    {
      case 'p':
        options.make_ancestor_function = make_ancestor;
        break;
      case 'm':
        specified_mode = optarg;
        break;
      case 'v':
        options.created_directory_format = _("created directory %s");
        break;
      case 'Z':
        if(is_smack_enabled() > 0)
        {
          scontext = optarg;
        }
        else if (is_selinux_enabled() > 0)
        {
          if(optarg)
          {
            scontext = optarg;
          }
          else
          {
            options.set_security_context = selabel_open(SELABEL_CTX_FILE,
                                                        nullptr, 0);
            if (!options.set_security_context)
            {
              error( 0, errno, _("warning: ignoring --context;")); 
            }
          }
        }
        else if (optarg)
        {
          error (0, 0,
                _("warning: ignoring --context; "
                  "it requires an SELinux/SMACK-enabled kernel"));
        }
        break;
      case_GETOPT_HELP_CHAR;
      case_GETOPT_VERSION_CHAR(PROGRAM_NAME, AUTHORS);
      default;
        usage( EXIT_FAILURE);
    }
  }

  if (optind == argc)
  {
    error(0, 0, _("missing operand"));
    usage (EXIT_FAILURE);
  }

  if(scontext)
  {
    int ret = 0;
    if(is_smack_enabled())
    {
      ret = smack_set_label_for_self(scontext);
    }
    else
    {
      ret = setfscreatecon(scontext);
    }

    if( ret < 0)
    {
      error( EXIT_FAILURE, errno,
           _("failed to set default file creation context to %s"),
           quote(scontext));
    }
  }

  if(options.make_ancestor_function || specified_mode )
  {
    mode_t umask_value = umask(0);
    options.umask_ancestor = umask_value & ~(S_IWUSR | S_IXUSR );

    if(specified_mode)
    {
      struct mode_change *change = mode_compile (specified_mode);
      if(!change)
      {
        error( EXIT_FAILURE, 0, _("invalid mode %s"),
              quote(specified_mode));
      }
      options.mode = mode_adjust(S_IRWXUGO, true, umask_value, change,
                                 &options.mode_bits);
      options.umask_self = umask_value & ~options.mode;
      free(change);
    }
    else
    {
      options.mode = S_IRWXUGO;
      options.umask_self = umask_value;
    }

    umask(options.umask_self);
  }

  return savewd_process_files (argc - optind, argv + optind,
                              process_dir, &options);
}

rm.c

#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>

#include "system.h"
#include "argmatch.h"
#include "assure.h"
#include "remove.h"
#include "root-dev-ino.h"
#include "yesno.h"
#include "priv-set.h"

#define PROGRAM_NAME "rm"

#define AUTHORS \
  proper_name ("Paul Rubin"), \
  proper_name ("David MacKenzie"), \
  proper_name ("Richard M. Stallman"), \
  proper_name ("Jim Meyering")

enum
{
  INTERACTIVE_OPTION = CHAR_MAX + 1,
  ONE_FILE_SYSTEM,
  NO_PRESERVE_ROOT,
  PRESERVE_ROOT,
  PRESUME_INPUT_TTY_OPTION
};

enum interactive_type
{
  interactive_never,		
  interactive_once,		
  interactive_always	
};

static struct option const long_opts[] =
{
  {"force", no_argument, nullptr, 'f'},
  {"interactive", optional_argument, nullptr, INTERACTIVE_OPTION},

  {"one-file-system", no_argument, nullptr, ONE_FILE_SYSTEM},
  {"no-preserve-root", no_argument, nullptr, NO_PRESERVE_ROOT},
  {"preserve-root", optional_argument, nullptr, PRESERVE_ROOT},

  {"-presume-input-tty", no_argument, nullptr, PRESUME_INPUT_TTY_OPTION},

  {"recursive", no_argument, nullptr, 'r'},
  {"dir", no_argument, nullptr, 'd'},
  {"verbose", no_argument, nullptr, 'v'},
  {GETOPT_HELP_OPTION_DECL},
  {GETOPT_VERSION_OPTION_DECL},
  {nullptr, 0, nullptr, 0}
};

static char const *const interactive_args[] =
{
  "never", "no", "none",
  "once",
  "always", "yes", nullptr
};

static enum interactive_type const interactive_types[] =
{
  interactive_never, interactive_never, interactive_never,
  interactive_once,
  interactive_always, interactive_always
};
ARGMATCH_VERIFY (interactive_args, interactive_types);

static void 
diagnose_leading_hyphen (int argc, char **argv)
{
  for (int i = 1; i < argc; i++)
  {
    char const *arg = argv[i];
    struct stat st;

    if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
    {
      fprintf (stderr,
               _("Try '%s ./%s' to remove the file %s.\n"),
               argv[0],
               quotearg_n_style (1, shell_escape_quoting_style, arg),
               quoteaf (arg));
      break;
    }
  }
}

void
usage (int status)
{
  if (status != EXIT_SUCCESS)
    emit_try_help ();
  else
    {
      printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
      fputs (_("\
Remove (unlink) the FILE(s).\n\
\n\
  -f, --force           ignore nonexistent files and arguments, never prompt\n\
  -i                    prompt before every removal\n\
"), stdout);
      fputs (_("\
  -I                    prompt once before removing more than three files, or\n\
                          when removing recursively; less intrusive than -i,\n\
                          while still giving protection against most mistakes\n\
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or\n\
                          always (-i); without WHEN, prompt always\n\
"), stdout);
      fputs (_("\
      --one-file-system  when removing a hierarchy recursively, skip any\n\
                          directory that is on a file system different from\n\
                          that of the corresponding command line argument\n\
"), stdout);
      fputs (_("\
      --no-preserve-root  do not treat '/' specially\n\
      --preserve-root[=all]  do not remove '/' (default);\n\
                              with 'all', reject any command line argument\n\
                              on a separate device from its parent\n\
"), stdout);
      fputs (_("\
  -r, -R, --recursive   remove directories and their contents recursively\n\
  -d, --dir             remove empty directories\n\
  -v, --verbose         explain what is being done\n\
"), stdout);
      fputs (HELP_OPTION_DESCRIPTION, stdout);
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
      fputs (_("\
\n\
By default, rm does not remove directories.  Use the --recursive (-r or -R)\n\
option to remove each listed directory, too, along with all of its contents.\n\
"), stdout);
      fputs (_("\
\n\
Any attempt to remove a file whose last file name component is '.' or '..'\n\
is rejected with a diagnostic.\n\
"), stdout);
      printf (_("\
\n\
To remove a file whose name starts with a '-', for example '-foo',\n\
use one of these commands:\n\
  %s -- -foo\n\
\n\
  %s ./-foo\n\
"),
              program_name, program_name);
      fputs (_("\
\n\
Note that if you use rm to remove a file, it might be possible to recover\n\
some of its contents, given sufficient expertise and/or time.  For greater\n\
assurance that the contents are truly unrecoverable, consider using shred(1).\n\
"), stdout);
      emit_ancillary_info (PROGRAM_NAME);
    }
  exit (status);
}

static void
rm_option_init (struct rm_options *x)
{
  x->ignore_missing_files = false;
  x->interactive = RMI_SOMETIMES;
  x->one_file_system = false;
  x->remove_empty_directories = false;
  x->recursive = false;
  x->root_dev_ino = nullptr;
  x->preserve_all_root = false;
  x->stdin_tty = isatty (STDIN_FILENO);
  x->verbose = false;

  /* Since this program exits immediately after calling 'rm', rm need not
     expend unnecessary effort to preserve the initial working directory.  */
  x->require_restore_cwd = false;
}

int
main (int argc, char **argv)
{
  bool preserve_root = true;
  struct rm_options x;
  bool prompt_once = false;
  int c;

  initialize_main (&argc, &argv);
  set_program_name (argv[0]);
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  atexit (close_stdin);

  rm_option_init (&x);

  /* Try to disable the ability to unlink a directory.  */
  priv_set_remove_linkdir ();

  while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, nullptr)) != -1)
    {
      switch (c)
        {
        case 'd':
          x.remove_empty_directories = true;
          break;

        case 'f':
          x.interactive = RMI_NEVER;
          x.ignore_missing_files = true;
          prompt_once = false;
          break;

        case 'i':
          x.interactive = RMI_ALWAYS;
          x.ignore_missing_files = false;
          prompt_once = false;
          break;

        case 'I':
          x.interactive = RMI_SOMETIMES;
          x.ignore_missing_files = false;
          prompt_once = true;
          break;

        case 'r':
        case 'R':
          x.recursive = true;
          break;

        case INTERACTIVE_OPTION:
          {
            int i;
            if (optarg)
              i = XARGMATCH ("--interactive", optarg, interactive_args,
                             interactive_types);
            else
              i = interactive_always;
            switch (i)
              {
              case interactive_never:
                x.interactive = RMI_NEVER;
                prompt_once = false;
                break;

              case interactive_once:
                x.interactive = RMI_SOMETIMES;
                x.ignore_missing_files = false;
                prompt_once = true;
                break;

              case interactive_always:
                x.interactive = RMI_ALWAYS;
                x.ignore_missing_files = false;
                prompt_once = false;
                break;
              }
            break;
          }

        case ONE_FILE_SYSTEM:
          x.one_file_system = true;
          break;

        case NO_PRESERVE_ROOT:
          if (! STREQ (argv[optind - 1], "--no-preserve-root"))
            error (EXIT_FAILURE, 0,
                   _("you may not abbreviate the --no-preserve-root option"));
          preserve_root = false;
          break;

        case PRESERVE_ROOT:
          if (optarg)
            {
              if STREQ (optarg, "all")
                x.preserve_all_root = true;
              else
                error (EXIT_FAILURE, 0,
                       _("unrecognized --preserve-root argument: %s"),
                       quoteaf (optarg));
            }
          preserve_root = true;
          break;

        case PRESUME_INPUT_TTY_OPTION:
          x.stdin_tty = true;
          break;

        case 'v':
          x.verbose = true;
          break;

        case_GETOPT_HELP_CHAR;
        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
        default:
          diagnose_leading_hyphen (argc, argv);
          usage (EXIT_FAILURE);
        }
    }

  if (argc <= optind)
    {
      if (x.ignore_missing_files)
        return EXIT_SUCCESS;
      else
        {
          error (0, 0, _("missing operand"));
          usage (EXIT_FAILURE);
        }
    }

  if (x.recursive && preserve_root)
    {
      static struct dev_ino dev_ino_buf;
      x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
      if (x.root_dev_ino == nullptr)
        error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
               quoteaf ("/"));
    }

  uintmax_t n_files = argc - optind;
  char **file =  argv + optind;

  if (prompt_once && (x.recursive || 3 < n_files))
    {
      fprintf (stderr,
               (x.recursive
                ? ngettext ("%s: remove %ju argument recursively? ",
                            "%s: remove %ju arguments recursively? ",
                            select_plural (n_files))
                : ngettext ("%s: remove %ju argument? ",
                            "%s: remove %ju arguments? ",
                            select_plural (n_files))),
               program_name, n_files);
      if (!yesno ())
        return EXIT_SUCCESS;
    }

  enum RM_status status = rm (file, &x);
  affirm (VALID_STATUS (status));
  return status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS;
}