|
我下了一个rm命令的源代码,看不明白,请高手详细讲解一下,不胜感激!#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <assert.h>
#include "system.h"
#include "dirname.h"
#include "error.h"
#include "quote.h"
#include "remove.h"
#include "root-dev-ino.h"
#include "save-cwd.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "rm"
#define AUTHORS \
"Paul Rubin", "David MacKenzie, Richard Stallman", "Jim Meyering"
/* Name this program was run with. */
char *program_name;
/* For long options that have no equivalent short option, use a
non-character as a pseudo short option, starting with CHAR_MAX + 1. */
enum
{
NO_PRESERVE_ROOT = CHAR_MAX + 1,
PRESERVE_ROOT,
PRESUME_INPUT_TTY_OPTION
};
static struct option const long_opts[] =
{
{"directory", no_argument, NULL, 'd'},
{"force", no_argument, NULL, 'f'},
{"interactive", no_argument, NULL, 'i'},
{"no-preserve-root", no_argument, 0, NO_PRESERVE_ROOT},
{"preserve-root", no_argument, 0, PRESERVE_ROOT},
/* This is solely for testing. Do not document. */
/* It is relatively difficult to ensure that there is a tty on stdin.
Since rm acts differently depending on that, without this option,
it'd be harder to test the parts of rm that depend on that setting. */
{"presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},
{"recursive", no_argument, NULL, 'r'},
{"verbose", no_argument, NULL, 'v'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
char *base = base_name (program_name);
printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
fputs (_("\
Remove (unlink) the FILE(s).\n\
\n\
-d, --directory unlink FILE, even if it is a non-empty directory\n\
(super-user only; this works only if your system\n\
supports `unlink' for nonempty directories)\n\
-f, --force ignore nonexistent files, never prompt\n\
-i, --interactive prompt before any removal\n\
"), stdout);
fputs (_("\
--no-preserve-root do not treat `/' specially (the default)\n\
--preserve-root fail to operate recursively on `/'\n\
-r, -R, --recursive remove the contents of directories recursively\n\
-v, --verbose explain what is being done\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, 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\
"),
base, base);
fputs (_("\
\n\
Note that if you use rm to remove a file, it is usually possible to recover\n\
the contents of that file. If you want more assurance that the contents are\n\
truly unrecoverable, consider using shred.\n\
"), stdout);
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}
exit (status);
}
static void
rm_option_init (struct rm_options *x)
{
x->unlink_dirs = 0;
x->ignore_missing_files = 0;
x->interactive = 0;
x->recursive = 0;
x->root_dev_ino = NULL;
x->stdin_tty = isatty (STDIN_FILENO);
x->verbose = 0;
}
int
main (int argc, char **argv)
{
bool preserve_root = false;
struct rm_options x;
int fail = 0;
int c;
initialize_main (&argc, &argv);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
rm_option_init (&x);
while ((c = getopt_long (argc, argv, "dfirvR", long_opts, NULL)) != -1)
{
switch (c)
{
case 0: /* Long option. */
break;
case 'd':
x.unlink_dirs = 1;
break;
case 'f':
x.interactive = 0;
x.ignore_missing_files = 1;
break;
case 'i':
x.interactive = 1;
x.ignore_missing_files = 0;
break;
case 'r':
case 'R':
x.recursive = 1;
break;
case NO_PRESERVE_ROOT:
preserve_root = false;
break;
case PRESERVE_ROOT:
preserve_root = true;
break;
case PRESUME_INPUT_TTY_OPTION:
x.stdin_tty = 1;
break;
case 'v':
x.verbose = 1;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
if (argc <= optind)
{
if (x.ignore_missing_files)
exit (EXIT_SUCCESS);
else
{
error (0, 0, _("too few arguments"));
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 == NULL)
error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
quote ("/"));
}
{
size_t n_files = argc - optind;
char const *const *file = (char const *const *) argv + optind;
enum RM_status status = rm (n_files, file, &x);
assert (VALID_STATUS (status));
if (status == RM_ERROR)
fail = 1;
}
exit (fail);
}
请大家不吝赐教! |
|