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(__osf__) || 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_gettext.h"
55 #include "util/copyright.h"
69 char *base = strrchr( arg0, '/' );
73 base++; // Don't want the '/'
76 _process_name = _prog_name;
78 _args = new _Tt_string_list();
79 _from_paths = new _Tt_string_list();
83 _to_path_is_dir = FALSE;
92 * mover::do_mv() - Use system() to invoke mv(1), and return its exit status.
93 * We can just use _args, since we never get here when our one
94 * mv-incompatible option (-L) has been given.
99 _Tt_string cmd( "mv" );
100 _Tt_string_list_cursor arg_cursor( _args );
102 while (arg_cursor.next()) {
103 cmd = cmd.cat( " " ).cat( *arg_cursor );
105 //printf( "Invoking: %s\n", (char *)cmd );
106 int sys_status = system( (char *)cmd );
107 if (WIFEXITED(sys_status)) {
108 return WEXITSTATUS(sys_status);
111 fprintf(stderr, "%s: system(\"%s\"): %d\n",
112 (char *)_process_name, (char *)cmd, sys_status);
119 * mover::do_ttmv() - Use tt_file_move() on the things to move.
124 Tt_status worst_err = TT_OK;
126 _Tt_string full_to_path;
127 bool_t abort = FALSE;
130 full_to_path = _to_path;
131 _Tt_string_list_cursor from_path_cursor( _from_paths );
133 * call to next() must be first, so that are_more will be valid
134 * if we abort. Why does the next() method wrap around?
136 while ((are_more = from_path_cursor.next()) && (! abort)) {
137 if (! this->can_mv( *from_path_cursor )) {
141 * tt_file_destroy() any path that mv(1) will delete
143 if (_to_path_is_dir) {
144 full_to_path = _to_path.cat("/").
145 cat(*from_path_cursor);
148 * mv(1) will overwrite any entry in _to_path that
149 * has the same name as a _from_path.
151 err = tt_file_destroy( (char *)full_to_path );
152 if ((err > TT_WRN_LAST) && (! _force)) {
154 catgets(_ttcatd, 8, 2,
155 "%s: Could not remove "
156 "ToolTalk objects of %s "
158 (char *)_process_name,
159 (char *)full_to_path,
160 tt_status_message(err) );
162 err = tt_file_move( (char *)*from_path_cursor,
163 (char *)full_to_path );
164 if (err > TT_WRN_LAST) {
168 catgets(_ttcatd, 8, 3,
169 "%s: Could not move ToolTalk "
170 "objects of \"%s\" to \"%s\" "
172 (char *)_process_name,
173 (char *)*from_path_cursor,
174 (char *)full_to_path,
175 tt_status_message( err ));
189 if (are_more && (! _force)) {
190 from_path_cursor.prev();
192 catgets(_ttcatd, 8, 4,
193 "%s: Will not attempt to move the ToolTalk "
195 (char *)_process_name );
196 while (from_path_cursor.next()) {
197 fprintf( stderr, "\t%s\n", (char *)*from_path_cursor );
201 * TO_DO: This should be uncommented if you think that warning them
202 * about hygiene is more important than obeying the -f flag.
204 if ((worst_err > TT_WRN_LAST) && _should_mv && _force) {
205 fprintf( stderr, "%s: The ToolTalk objects of some files were "
206 "not moved.\nSince you've told us to move the files "
207 "anyway, you will need to\nuse %s -L to move "
208 "the ToolTalk objects of the problem files.\n",
209 (char *)_process_name, (char *)_prog_name );
217 * mover::can_mv() - Can we move this path to _to_path?
219 * TO_DO: Judging by mv.c, can_mv() can be as tricky as you like.
220 * I'll count on tt_file_move() to Do The Right Thing.
223 can_mv( _Tt_string from_path )
225 struct stat lstat_buf;
226 if (lstat( (char *)from_path, &lstat_buf) == 0) {
227 if (S_ISLNK(lstat_buf.st_mode)) {
229 * Don't tt_file_move() a symlink, or TT will
230 * tt_file_move() the linked file.
238 * If we're trying to mv a file that doesn't exist,
239 * let's not tt_file_move() the associated pathname.
240 * But if we're trying to ttmv -L a file that doesn't
241 * exist, we should probably tt_file_move() it anyway.
257 char *process_id = tt_open();
258 Tt_status err = tt_ptr_error( process_id );
260 _process_id = process_id;
262 } else if (err > TT_WRN_LAST) {
264 "%s: tt_open(): %s\n",
265 (char *)_process_name, tt_status_message(err) );
279 Tt_status err = tt_close();
280 if (err > TT_WRN_LAST) {
282 "%s: tt_close(): %s\n",
283 (char *)_process_name, tt_status_message(err) );
289 * mover::parse_args()
292 parse_args( int argc, char **argv )
294 bool_t no_more_options = FALSE;
296 for ( int arg_num = 1; arg_num < argc; arg_num++ ) {
297 _Tt_string arg( argv[arg_num] );
298 _args->append( arg );
299 if ((arg[0] == '-') && (! no_more_options)) {
300 if (arg[1] == '\0') {
302 * The bare option "-" means take the
303 * subsequent arguments to be pathnames.
305 no_more_options = TRUE;
307 for (int n = 1; n < arg.len(); n++) {
316 _TT_PRINT_VERSIONS((char *)_prog_name)
327 if (arg_num == argc - 1) {
330 _from_paths->append( arg );
334 if ((_to_path.len() <= 0) || (_from_paths->count() <= 0)) {
338 if (_from_paths->count() > 1) {
340 * If multiple things to move, the place we're moving them to
341 * must be a directory.
343 struct stat stat_buf;
345 if (stat( (char *)_to_path, &stat_buf) != 0) {
346 fprintf( stderr, "%s: \"%s\": ", (char *)_process_name,
351 if (! S_ISDIR(stat_buf.st_mode)) {
352 fprintf( stderr, "%s: \"%s\": %s\n",
353 (char *)_process_name, (char *)_to_path,
358 _to_path_is_dir = TRUE;
360 struct stat stat_buf;
362 _to_path_is_dir = FALSE;
363 if (stat( (char *)_to_path, &stat_buf) == 0) {
364 _to_path_is_dir = S_ISDIR(stat_buf.st_mode);
373 usage(FILE *fs) const
376 catgets(_ttcatd, 8, 5,
377 "Usage: %s [-] [-fL] path1 path2\n"
378 " %s [-] [-fL] path1 [path2 ...] dir\n"
381 (char *)_prog_name, (char *)_prog_name, (char *)_prog_name,
382 (char *)_prog_name );
384 catgets(_ttcatd, 8, 6,
385 "\t-L do not perform a mv(1)\n"
386 "\t-v print the version number and quit\n"
387 "\t-h print this message\n" ));