2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $TOG: remover.C /main/5 1999/10/14 18:37:42 mgreess $
29 * remover.cc - ToolTalk wrapper for rm(1) and rmdir(1).
31 * Copyright (c) 1990 by Sun Microsystems, Inc.
35 #include "tt_options.h"
38 #if defined(linux) || defined(CSRG_BASED)
48 #include <sys/inode.h>
49 #define S_ISLNK(m) (((m)&IFMT) == IFLNK)
53 #include "api/c/tt_c.h"
54 #include "util/tt_path.h"
55 #include "util/tt_gettext.h"
56 #include "util/copyright.h"
70 char *base = strrchr( arg0, '/' );
77 _process_name = _prog_name;
79 _am_rmdir = (_prog_name == "ttrmdir");
80 _args = new _Tt_string_list();
81 _paths = new _Tt_string_list();
94 * remover::do_rm() - Invoke rm[dir](1), and return its exit status.
95 * We can just use _args, since we never get here when our one
96 * incompatible option (-L) has been given.
102 _Tt_string_list_cursor arg_cursor( _args );
109 while (arg_cursor.next()) {
110 cmd = cmd.cat( " " ).cat( *arg_cursor );
112 //printf( "Invoking: %s\n", (char *)cmd );
113 int sys_status = system( (char *)cmd );
114 if (WIFEXITED(sys_status)) {
115 return WEXITSTATUS(sys_status);
118 "%s: system(\"%s\"): %d\n",
119 (char *)_process_name, (char *)cmd, sys_status );
125 * remover::do_ttrm() - Use tt_file_destroy() on the paths to destroy.
130 return this->_ttrm_paths( _paths );
134 * remover::ttrm_path() - tt_file_destroy() this path.
137 _ttrm_paths( _Tt_string_list_ptr paths )
139 Tt_status worst_err = TT_OK;
141 bool_t abort = FALSE;
143 while ((! paths->is_empty()) && (! abort)) {
144 _Tt_string path = paths->top();
145 _Tt_string_list_ptr children;
148 if (! this->can_rm( path )) {
152 _Tt_string_list_ptr children = _tt_dir_entries(path,
154 err = this->_ttrm_paths( children );
155 if (err > TT_WRN_LAST) {
169 err = tt_file_destroy( (char *)path );
170 if (err > TT_WRN_LAST) {
174 catgets(_ttcatd, 8, 7,
175 "%s: Could not remove "
176 "ToolTalk objects of "
178 (char *)_process_name,
180 tt_status_message(err) );
200 * remover::can_rm() - Can we remove this path?
202 * TO_DO: For now I'll count on tt_file_destroy() not to let me
203 * destroy the specs in files I can't remove.
206 can_rm( _Tt_string path )
211 struct stat lstat_buf;
213 if (lstat( (char *)path, &lstat_buf) == 0) {
214 if (S_ISDIR(lstat_buf.st_mode)) {
216 * rm(1) without the -r flag
217 * won't remove directories,
218 * but tt_file_destroy() will,
219 * so we make sure not to ask
223 fprintf( stderr, "%s: %s: %s\n",
229 } else if (S_ISLNK(lstat_buf.st_mode)) {
231 * Don't tt_file_destroy() a symlink,
232 * or TT will tt_file_destroy() the
253 char *process_id = tt_open();
254 Tt_status err = tt_ptr_error( process_id );
256 _process_id = process_id;
258 } else if (err > TT_WRN_LAST) {
260 "%s: tt_open(): %s\n",
261 (char *)_process_name, tt_status_message(err) );
267 * remover::close_tt()
275 Tt_status err = tt_close();
276 if (err > TT_WRN_LAST) {
278 "%s: tt_close(): %s\n",
279 (char *)_process_name, tt_status_message(err) );
285 * remover::parse_args()
288 parse_args( int argc, char **argv )
290 bool_t no_more_options = FALSE;
292 for ( int arg_num = 1; arg_num < argc; arg_num++ ) {
293 _Tt_string arg( argv[arg_num] );
294 _args->append( arg );
297 this->_parse_arg( (char *)arg );
299 _paths->append( arg );
302 if ((arg[0] != '-') || no_more_options) {
303 _paths->append( arg );
305 if (arg[1] == '\0') {
307 * The bare option "-" means take the
308 * subsequent arguments to be paths.
310 no_more_options = TRUE;
312 this->_parse_arg( (char *)arg );
317 if (_paths->count() <= 0) {
324 * remover::_parse_arg() - Parse an option
327 _parse_arg( char *arg )
333 while (arg[++n] != '\0') {
362 _TT_PRINT_VERSIONS((char *)_prog_name)
377 usage(FILE *fs) const
380 catgets(_ttcatd, 8, 8,
381 "Usage: %s [-] [-%s] %s ...\n"
385 (_am_rmdir ? "L" : "fLrR"),
386 (_am_rmdir ? catgets(_ttcatd, 8, 9, "file")
387 : catgets(_ttcatd, 8, 10, "dir") ),
388 (char *)_prog_name, (char *)_prog_name );
390 catgets(_ttcatd, 8, 11,
391 "\t-L do not perform a %s(1)\n"
392 "\t-v print the version number and quit\n"
393 "\t-h[elp] print this message\n" ),
394 (_am_rmdir ? "rmdir" : "rm"));