da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1992-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * Glenn Fowler
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * AT&T Research
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * rm [-fir] [file ...]
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz"[-?\n@(#)$Id: rm (AT&T Research) 2009-06-18 $\n]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+NAME?rm - remove files]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+DESCRIPTION?\brm\b removes the named \afile\a arguments. By default it"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" does not remove directories. If a file is unwritable, the"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" standard input is a terminal, and the \b--force\b option is not"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" given, \brm\b prompts the user for whether to remove the file."
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" An affirmative response (\by\b or \bY\b) removes the file, a quit"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" response (\bq\b or \bQ\b) causes \brm\b to exit immediately, and"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" all other responses skip the current file.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[c|F:clear|clobber?Clear the contents of each file before removing by"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" writing a 0 filled buffer the same size as the file, executing"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" \bfsync\b(2) and closing before attempting to remove. Implemented"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" only on systems that support \bfsync\b(2).]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[d:directory?\bremove\b(3) (or \bunlink\b(2)) directories rather than"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" \brmdir\b(2), and don't require that they be empty before removal."
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" The caller requires sufficient privilege, not to mention a strong"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" constitution, to use this option. Even though the directory must"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" not be empty, \brm\b still attempts to empty it before removal.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[f:force?Ignore nonexistent files and never prompt the user.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[i:interactive|prompt?Prompt whether to remove each file."
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" An affirmative response (\by\b or \bY\b) removes the file, a quit"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" response (\bq\b or \bQ\b) causes \brm\b to exit immediately, and"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" all other responses skip the current file.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[r|R:recursive?Remove the contents of directories recursively.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[u:unconditional?If \b--recursive\b and \b--force\b are also enabled then"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" the owner read, write and execute modes are enabled (if not already"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" enabled) for each directory before attempting to remove directory"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin" contents.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[v:verbose?Print the name of each file before removing it.]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"\nfile ...\n"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"[+SEE ALSO?\bmv\b(1), \brmdir\b(2), \bunlink\b(2), \bremove\b(3)]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define beenhere(f) (((f)->fts_number>>1)==(f)->fts_statp->st_nlink)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define nonempty(f) ((f)->fts_parent->fts_number|=RM_ENTRY)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define retry(f) ((f)->fts_number=((f)->fts_statp->st_nlink<<1))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * remove a single file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register int n;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (ent->fts_info == FTS_NS || ent->fts_info == FTS_ERR || ent->fts_info == FTS_SLNONE)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!chmod(ent->fts_name, (ent->fts_statp->st_mode & S_IPERM)|S_IRWXU))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(2, "%s: cannot %s directory", ent->fts_path, (ent->fts_info & FTS_NR) ? "read" : "search");
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (path[0] == '.' && (!path[1] || path[1] == '.' && !path[2]) && (ent->fts_level > 0 || path[1]))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (state->unconditional && (ent->fts_statp->st_mode ^ S_IRWXU))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin chmod(path, (ent->fts_statp->st_mode & S_IPERM)|S_IRWXU);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (ent->fts_accpath == ent->fts_name || !(s = strrchr(ent->fts_accpath, '/')))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin v = st.st_nlink <= 2 || st.st_ino == ent->fts_parent->fts_statp->st_ino && st.st_dev == ent->fts_parent->fts_statp->st_dev || strchr(astconf("PATH_ATTRIBUTES", path, NiL), 'l');
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if ((v = astquery(-1, "remove directory %s? ", ent->fts_path)) < 0 || sh_checksig(state->context))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (v > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*FALLTHROUGH*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if ((ent->fts_info == FTS_DC || state->directory) ? remove(path) : rmdir(path))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /*FALLTHROUGH*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_SYSTEM|2, "%s: directory not removed", ent->fts_path);
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if ((v = astquery(-1, "remove %s? ", ent->fts_path)) < 0 || sh_checksig(state->context))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (v > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if (!state->force && state->terminal && S_ISREG(ent->fts_statp->st_mode))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ent->fts_statp->st_uid != state->uid ? "``not owner''" :
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz fmtmode(ent->fts_statp->st_mode & S_IPERM, 0) + 1, ent->fts_path)) < 0 ||
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (v > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (state->clobber && S_ISREG(ent->fts_statp->st_mode) && ent->fts_statp->st_size > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_SYSTEM|2, "%s: cannot clear data", ent->fts_path);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (write(n, state->buf, sizeof(state->buf)) != sizeof(state->buf))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(ERROR_SYSTEM|2, "%s: data clear error", ent->fts_path);
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin error(ERROR_SYSTEM|2, "%s: not removed", ent->fts_path);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin cmdinit(argc, argv, context, ERROR_CATALOG, ERROR_NOTIFY);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin error(1, "%s not implemented on this system", opt_info.name);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (*argv && streq(*argv, "-") && !streq(*(argv - 1), "--"))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin state.unconditional = state.unconditional && state.recursive && state.force;
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin while (!sh_checksig(context) && (ent = fts_read(fts)) && !rm(&state, ent));