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 //%% $XConsortium: mover.C /main/4 1995/10/20 16:36:15 rswiston $
29 * mover.cc - Link Service/ToolTalk wrapper for mv(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_gettext.h"
51 #include "util/copyright.h"
65 char *base = strrchr( arg0, '/' );
69 base++; // Don't want the '/'
72 _process_name = _prog_name;
74 _args = new _Tt_string_list();
75 _from_paths = new _Tt_string_list();
79 _to_path_is_dir = FALSE;
88 * mover::do_mv() - Use system() to invoke mv(1), and return its exit status.
89 * We can just use _args, since we never get here when our one
90 * mv-incompatible option (-L) has been given.
95 _Tt_string cmd( "mv" );
96 _Tt_string_list_cursor arg_cursor( _args );
98 while (arg_cursor.next()) {
99 cmd = cmd.cat( " " ).cat( *arg_cursor );
101 //printf( "Invoking: %s\n", (char *)cmd );
102 int sys_status = system( (char *)cmd );
103 if (WIFEXITED(sys_status)) {
104 return WEXITSTATUS(sys_status);
107 fprintf(stderr, "%s: system(\"%s\"): %d\n",
108 (char *)_process_name, (char *)cmd, sys_status);
115 * mover::do_ttmv() - Use tt_file_move() on the things to move.
120 Tt_status worst_err = TT_OK;
122 _Tt_string full_to_path;
123 bool_t abort = FALSE;
126 full_to_path = _to_path;
127 _Tt_string_list_cursor from_path_cursor( _from_paths );
129 * call to next() must be first, so that are_more will be valid
130 * if we abort. Why does the next() method wrap around?
132 while ((are_more = from_path_cursor.next()) && (! abort)) {
133 if (! this->can_mv( *from_path_cursor )) {
137 * tt_file_destroy() any path that mv(1) will delete
139 if (_to_path_is_dir) {
140 full_to_path = _to_path.cat("/").
141 cat(*from_path_cursor);
144 * mv(1) will overwrite any entry in _to_path that
145 * has the same name as a _from_path.
147 err = tt_file_destroy( (char *)full_to_path );
148 if ((err > TT_WRN_LAST) && (! _force)) {
150 catgets(_ttcatd, 8, 2,
151 "%s: Could not remove "
152 "ToolTalk objects of %s "
154 (char *)_process_name,
155 (char *)full_to_path,
156 tt_status_message(err) );
158 err = tt_file_move( (char *)*from_path_cursor,
159 (char *)full_to_path );
160 if (err > TT_WRN_LAST) {
164 catgets(_ttcatd, 8, 3,
165 "%s: Could not move ToolTalk "
166 "objects of \"%s\" to \"%s\" "
168 (char *)_process_name,
169 (char *)*from_path_cursor,
170 (char *)full_to_path,
171 tt_status_message( err ));
185 if (are_more && (! _force)) {
186 from_path_cursor.prev();
188 catgets(_ttcatd, 8, 4,
189 "%s: Will not attempt to move the ToolTalk "
191 (char *)_process_name );
192 while (from_path_cursor.next()) {
193 fprintf( stderr, "\t%s\n", (char *)*from_path_cursor );
197 * TO_DO: This should be uncommented if you think that warning them
198 * about hygiene is more important than obeying the -f flag.
200 if ((worst_err > TT_WRN_LAST) && _should_mv && _force) {
201 fprintf( stderr, "%s: The ToolTalk objects of some files were "
202 "not moved.\nSince you've told us to move the files "
203 "anyway, you will need to\nuse %s -L to move "
204 "the ToolTalk objects of the problem files.\n",
205 (char *)_process_name, (char *)_prog_name );
213 * mover::can_mv() - Can we move this path to _to_path?
215 * TO_DO: Judging by mv.c, can_mv() can be as tricky as you like.
216 * I'll count on tt_file_move() to Do The Right Thing.
219 can_mv( _Tt_string from_path )
221 struct stat lstat_buf;
222 if (lstat( (char *)from_path, &lstat_buf) == 0) {
223 if (S_ISLNK(lstat_buf.st_mode)) {
225 * Don't tt_file_move() a symlink, or TT will
226 * tt_file_move() the linked file.
234 * If we're trying to mv a file that doesn't exist,
235 * let's not tt_file_move() the associated pathname.
236 * But if we're trying to ttmv -L a file that doesn't
237 * exist, we should probably tt_file_move() it anyway.
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) );
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 * mover::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 );
295 if ((arg[0] == '-') && (! no_more_options)) {
296 if (arg[1] == '\0') {
298 * The bare option "-" means take the
299 * subsequent arguments to be pathnames.
301 no_more_options = TRUE;
303 for (int n = 1; n < arg.len(); n++) {
312 _TT_PRINT_VERSIONS((char *)_prog_name)
323 if (arg_num == argc - 1) {
326 _from_paths->append( arg );
330 if ((_to_path.len() <= 0) || (_from_paths->count() <= 0)) {
334 if (_from_paths->count() > 1) {
336 * If multiple things to move, the place we're moving them to
337 * must be a directory.
339 struct stat stat_buf;
341 if (stat( (char *)_to_path, &stat_buf) != 0) {
342 fprintf( stderr, "%s: \"%s\": ", (char *)_process_name,
347 if (! S_ISDIR(stat_buf.st_mode)) {
348 fprintf( stderr, "%s: \"%s\": %s\n",
349 (char *)_process_name, (char *)_to_path,
354 _to_path_is_dir = TRUE;
356 struct stat stat_buf;
358 _to_path_is_dir = FALSE;
359 if (stat( (char *)_to_path, &stat_buf) == 0) {
360 _to_path_is_dir = S_ISDIR(stat_buf.st_mode);
369 usage(FILE *fs) const
372 catgets(_ttcatd, 8, 5,
373 "Usage: %s [-] [-fL] path1 path2\n"
374 " %s [-] [-fL] path1 [path2 ...] dir\n"
377 (char *)_prog_name, (char *)_prog_name, (char *)_prog_name,
378 (char *)_prog_name );
380 catgets(_ttcatd, 8, 6,
381 "\t-L do not perform a mv(1)\n"
382 "\t-v print the version number and quit\n"
383 "\t-h print this message\n" ));