17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*79033acbSas145665 * Common Development and Distribution License (the "License"). 6*79033acbSas145665 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 227c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 26*79033acbSas145665 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 277c478bd9Sstevel@tonic-gate * Use is subject to license terms. 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * rm [-fiRr] file ... 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <fcntl.h> 387c478bd9Sstevel@tonic-gate #include <string.h> 397c478bd9Sstevel@tonic-gate #include <sys/types.h> 407c478bd9Sstevel@tonic-gate #include <sys/stat.h> 417c478bd9Sstevel@tonic-gate #include <dirent.h> 427c478bd9Sstevel@tonic-gate #include <limits.h> 437c478bd9Sstevel@tonic-gate #include <locale.h> 447c478bd9Sstevel@tonic-gate #include <langinfo.h> 457c478bd9Sstevel@tonic-gate #include <unistd.h> 467c478bd9Sstevel@tonic-gate #include <stdlib.h> 477c478bd9Sstevel@tonic-gate #include <errno.h> 487c478bd9Sstevel@tonic-gate #include <sys/resource.h> 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #define ARGCNT 5 /* Number of arguments */ 517c478bd9Sstevel@tonic-gate #define CHILD 0 527c478bd9Sstevel@tonic-gate #define DIRECTORY ((buffer.st_mode&S_IFMT) == S_IFDIR) 537c478bd9Sstevel@tonic-gate #define SYMLINK ((buffer.st_mode&S_IFMT) == S_IFLNK) 547c478bd9Sstevel@tonic-gate #define FAIL -1 557c478bd9Sstevel@tonic-gate #define MAXFORK 100 /* Maximum number of forking attempts */ 567c478bd9Sstevel@tonic-gate #define NAMESIZE MAXNAMLEN + 1 /* "/" + (file name size) */ 577c478bd9Sstevel@tonic-gate #define TRUE 1 587c478bd9Sstevel@tonic-gate #define FALSE 0 597c478bd9Sstevel@tonic-gate #define WRITE 02 607c478bd9Sstevel@tonic-gate #define SEARCH 07 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate static int errcode; 637c478bd9Sstevel@tonic-gate static int interactive, recursive, silent; /* flags for command line options */ 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate static void rm(char *, int); 667c478bd9Sstevel@tonic-gate static void undir(char *, int, dev_t, ino_t); 677c478bd9Sstevel@tonic-gate static int yes(void); 687c478bd9Sstevel@tonic-gate static int mypath(dev_t, ino_t); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static char yeschr[SCHAR_MAX + 2]; 717c478bd9Sstevel@tonic-gate static char nochr[SCHAR_MAX + 2]; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate static char *fullpath; 747c478bd9Sstevel@tonic-gate static int homedirfd; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static void push_name(char *name, int first); 777c478bd9Sstevel@tonic-gate static void pop_name(int first); 787c478bd9Sstevel@tonic-gate static void force_chdir(char *); 797c478bd9Sstevel@tonic-gate static void ch_dir(char *); 807c478bd9Sstevel@tonic-gate static char *get_filename(char *name); 817c478bd9Sstevel@tonic-gate static void chdir_home(void); 827c478bd9Sstevel@tonic-gate static void check_homedir(void); 837c478bd9Sstevel@tonic-gate static void cleanup(void); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate static char *cwd; /* pathname of home dir, from getcwd() */ 867c478bd9Sstevel@tonic-gate static rlim_t maxfiles; /* maximum number of open files */ 877c478bd9Sstevel@tonic-gate static int first_dir = 1; /* flag set when first trying to remove a dir */ 887c478bd9Sstevel@tonic-gate /* flag set when can't get dev/inode of a parent dir */ 897c478bd9Sstevel@tonic-gate static int parent_err = 0; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate struct dir_id { 927c478bd9Sstevel@tonic-gate dev_t dev; 937c478bd9Sstevel@tonic-gate ino_t inode; 947c478bd9Sstevel@tonic-gate struct dir_id *next; 957c478bd9Sstevel@tonic-gate }; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * homedir is the first of a linked list of structures 997c478bd9Sstevel@tonic-gate * containing unique identifying device and inode numbers for 1007c478bd9Sstevel@tonic-gate * each directory, from the home dir up to the root. 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate static struct dir_id homedir; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate int 1057c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate extern int optind; 1087c478bd9Sstevel@tonic-gate int errflg = 0; 1097c478bd9Sstevel@tonic-gate int c; 1107c478bd9Sstevel@tonic-gate struct rlimit rl; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1137c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1147c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1157c478bd9Sstevel@tonic-gate #endif 1167c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate (void) strncpy(yeschr, nl_langinfo(YESSTR), SCHAR_MAX + 1); 1197c478bd9Sstevel@tonic-gate (void) strncpy(nochr, nl_langinfo(NOSTR), SCHAR_MAX + 1); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "frRi")) != EOF) 1227c478bd9Sstevel@tonic-gate switch (c) { 1237c478bd9Sstevel@tonic-gate case 'f': 1247c478bd9Sstevel@tonic-gate silent = TRUE; 1257c478bd9Sstevel@tonic-gate #ifdef XPG4 1267c478bd9Sstevel@tonic-gate interactive = FALSE; 1277c478bd9Sstevel@tonic-gate #endif 1287c478bd9Sstevel@tonic-gate break; 1297c478bd9Sstevel@tonic-gate case 'i': 1307c478bd9Sstevel@tonic-gate interactive = TRUE; 1317c478bd9Sstevel@tonic-gate #ifdef XPG4 1327c478bd9Sstevel@tonic-gate silent = FALSE; 1337c478bd9Sstevel@tonic-gate #endif 1347c478bd9Sstevel@tonic-gate break; 1357c478bd9Sstevel@tonic-gate case 'r': 1367c478bd9Sstevel@tonic-gate case 'R': 1377c478bd9Sstevel@tonic-gate recursive = TRUE; 1387c478bd9Sstevel@tonic-gate break; 1397c478bd9Sstevel@tonic-gate case '?': 1407c478bd9Sstevel@tonic-gate errflg = 1; 1417c478bd9Sstevel@tonic-gate break; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * For BSD compatibility allow '-' to delimit the end 1467c478bd9Sstevel@tonic-gate * of options. However, if options were already explicitly 1477c478bd9Sstevel@tonic-gate * terminated with '--', then treat '-' literally: otherwise, 1487c478bd9Sstevel@tonic-gate * "rm -- -" won't remove '-'. 1497c478bd9Sstevel@tonic-gate */ 1507c478bd9Sstevel@tonic-gate if (optind < argc && 1517c478bd9Sstevel@tonic-gate strcmp(argv[optind], "-") == 0 && 1527c478bd9Sstevel@tonic-gate strcmp(argv[optind - 1], "--") != 0) 1537c478bd9Sstevel@tonic-gate optind++; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate argc -= optind; 1567c478bd9Sstevel@tonic-gate argv = &argv[optind]; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if ((argc < 1 && !silent) || errflg) { 1597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1607c478bd9Sstevel@tonic-gate gettext("usage: rm [-fiRr] file ...\n")); 1617c478bd9Sstevel@tonic-gate exit(2); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rl)) { 1657c478bd9Sstevel@tonic-gate perror("getrlimit"); 1667c478bd9Sstevel@tonic-gate exit(2); 1677c478bd9Sstevel@tonic-gate } else 1687c478bd9Sstevel@tonic-gate maxfiles = rl.rlim_cur - 2; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate while (argc-- > 0) { 1717c478bd9Sstevel@tonic-gate rm(*argv, 1); 1727c478bd9Sstevel@tonic-gate argv++; 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate cleanup(); 1767c478bd9Sstevel@tonic-gate return (errcode ? 2 : 0); 1777c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate static void 1817c478bd9Sstevel@tonic-gate rm(char *path, int first) 1827c478bd9Sstevel@tonic-gate { 1837c478bd9Sstevel@tonic-gate struct stat buffer; 1847c478bd9Sstevel@tonic-gate char *filepath; 1857c478bd9Sstevel@tonic-gate char *p; 1867c478bd9Sstevel@tonic-gate char resolved_path[PATH_MAX]; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /* 1897c478bd9Sstevel@tonic-gate * Check file to see if it exists. 1907c478bd9Sstevel@tonic-gate */ 1917c478bd9Sstevel@tonic-gate if (lstat(path, &buffer) == FAIL) { 1927c478bd9Sstevel@tonic-gate if (!silent) { 1937c478bd9Sstevel@tonic-gate perror(path); 1947c478bd9Sstevel@tonic-gate ++errcode; 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate return; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* prevent removal of / but allow removal of sym-links */ 2007c478bd9Sstevel@tonic-gate if (!S_ISLNK(buffer.st_mode) && realpath(path, resolved_path) != NULL && 2017c478bd9Sstevel@tonic-gate strcmp(resolved_path, "/") == 0) { 2027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2037c478bd9Sstevel@tonic-gate gettext("rm of %s is not allowed\n"), resolved_path); 2047c478bd9Sstevel@tonic-gate errcode++; 2057c478bd9Sstevel@tonic-gate return; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* prevent removal of . or .. (directly) */ 2097c478bd9Sstevel@tonic-gate if (p = strrchr(path, '/')) 2107c478bd9Sstevel@tonic-gate p++; 2117c478bd9Sstevel@tonic-gate else 2127c478bd9Sstevel@tonic-gate p = path; 2137c478bd9Sstevel@tonic-gate if (strcmp(".", p) == 0 || strcmp("..", p) == 0) { 2147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2157c478bd9Sstevel@tonic-gate gettext("rm of %s is not allowed\n"), path); 2167c478bd9Sstevel@tonic-gate errcode++; 2177c478bd9Sstevel@tonic-gate return; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate /* 2207c478bd9Sstevel@tonic-gate * If it's a directory, remove its contents. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate if (DIRECTORY) { 2237c478bd9Sstevel@tonic-gate /* 2247c478bd9Sstevel@tonic-gate * If "-r" wasn't specified, trying to remove directories 2257c478bd9Sstevel@tonic-gate * is an error. 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate if (!recursive) { 2287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2297c478bd9Sstevel@tonic-gate gettext("rm: %s is a directory\n"), path); 2307c478bd9Sstevel@tonic-gate ++errcode; 2317c478bd9Sstevel@tonic-gate return; 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate if (first_dir) { 2357c478bd9Sstevel@tonic-gate check_homedir(); 2367c478bd9Sstevel@tonic-gate first_dir = 0; 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate undir(path, first, buffer.st_dev, buffer.st_ino); 2407c478bd9Sstevel@tonic-gate return; 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate filepath = get_filename(path); 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate /* 2467c478bd9Sstevel@tonic-gate * If interactive, ask for acknowledgement. 2477c478bd9Sstevel@tonic-gate * 2487c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE - The following message will contain the 2497c478bd9Sstevel@tonic-gate * first character of the strings for "yes" and "no" defined 2507c478bd9Sstevel@tonic-gate * in the file "nl_langinfo.po". After substitution, the 2517c478bd9Sstevel@tonic-gate * message will appear as follows: 2527c478bd9Sstevel@tonic-gate * rm: remove <filename> (y/n)? 2537c478bd9Sstevel@tonic-gate * For example, in German, this will appear as 2547c478bd9Sstevel@tonic-gate * rm: l�schen <filename> (j/n)? 2557c478bd9Sstevel@tonic-gate * where j=ja, n=nein, <filename>=the file to be removed 2567c478bd9Sstevel@tonic-gate * 2577c478bd9Sstevel@tonic-gate */ 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate if (interactive) { 2617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("rm: remove %s (%s/%s)? "), 2627c478bd9Sstevel@tonic-gate filepath, yeschr, nochr); 2637c478bd9Sstevel@tonic-gate if (!yes()) { 2647c478bd9Sstevel@tonic-gate free(filepath); 2657c478bd9Sstevel@tonic-gate return; 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate } else if (!silent) { 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * If not silent, and stdin is a terminal, and there's 2707c478bd9Sstevel@tonic-gate * no write access, and the file isn't a symbolic link, 2717c478bd9Sstevel@tonic-gate * ask for permission. 2727c478bd9Sstevel@tonic-gate * 2737c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE - The following message will contain the 2747c478bd9Sstevel@tonic-gate * first character of the strings for "yes" and "no" defined 2757c478bd9Sstevel@tonic-gate * in the file "nl_langinfo.po". After substitution, the 2767c478bd9Sstevel@tonic-gate * message will appear as follows: 2777c478bd9Sstevel@tonic-gate * rm: <filename>: override protection XXX (y/n)? 2787c478bd9Sstevel@tonic-gate * where XXX is the permission mode bits of the file in octal 2797c478bd9Sstevel@tonic-gate * and <filename> is the file to be removed 2807c478bd9Sstevel@tonic-gate * 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate if (!SYMLINK && access(path, W_OK) == FAIL && 2837c478bd9Sstevel@tonic-gate isatty(fileno(stdin))) { 2847c478bd9Sstevel@tonic-gate (void) printf( 2857c478bd9Sstevel@tonic-gate gettext("rm: %s: override protection %o (%s/%s)? "), 2867c478bd9Sstevel@tonic-gate filepath, buffer.st_mode & 0777, yeschr, nochr); 2877c478bd9Sstevel@tonic-gate /* 2887c478bd9Sstevel@tonic-gate * If permission isn't given, skip the file. 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate if (!yes()) { 2917c478bd9Sstevel@tonic-gate free(filepath); 2927c478bd9Sstevel@tonic-gate return; 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /* 2987c478bd9Sstevel@tonic-gate * If the unlink fails, inform the user. For /usr/bin/rm, only inform 2997c478bd9Sstevel@tonic-gate * the user if interactive or not silent. 3007c478bd9Sstevel@tonic-gate * If unlink fails with errno = ENOENT because file was removed 3017c478bd9Sstevel@tonic-gate * in between the lstat call and unlink don't inform the user and 3027c478bd9Sstevel@tonic-gate * don't change errcode. 3037c478bd9Sstevel@tonic-gate */ 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate if (unlink(path) == FAIL) { 3067c478bd9Sstevel@tonic-gate if (errno == ENOENT) { 3077c478bd9Sstevel@tonic-gate free(filepath); 3087c478bd9Sstevel@tonic-gate return; 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate #ifndef XPG4 3117c478bd9Sstevel@tonic-gate if (!silent || interactive) { 3127c478bd9Sstevel@tonic-gate #endif 3137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3147c478bd9Sstevel@tonic-gate gettext("rm: %s not removed: "), filepath); 3157c478bd9Sstevel@tonic-gate perror(""); 3167c478bd9Sstevel@tonic-gate #ifndef XPG4 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate #endif 3197c478bd9Sstevel@tonic-gate ++errcode; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate free(filepath); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate static void 3267c478bd9Sstevel@tonic-gate undir(char *path, int first, dev_t dev, ino_t ino) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate char *newpath; 3297c478bd9Sstevel@tonic-gate DIR *name; 3307c478bd9Sstevel@tonic-gate struct dirent *direct; 3317c478bd9Sstevel@tonic-gate int ismypath; 3327c478bd9Sstevel@tonic-gate int chdir_failed = 0; 3337c478bd9Sstevel@tonic-gate size_t len; 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate push_name(path, first); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate /* 3387c478bd9Sstevel@tonic-gate * If interactive and this file isn't in the path of the 3397c478bd9Sstevel@tonic-gate * current working directory, ask for acknowledgement. 3407c478bd9Sstevel@tonic-gate * 3417c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE - The following message will contain the 3427c478bd9Sstevel@tonic-gate * first character of the strings for "yes" and "no" defined 3437c478bd9Sstevel@tonic-gate * in the file "nl_langinfo.po". After substitution, the 3447c478bd9Sstevel@tonic-gate * message will appear as follows: 3457c478bd9Sstevel@tonic-gate * rm: examine files in directory <directoryname> (y/n)? 3467c478bd9Sstevel@tonic-gate * where <directoryname> is the directory to be removed 3477c478bd9Sstevel@tonic-gate * 3487c478bd9Sstevel@tonic-gate */ 3497c478bd9Sstevel@tonic-gate ismypath = mypath(dev, ino); 3507c478bd9Sstevel@tonic-gate if (interactive) { 3517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3527c478bd9Sstevel@tonic-gate gettext("rm: examine files in directory %s (%s/%s)? "), 3537c478bd9Sstevel@tonic-gate fullpath, yeschr, nochr); 3547c478bd9Sstevel@tonic-gate /* 3557c478bd9Sstevel@tonic-gate * If the answer is no, skip the directory. 3567c478bd9Sstevel@tonic-gate */ 3577c478bd9Sstevel@tonic-gate if (!yes()) { 3587c478bd9Sstevel@tonic-gate pop_name(first); 3597c478bd9Sstevel@tonic-gate return; 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate #ifdef XPG4 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * XCU4 and POSIX.2: If not interactive and file is not in the 3667c478bd9Sstevel@tonic-gate * path of the current working directory, check to see whether 3677c478bd9Sstevel@tonic-gate * or not directory is readable or writable and if not, 3687c478bd9Sstevel@tonic-gate * prompt user for response. 3697c478bd9Sstevel@tonic-gate */ 3707c478bd9Sstevel@tonic-gate if (!interactive && !ismypath && 3717c478bd9Sstevel@tonic-gate (access(path, W_OK|X_OK) == FAIL) && isatty(fileno(stdin))) { 3727c478bd9Sstevel@tonic-gate if (!silent) { 3737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3747c478bd9Sstevel@tonic-gate gettext( 3757c478bd9Sstevel@tonic-gate "rm: examine files in directory %s (%s/%s)? "), 3767c478bd9Sstevel@tonic-gate fullpath, yeschr, nochr); 3777c478bd9Sstevel@tonic-gate /* 3787c478bd9Sstevel@tonic-gate * If the answer is no, skip the directory. 3797c478bd9Sstevel@tonic-gate */ 3807c478bd9Sstevel@tonic-gate if (!yes()) { 3817c478bd9Sstevel@tonic-gate pop_name(first); 3827c478bd9Sstevel@tonic-gate return; 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate #endif 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate /* 3897c478bd9Sstevel@tonic-gate * Open the directory for reading. 3907c478bd9Sstevel@tonic-gate */ 3917c478bd9Sstevel@tonic-gate if ((name = opendir(path)) == NULL) { 3927c478bd9Sstevel@tonic-gate int saveerrno = errno; 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * If interactive, ask for acknowledgement. 3967c478bd9Sstevel@tonic-gate */ 3977c478bd9Sstevel@tonic-gate if (interactive) { 3987c478bd9Sstevel@tonic-gate /* 3997c478bd9Sstevel@tonic-gate * Print an error message that 4007c478bd9Sstevel@tonic-gate * we could not read the directory 4017c478bd9Sstevel@tonic-gate * as the user wanted to examine 4027c478bd9Sstevel@tonic-gate * files in the directory. Only 4037c478bd9Sstevel@tonic-gate * affect the error status if 4047c478bd9Sstevel@tonic-gate * user doesn't want to remove the 4057c478bd9Sstevel@tonic-gate * directory as we still may be able 4067c478bd9Sstevel@tonic-gate * remove the directory successfully. 4077c478bd9Sstevel@tonic-gate */ 4087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4097c478bd9Sstevel@tonic-gate "rm: cannot read directory %s: "), 4107c478bd9Sstevel@tonic-gate fullpath); 4117c478bd9Sstevel@tonic-gate errno = saveerrno; 4127c478bd9Sstevel@tonic-gate perror(""); 4137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4147c478bd9Sstevel@tonic-gate "rm: remove %s: (%s/%s)? "), 4157c478bd9Sstevel@tonic-gate fullpath, yeschr, nochr); 4167c478bd9Sstevel@tonic-gate if (!yes()) { 4177c478bd9Sstevel@tonic-gate ++errcode; 4187c478bd9Sstevel@tonic-gate pop_name(first); 4197c478bd9Sstevel@tonic-gate return; 4207c478bd9Sstevel@tonic-gate } 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate /* 4247c478bd9Sstevel@tonic-gate * If the directory is empty, we may be able to 4257c478bd9Sstevel@tonic-gate * go ahead and remove it. 4267c478bd9Sstevel@tonic-gate */ 4277c478bd9Sstevel@tonic-gate if (rmdir(path) == FAIL) { 4287c478bd9Sstevel@tonic-gate if (interactive) { 4297c478bd9Sstevel@tonic-gate int rmdirerr = errno; 4307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4317c478bd9Sstevel@tonic-gate "rm: Unable to remove directory %s: "), 4327c478bd9Sstevel@tonic-gate fullpath); 4337c478bd9Sstevel@tonic-gate errno = rmdirerr; 4347c478bd9Sstevel@tonic-gate perror(""); 4357c478bd9Sstevel@tonic-gate } else { 4367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4377c478bd9Sstevel@tonic-gate "rm: cannot read directory %s: "), 4387c478bd9Sstevel@tonic-gate fullpath); 4397c478bd9Sstevel@tonic-gate errno = saveerrno; 4407c478bd9Sstevel@tonic-gate perror(""); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate ++errcode; 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate /* Continue to next file/directory rather than exit */ 4467c478bd9Sstevel@tonic-gate pop_name(first); 4477c478bd9Sstevel@tonic-gate return; 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate /* 4517c478bd9Sstevel@tonic-gate * XCU4 requires that rm -r descend the directory 4527c478bd9Sstevel@tonic-gate * hierarchy without regard to PATH_MAX. If we can't 4537c478bd9Sstevel@tonic-gate * chdir() do not increment error counter and do not 4547c478bd9Sstevel@tonic-gate * print message. 4557c478bd9Sstevel@tonic-gate * 4567c478bd9Sstevel@tonic-gate * However, if we cannot chdir because someone has taken away 4577c478bd9Sstevel@tonic-gate * execute access we may still be able to delete the directory 4587c478bd9Sstevel@tonic-gate * if it's empty. The old rm could do this. 4597c478bd9Sstevel@tonic-gate */ 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate if (chdir(path) == -1) { 4627c478bd9Sstevel@tonic-gate chdir_failed = 1; 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate /* 4667c478bd9Sstevel@tonic-gate * Read every directory entry. 4677c478bd9Sstevel@tonic-gate */ 4687c478bd9Sstevel@tonic-gate while ((direct = readdir(name)) != NULL) { 4697c478bd9Sstevel@tonic-gate /* 4707c478bd9Sstevel@tonic-gate * Ignore "." and ".." entries. 4717c478bd9Sstevel@tonic-gate */ 4727c478bd9Sstevel@tonic-gate if (strcmp(direct->d_name, ".") == 0 || 4737c478bd9Sstevel@tonic-gate strcmp(direct->d_name, "..") == 0) 4747c478bd9Sstevel@tonic-gate continue; 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * Try to remove the file. 4777c478bd9Sstevel@tonic-gate */ 4787c478bd9Sstevel@tonic-gate len = strlen(direct->d_name) + 1; 4797c478bd9Sstevel@tonic-gate if (chdir_failed) { 4807c478bd9Sstevel@tonic-gate len += strlen(path) + 2; 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate 4837c478bd9Sstevel@tonic-gate newpath = malloc(len); 4847c478bd9Sstevel@tonic-gate if (newpath == NULL) { 4857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4867c478bd9Sstevel@tonic-gate gettext("rm: Insufficient memory.\n")); 4877c478bd9Sstevel@tonic-gate cleanup(); 4887c478bd9Sstevel@tonic-gate exit(1); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate if (!chdir_failed) { 4927c478bd9Sstevel@tonic-gate (void) strcpy(newpath, direct->d_name); 4937c478bd9Sstevel@tonic-gate } else { 4947c478bd9Sstevel@tonic-gate (void) snprintf(newpath, len, "%s/%s", 4957c478bd9Sstevel@tonic-gate path, direct->d_name); 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate /* 5007c478bd9Sstevel@tonic-gate * If a spare file descriptor is available, just call the 5017c478bd9Sstevel@tonic-gate * "rm" function with the file name; otherwise close the 5027c478bd9Sstevel@tonic-gate * directory and reopen it when the child is removed. 5037c478bd9Sstevel@tonic-gate */ 5047c478bd9Sstevel@tonic-gate if (name->dd_fd >= maxfiles) { 5057c478bd9Sstevel@tonic-gate (void) closedir(name); 5067c478bd9Sstevel@tonic-gate rm(newpath, 0); 5077c478bd9Sstevel@tonic-gate if (!chdir_failed) 5087c478bd9Sstevel@tonic-gate name = opendir("."); 5097c478bd9Sstevel@tonic-gate else 5107c478bd9Sstevel@tonic-gate name = opendir(path); 5117c478bd9Sstevel@tonic-gate if (name == NULL) { 5127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5137c478bd9Sstevel@tonic-gate gettext("rm: cannot read directory %s: "), 5147c478bd9Sstevel@tonic-gate fullpath); 5157c478bd9Sstevel@tonic-gate perror(""); 5167c478bd9Sstevel@tonic-gate cleanup(); 5177c478bd9Sstevel@tonic-gate exit(2); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate } else 5207c478bd9Sstevel@tonic-gate rm(newpath, 0); 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate free(newpath); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate /* 5267c478bd9Sstevel@tonic-gate * Close the directory we just finished reading. 5277c478bd9Sstevel@tonic-gate */ 5287c478bd9Sstevel@tonic-gate (void) closedir(name); 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate /* 5317c478bd9Sstevel@tonic-gate * The contents of the directory have been removed. If the 5327c478bd9Sstevel@tonic-gate * directory itself is in the path of the current working 5337c478bd9Sstevel@tonic-gate * directory, don't try to remove it. 5347c478bd9Sstevel@tonic-gate * When the directory itself is the current working directory, mypath() 5357c478bd9Sstevel@tonic-gate * has a return code == 2. 5367c478bd9Sstevel@tonic-gate * 5377c478bd9Sstevel@tonic-gate * XCU4: Because we've descended the directory hierarchy in order 5387c478bd9Sstevel@tonic-gate * to avoid PATH_MAX limitation, we must now start ascending 5397c478bd9Sstevel@tonic-gate * one level at a time and remove files/directories. 5407c478bd9Sstevel@tonic-gate */ 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate if (!chdir_failed) { 5437c478bd9Sstevel@tonic-gate if (first) 5447c478bd9Sstevel@tonic-gate chdir_home(); 5457c478bd9Sstevel@tonic-gate else if (chdir("..") == -1) { 5467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5477c478bd9Sstevel@tonic-gate gettext("rm: cannot change to parent of " 5487c478bd9Sstevel@tonic-gate "directory %s: "), 5497c478bd9Sstevel@tonic-gate fullpath); 5507c478bd9Sstevel@tonic-gate perror(""); 5517c478bd9Sstevel@tonic-gate cleanup(); 5527c478bd9Sstevel@tonic-gate exit(2); 5537c478bd9Sstevel@tonic-gate } 5547c478bd9Sstevel@tonic-gate } 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate switch (ismypath) { 5577c478bd9Sstevel@tonic-gate case 3: 5587c478bd9Sstevel@tonic-gate pop_name(first); 5597c478bd9Sstevel@tonic-gate return; 5607c478bd9Sstevel@tonic-gate case 2: 5617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5627c478bd9Sstevel@tonic-gate gettext("rm: Cannot remove any directory in the path " 5637c478bd9Sstevel@tonic-gate "of the current working directory\n%s\n"), fullpath); 5647c478bd9Sstevel@tonic-gate ++errcode; 5657c478bd9Sstevel@tonic-gate pop_name(first); 5667c478bd9Sstevel@tonic-gate return; 5677c478bd9Sstevel@tonic-gate case 1: 5687c478bd9Sstevel@tonic-gate ++errcode; 5697c478bd9Sstevel@tonic-gate pop_name(first); 5707c478bd9Sstevel@tonic-gate return; 5717c478bd9Sstevel@tonic-gate case 0: 5727c478bd9Sstevel@tonic-gate break; 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate /* 5767c478bd9Sstevel@tonic-gate * If interactive, ask for acknowledgement. 5777c478bd9Sstevel@tonic-gate */ 5787c478bd9Sstevel@tonic-gate if (interactive) { 5797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("rm: remove %s: (%s/%s)? "), 5807c478bd9Sstevel@tonic-gate fullpath, yeschr, nochr); 5817c478bd9Sstevel@tonic-gate if (!yes()) { 5827c478bd9Sstevel@tonic-gate pop_name(first); 5837c478bd9Sstevel@tonic-gate return; 5847c478bd9Sstevel@tonic-gate } 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate if (rmdir(path) == FAIL) { 5877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5887c478bd9Sstevel@tonic-gate gettext("rm: Unable to remove directory %s: "), 5897c478bd9Sstevel@tonic-gate fullpath); 5907c478bd9Sstevel@tonic-gate perror(""); 5917c478bd9Sstevel@tonic-gate ++errcode; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate pop_name(first); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate static int 5987c478bd9Sstevel@tonic-gate yes(void) 5997c478bd9Sstevel@tonic-gate { 6007c478bd9Sstevel@tonic-gate int i, b; 6017c478bd9Sstevel@tonic-gate char ans[SCHAR_MAX + 1]; 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate for (i = 0; ; i++) { 6047c478bd9Sstevel@tonic-gate b = getchar(); 6057c478bd9Sstevel@tonic-gate if (b == '\n' || b == '\0' || b == EOF) { 6067c478bd9Sstevel@tonic-gate ans[i] = 0; 6077c478bd9Sstevel@tonic-gate break; 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate if (i < SCHAR_MAX) 6107c478bd9Sstevel@tonic-gate ans[i] = b; 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate if (i >= SCHAR_MAX) { 6137c478bd9Sstevel@tonic-gate i = SCHAR_MAX; 6147c478bd9Sstevel@tonic-gate ans[SCHAR_MAX] = 0; 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate if ((i == 0) | (strncmp(yeschr, ans, i))) 6177c478bd9Sstevel@tonic-gate return (0); 6187c478bd9Sstevel@tonic-gate return (1); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate static int 6237c478bd9Sstevel@tonic-gate mypath(dev_t dev, ino_t ino) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate struct dir_id *curdir; 6267c478bd9Sstevel@tonic-gate 6277c478bd9Sstevel@tonic-gate /* 6287c478bd9Sstevel@tonic-gate * Check to see if this is our current directory 6297c478bd9Sstevel@tonic-gate * Indicated by return 2; 6307c478bd9Sstevel@tonic-gate */ 6317c478bd9Sstevel@tonic-gate if (dev == homedir.dev && ino == homedir.inode) { 6327c478bd9Sstevel@tonic-gate return (2); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate curdir = homedir.next; 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate while (curdir != NULL) { 6387c478bd9Sstevel@tonic-gate /* 6397c478bd9Sstevel@tonic-gate * If we find a match, the directory (dev, ino) passed to 6407c478bd9Sstevel@tonic-gate * mypath() is an ancestor of ours. Indicated by return 3. 6417c478bd9Sstevel@tonic-gate */ 6427c478bd9Sstevel@tonic-gate if (curdir->dev == dev && curdir->inode == ino) 6437c478bd9Sstevel@tonic-gate return (3); 6447c478bd9Sstevel@tonic-gate curdir = curdir->next; 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate /* 6477c478bd9Sstevel@tonic-gate * parent_err indicates we couldn't stat or chdir to 6487c478bd9Sstevel@tonic-gate * one of our parent dirs, so the linked list of dir_id structs 6497c478bd9Sstevel@tonic-gate * is incomplete 6507c478bd9Sstevel@tonic-gate */ 6517c478bd9Sstevel@tonic-gate if (parent_err) { 6527c478bd9Sstevel@tonic-gate #ifndef XPG4 6537c478bd9Sstevel@tonic-gate if (!silent || interactive) { 6547c478bd9Sstevel@tonic-gate #endif 6557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("rm: cannot determine " 6567c478bd9Sstevel@tonic-gate "if this is an ancestor of the current " 6577c478bd9Sstevel@tonic-gate "working directory\n%s\n"), fullpath); 6587c478bd9Sstevel@tonic-gate #ifndef XPG4 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate #endif 6617c478bd9Sstevel@tonic-gate /* assume it is. least dangerous */ 6627c478bd9Sstevel@tonic-gate return (1); 6637c478bd9Sstevel@tonic-gate } 6647c478bd9Sstevel@tonic-gate return (0); 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate 6677c478bd9Sstevel@tonic-gate static int maxlen; 6687c478bd9Sstevel@tonic-gate static int curlen; 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate static char * 6717c478bd9Sstevel@tonic-gate get_filename(char *name) 6727c478bd9Sstevel@tonic-gate { 6737c478bd9Sstevel@tonic-gate char *path; 6747c478bd9Sstevel@tonic-gate size_t len; 6757c478bd9Sstevel@tonic-gate 6767c478bd9Sstevel@tonic-gate if (fullpath == NULL || *fullpath == '\0') { 6777c478bd9Sstevel@tonic-gate path = strdup(name); 6787c478bd9Sstevel@tonic-gate if (path == NULL) { 6797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6807c478bd9Sstevel@tonic-gate gettext("rm: Insufficient memory.\n")); 6817c478bd9Sstevel@tonic-gate cleanup(); 6827c478bd9Sstevel@tonic-gate exit(1); 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate } else { 6857c478bd9Sstevel@tonic-gate len = strlen(fullpath) + strlen(name) + 2; 6867c478bd9Sstevel@tonic-gate path = malloc(len); 6877c478bd9Sstevel@tonic-gate if (path == NULL) { 6887c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6897c478bd9Sstevel@tonic-gate gettext("rm: Insufficient memory.\n")); 6907c478bd9Sstevel@tonic-gate cleanup(); 6917c478bd9Sstevel@tonic-gate exit(1); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate (void) snprintf(path, len, "%s/%s", fullpath, name); 6947c478bd9Sstevel@tonic-gate } 6957c478bd9Sstevel@tonic-gate return (path); 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate 6987c478bd9Sstevel@tonic-gate static void 6997c478bd9Sstevel@tonic-gate push_name(char *name, int first) 7007c478bd9Sstevel@tonic-gate { 7017c478bd9Sstevel@tonic-gate int namelen; 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate namelen = strlen(name) + 1; /* 1 for "/" */ 7047c478bd9Sstevel@tonic-gate if ((curlen + namelen) >= maxlen) { 7057c478bd9Sstevel@tonic-gate maxlen += PATH_MAX; 7067c478bd9Sstevel@tonic-gate fullpath = (char *)realloc(fullpath, (size_t)(maxlen + 1)); 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate if (first) { 7097c478bd9Sstevel@tonic-gate (void) strcpy(fullpath, name); 7107c478bd9Sstevel@tonic-gate } else { 7117c478bd9Sstevel@tonic-gate (void) strcat(fullpath, "/"); 7127c478bd9Sstevel@tonic-gate (void) strcat(fullpath, name); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate curlen = strlen(fullpath); 7157c478bd9Sstevel@tonic-gate } 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate static void 7187c478bd9Sstevel@tonic-gate pop_name(int first) 7197c478bd9Sstevel@tonic-gate { 7207c478bd9Sstevel@tonic-gate char *slash; 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate if (first) { 7237c478bd9Sstevel@tonic-gate *fullpath = '\0'; 7247c478bd9Sstevel@tonic-gate return; 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate slash = strrchr(fullpath, '/'); 7277c478bd9Sstevel@tonic-gate if (slash) 7287c478bd9Sstevel@tonic-gate *slash = '\0'; 7297c478bd9Sstevel@tonic-gate else 7307c478bd9Sstevel@tonic-gate *fullpath = '\0'; 7317c478bd9Sstevel@tonic-gate curlen = strlen(fullpath); 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate static void 7357c478bd9Sstevel@tonic-gate force_chdir(char *dirname) 7367c478bd9Sstevel@tonic-gate { 7377c478bd9Sstevel@tonic-gate char *pathname, *mp, *tp; 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate /* use pathname instead of dirname, so dirname won't be modified */ 7407c478bd9Sstevel@tonic-gate if ((pathname = strdup(dirname)) == NULL) { 7417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("rm: strdup: ")); 7427c478bd9Sstevel@tonic-gate perror(""); 7437c478bd9Sstevel@tonic-gate cleanup(); 7447c478bd9Sstevel@tonic-gate exit(2); 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate /* pathname is an absolute full path from getcwd() */ 7487c478bd9Sstevel@tonic-gate mp = pathname; 7497c478bd9Sstevel@tonic-gate while (mp) { 7507c478bd9Sstevel@tonic-gate tp = strchr(mp, '/'); 7517c478bd9Sstevel@tonic-gate if (strlen(mp) >= PATH_MAX) { 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate * after the first iteration through this 7547c478bd9Sstevel@tonic-gate * loop, the below will NULL out the '/' 7557c478bd9Sstevel@tonic-gate * which follows the first dir on pathname 7567c478bd9Sstevel@tonic-gate */ 7577c478bd9Sstevel@tonic-gate *tp = 0; 7587c478bd9Sstevel@tonic-gate tp++; 7597c478bd9Sstevel@tonic-gate if (*mp == NULL) 7607c478bd9Sstevel@tonic-gate ch_dir("/"); 7617c478bd9Sstevel@tonic-gate else 7627c478bd9Sstevel@tonic-gate /* 7637c478bd9Sstevel@tonic-gate * mp points to the start of a dirname, 7647c478bd9Sstevel@tonic-gate * terminated by NULL, so ch_dir() 7657c478bd9Sstevel@tonic-gate * here will move down one directory 7667c478bd9Sstevel@tonic-gate */ 7677c478bd9Sstevel@tonic-gate ch_dir(mp); 7687c478bd9Sstevel@tonic-gate /* 7697c478bd9Sstevel@tonic-gate * reset mp to the start of the dirname 7707c478bd9Sstevel@tonic-gate * which follows the one we just chdir'd to 7717c478bd9Sstevel@tonic-gate */ 7727c478bd9Sstevel@tonic-gate mp = tp; 7737c478bd9Sstevel@tonic-gate continue; /* probably can remove this */ 7747c478bd9Sstevel@tonic-gate } else { 7757c478bd9Sstevel@tonic-gate ch_dir(mp); 7767c478bd9Sstevel@tonic-gate break; 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate } 7797c478bd9Sstevel@tonic-gate free(pathname); 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate static void 7837c478bd9Sstevel@tonic-gate ch_dir(char *dirname) 7847c478bd9Sstevel@tonic-gate { 7857c478bd9Sstevel@tonic-gate if (chdir(dirname) == -1) { 7867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7877c478bd9Sstevel@tonic-gate gettext("rm: cannot change to %s directory: "), dirname); 7887c478bd9Sstevel@tonic-gate perror(""); 7897c478bd9Sstevel@tonic-gate cleanup(); 7907c478bd9Sstevel@tonic-gate exit(2); 7917c478bd9Sstevel@tonic-gate } 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate static void 7957c478bd9Sstevel@tonic-gate chdir_home(void) 7967c478bd9Sstevel@tonic-gate { 7977c478bd9Sstevel@tonic-gate /* 7987c478bd9Sstevel@tonic-gate * Go back to home dir--the dir from where rm was executed--using 7997c478bd9Sstevel@tonic-gate * one of two methods, depending on which method works 8007c478bd9Sstevel@tonic-gate * for the given permissions of the home dir and its 8017c478bd9Sstevel@tonic-gate * parent directories. 8027c478bd9Sstevel@tonic-gate */ 8037c478bd9Sstevel@tonic-gate if (homedirfd != -1) { 8047c478bd9Sstevel@tonic-gate if (fchdir(homedirfd) == -1) { 8057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8067c478bd9Sstevel@tonic-gate gettext("rm: cannot change to starting " 8077c478bd9Sstevel@tonic-gate "directory: ")); 8087c478bd9Sstevel@tonic-gate perror(""); 8097c478bd9Sstevel@tonic-gate cleanup(); 8107c478bd9Sstevel@tonic-gate exit(2); 8117c478bd9Sstevel@tonic-gate } 8127c478bd9Sstevel@tonic-gate } else { 8137c478bd9Sstevel@tonic-gate if (strlen(cwd) < PATH_MAX) 8147c478bd9Sstevel@tonic-gate ch_dir(cwd); 8157c478bd9Sstevel@tonic-gate else 8167c478bd9Sstevel@tonic-gate force_chdir(cwd); 8177c478bd9Sstevel@tonic-gate } 8187c478bd9Sstevel@tonic-gate } 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate /* 8217c478bd9Sstevel@tonic-gate * check_homedir - 8227c478bd9Sstevel@tonic-gate * is only called the first time rm tries to 8237c478bd9Sstevel@tonic-gate * remove a directory. It saves the current directory, i.e., 8247c478bd9Sstevel@tonic-gate * home dir, so we can go back to it after traversing elsewhere. 8257c478bd9Sstevel@tonic-gate * It also saves all the device and inode numbers of each 8267c478bd9Sstevel@tonic-gate * dir from the home dir back to the root in a linked list, so we 8277c478bd9Sstevel@tonic-gate * can later check, via mypath(), if we are trying to remove our current 8287c478bd9Sstevel@tonic-gate * dir or an ancestor. 8297c478bd9Sstevel@tonic-gate */ 8307c478bd9Sstevel@tonic-gate static void 8317c478bd9Sstevel@tonic-gate check_homedir(void) 8327c478bd9Sstevel@tonic-gate { 8337c478bd9Sstevel@tonic-gate int size; /* size allocated for pathname of home dir (cwd) */ 8347c478bd9Sstevel@tonic-gate struct stat buffer; 8357c478bd9Sstevel@tonic-gate struct dir_id *lastdir, *curdir; 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate /* 8387c478bd9Sstevel@tonic-gate * We need to save where we currently are (the "home dir") so 8397c478bd9Sstevel@tonic-gate * we can return after traversing down directories we're 8407c478bd9Sstevel@tonic-gate * removing. Two methods are attempted: 8417c478bd9Sstevel@tonic-gate * 8427c478bd9Sstevel@tonic-gate * 1) open() the home dir so we can use the fd 8437c478bd9Sstevel@tonic-gate * to fchdir() back. This requires read permission 8447c478bd9Sstevel@tonic-gate * on the home dir. 8457c478bd9Sstevel@tonic-gate * 8467c478bd9Sstevel@tonic-gate * 2) getcwd() so we can chdir() to go back. This 8477c478bd9Sstevel@tonic-gate * requires search (x) permission on the home dir, 8487c478bd9Sstevel@tonic-gate * and read and search permission on all parent dirs. Also, 8497c478bd9Sstevel@tonic-gate * getcwd() will not work if the home dir is > 341 8507c478bd9Sstevel@tonic-gate * directories deep (see open bugid 4033182 - getcwd needs 8517c478bd9Sstevel@tonic-gate * to work for pathnames of any depth). 8527c478bd9Sstevel@tonic-gate * 8537c478bd9Sstevel@tonic-gate * If neither method works, we can't remove any directories 8547c478bd9Sstevel@tonic-gate * and rm will fail. 8557c478bd9Sstevel@tonic-gate * 8567c478bd9Sstevel@tonic-gate * For future enhancement, a possible 3rd option to use 8577c478bd9Sstevel@tonic-gate * would be to fork a process to remove a directory, 8587c478bd9Sstevel@tonic-gate * eliminating the need to chdir back to the home directory 8597c478bd9Sstevel@tonic-gate * and eliminating the permission restrictions on the home dir 8607c478bd9Sstevel@tonic-gate * or its parent dirs. 8617c478bd9Sstevel@tonic-gate */ 8627c478bd9Sstevel@tonic-gate homedirfd = open(".", O_RDONLY); 8637c478bd9Sstevel@tonic-gate if (homedirfd == -1) { 8647c478bd9Sstevel@tonic-gate size = PATH_MAX; 8657c478bd9Sstevel@tonic-gate while ((cwd = getcwd(NULL, size)) == NULL) { 8667c478bd9Sstevel@tonic-gate if (errno == ERANGE) { 8677c478bd9Sstevel@tonic-gate size = PATH_MAX + size; 8687c478bd9Sstevel@tonic-gate continue; 8697c478bd9Sstevel@tonic-gate } else { 8707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8717c478bd9Sstevel@tonic-gate gettext("rm: cannot open starting " 8727c478bd9Sstevel@tonic-gate "directory: ")); 8737c478bd9Sstevel@tonic-gate perror("pwd"); 8747c478bd9Sstevel@tonic-gate exit(2); 8757c478bd9Sstevel@tonic-gate } 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate /* 8807c478bd9Sstevel@tonic-gate * since we exit on error here, we're guaranteed to at least 8817c478bd9Sstevel@tonic-gate * have info in the first dir_id struct, homedir 8827c478bd9Sstevel@tonic-gate */ 8837c478bd9Sstevel@tonic-gate if (stat(".", &buffer) == -1) { 8847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8857c478bd9Sstevel@tonic-gate gettext("rm: cannot stat current directory: ")); 8867c478bd9Sstevel@tonic-gate perror(""); 8877c478bd9Sstevel@tonic-gate exit(2); 8887c478bd9Sstevel@tonic-gate } 8897c478bd9Sstevel@tonic-gate homedir.dev = buffer.st_dev; 8907c478bd9Sstevel@tonic-gate homedir.inode = buffer.st_ino; 8917c478bd9Sstevel@tonic-gate homedir.next = NULL; 8927c478bd9Sstevel@tonic-gate 8937c478bd9Sstevel@tonic-gate lastdir = &homedir; 8947c478bd9Sstevel@tonic-gate /* 8957c478bd9Sstevel@tonic-gate * Starting from current working directory, walk toward the 8967c478bd9Sstevel@tonic-gate * root, looking at each directory along the way. 8977c478bd9Sstevel@tonic-gate */ 8987c478bd9Sstevel@tonic-gate for (;;) { 8997c478bd9Sstevel@tonic-gate if (chdir("..") == -1 || lstat(".", &buffer) == -1) { 9007c478bd9Sstevel@tonic-gate parent_err = 1; 9017c478bd9Sstevel@tonic-gate break; 9027c478bd9Sstevel@tonic-gate } 9037c478bd9Sstevel@tonic-gate 9047c478bd9Sstevel@tonic-gate if ((lastdir->next = malloc(sizeof (struct dir_id))) == 9057c478bd9Sstevel@tonic-gate NULL) { 9067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9077c478bd9Sstevel@tonic-gate gettext("rm: Insufficient memory.\n")); 9087c478bd9Sstevel@tonic-gate cleanup(); 9097c478bd9Sstevel@tonic-gate exit(1); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate curdir = lastdir->next; 9137c478bd9Sstevel@tonic-gate curdir->dev = buffer.st_dev; 9147c478bd9Sstevel@tonic-gate curdir->inode = buffer.st_ino; 9157c478bd9Sstevel@tonic-gate curdir->next = NULL; 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * Stop when we reach the root; note that chdir("..") 9197c478bd9Sstevel@tonic-gate * at the root dir will stay in root. Get rid of 9207c478bd9Sstevel@tonic-gate * the redundant dir_id struct for root. 9217c478bd9Sstevel@tonic-gate */ 9227c478bd9Sstevel@tonic-gate if (curdir->dev == lastdir->dev && curdir->inode == 9237c478bd9Sstevel@tonic-gate lastdir->inode) { 9247c478bd9Sstevel@tonic-gate lastdir->next = NULL; 9257c478bd9Sstevel@tonic-gate free(curdir); 9267c478bd9Sstevel@tonic-gate break; 9277c478bd9Sstevel@tonic-gate } 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate /* loop again to go back another level */ 9307c478bd9Sstevel@tonic-gate lastdir = curdir; 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate /* go back to home directory */ 9337c478bd9Sstevel@tonic-gate chdir_home(); 9347c478bd9Sstevel@tonic-gate } 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate /* 9377c478bd9Sstevel@tonic-gate * cleanup the dynamically-allocated list of device numbers and inodes, 9387c478bd9Sstevel@tonic-gate * if any. If homedir was never used, it is external and static so 9397c478bd9Sstevel@tonic-gate * it is guaranteed initialized to zero, thus homedir.next would be NULL. 9407c478bd9Sstevel@tonic-gate */ 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate static void 9437c478bd9Sstevel@tonic-gate cleanup(void) { 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate struct dir_id *lastdir, *curdir; 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate curdir = homedir.next; 9487c478bd9Sstevel@tonic-gate 9497c478bd9Sstevel@tonic-gate while (curdir != NULL) { 9507c478bd9Sstevel@tonic-gate lastdir = curdir; 9517c478bd9Sstevel@tonic-gate curdir = curdir->next; 9527c478bd9Sstevel@tonic-gate free(lastdir); 9537c478bd9Sstevel@tonic-gate } 9547c478bd9Sstevel@tonic-gate } 955