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)
49 #include "api/c/tt_c.h"
50 #include "util/tt_path.h"
51 #include "util/tt_gettext.h"
52 #include "util/copyright.h"
66 char *base = strrchr( arg0, '/' );
73 _process_name = _prog_name;
75 _am_rmdir = (_prog_name == "ttrmdir");
76 _args = new _Tt_string_list();
77 _paths = new _Tt_string_list();
90 * remover::do_rm() - Invoke rm[dir](1), and return its exit status.
91 * We can just use _args, since we never get here when our one
92 * incompatible option (-L) has been given.
98 _Tt_string_list_cursor arg_cursor( _args );
105 while (arg_cursor.next()) {
106 cmd = cmd.cat( " " ).cat( *arg_cursor );
108 //printf( "Invoking: %s\n", (char *)cmd );
109 int sys_status = system( (char *)cmd );
110 if (WIFEXITED(sys_status)) {
111 return WEXITSTATUS(sys_status);
114 "%s: system(\"%s\"): %d\n",
115 (char *)_process_name, (char *)cmd, sys_status );
121 * remover::do_ttrm() - Use tt_file_destroy() on the paths to destroy.
126 return this->_ttrm_paths( _paths );
130 * remover::ttrm_path() - tt_file_destroy() this path.
133 _ttrm_paths( _Tt_string_list_ptr paths )
135 Tt_status worst_err = TT_OK;
137 bool_t abort = FALSE;
139 while ((! paths->is_empty()) && (! abort)) {
140 _Tt_string path = paths->top();
141 _Tt_string_list_ptr children;
144 if (! this->can_rm( path )) {
148 _Tt_string_list_ptr children = _tt_dir_entries(path,
150 err = this->_ttrm_paths( children );
151 if (err > TT_WRN_LAST) {
165 err = tt_file_destroy( (char *)path );
166 if (err > TT_WRN_LAST) {
170 catgets(_ttcatd, 8, 7,
171 "%s: Could not remove "
172 "ToolTalk objects of "
174 (char *)_process_name,
176 tt_status_message(err) );
196 * remover::can_rm() - Can we remove this path?
198 * TO_DO: For now I'll count on tt_file_destroy() not to let me
199 * destroy the specs in files I can't remove.
202 can_rm( _Tt_string path )
207 struct stat lstat_buf;
209 if (lstat( (char *)path, &lstat_buf) == 0) {
210 if (S_ISDIR(lstat_buf.st_mode)) {
212 * rm(1) without the -r flag
213 * won't remove directories,
214 * but tt_file_destroy() will,
215 * so we make sure not to ask
219 fprintf( stderr, "%s: %s: %s\n",
225 } else if (S_ISLNK(lstat_buf.st_mode)) {
227 * Don't tt_file_destroy() a symlink,
228 * or TT will tt_file_destroy() the
249 char *process_id = tt_open();
250 Tt_status err = tt_ptr_error( process_id );
252 _process_id = process_id;
254 } else if (err > TT_WRN_LAST) {
256 "%s: tt_open(): %s\n",
257 (char *)_process_name, tt_status_message(err) );
263 * remover::close_tt()
271 Tt_status err = tt_close();
272 if (err > TT_WRN_LAST) {
274 "%s: tt_close(): %s\n",
275 (char *)_process_name, tt_status_message(err) );
281 * remover::parse_args()
284 parse_args( int argc, char **argv )
286 bool_t no_more_options = FALSE;
288 for ( int arg_num = 1; arg_num < argc; arg_num++ ) {
289 _Tt_string arg( argv[arg_num] );
290 _args->append( arg );
293 this->_parse_arg( (char *)arg );
295 _paths->append( arg );
298 if ((arg[0] != '-') || no_more_options) {
299 _paths->append( arg );
301 if (arg[1] == '\0') {
303 * The bare option "-" means take the
304 * subsequent arguments to be paths.
306 no_more_options = TRUE;
308 this->_parse_arg( (char *)arg );
313 if (_paths->count() <= 0) {
320 * remover::_parse_arg() - Parse an option
323 _parse_arg( char *arg )
329 while (arg[++n] != '\0') {
358 _TT_PRINT_VERSIONS((char *)_prog_name)
373 usage(FILE *fs) const
376 catgets(_ttcatd, 8, 8,
377 "Usage: %s [-] [-%s] %s ...\n"
381 (_am_rmdir ? "L" : "fLrR"),
382 (_am_rmdir ? catgets(_ttcatd, 8, 9, "file")
383 : catgets(_ttcatd, 8, 10, "dir") ),
384 (char *)_prog_name, (char *)_prog_name );
386 catgets(_ttcatd, 8, 11,
387 "\t-L do not perform a %s(1)\n"
388 "\t-v print the version number and quit\n"
389 "\t-h[elp] print this message\n" ),
390 (_am_rmdir ? "rmdir" : "rm"));