mover.C 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  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
  22. */
  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 $
  28. /*
  29. * mover.cc - Link Service/ToolTalk wrapper for mv(1).
  30. *
  31. * Copyright (c) 1990 by Sun Microsystems, Inc.
  32. *
  33. */
  34. #include "tt_options.h"
  35. #include <errno.h>
  36. #include <string.h>
  37. #if defined(__linux__) || defined(CSRG_BASED)
  38. #include <unistd.h>
  39. #else
  40. #if !defined(sun)
  41. #include <osfcn.h>
  42. #endif
  43. #endif
  44. #include <stdlib.h>
  45. #include <errno.h>
  46. #include <sys/stat.h>
  47. #include <sys/wait.h>
  48. #include "Tt/tt_c.h"
  49. #include "util/tt_gettext.h"
  50. #include "util/copyright.h"
  51. #include "mover.h"
  52. /*
  53. * External variables
  54. */
  55. /*
  56. * mover::mover()
  57. */
  58. mover::
  59. mover( char *arg0 )
  60. {
  61. if (arg0 != NULL) {
  62. char *base = strrchr( arg0, '/' );
  63. if (base == NULL) {
  64. base = arg0;
  65. } else {
  66. base++; // Don't want the '/'
  67. }
  68. _prog_name = base;
  69. _process_name = _prog_name;
  70. }
  71. _args = new _Tt_string_list();
  72. _from_paths = new _Tt_string_list();
  73. _should_mv = TRUE;
  74. _force = FALSE;
  75. _tt_opened = FALSE;
  76. _to_path_is_dir = FALSE;
  77. }
  78. mover::
  79. ~mover()
  80. {
  81. }
  82. /*
  83. * mover::do_mv() - Use system() to invoke mv(1), and return its exit status.
  84. * We can just use _args, since we never get here when our one
  85. * mv-incompatible option (-L) has been given.
  86. */
  87. int mover::
  88. do_mv()
  89. {
  90. _Tt_string cmd( "mv" );
  91. _Tt_string_list_cursor arg_cursor( _args );
  92. while (arg_cursor.next()) {
  93. cmd = cmd.cat( " " ).cat( *arg_cursor );
  94. }
  95. //printf( "Invoking: %s\n", (char *)cmd );
  96. int sys_status = system( (char *)cmd );
  97. if (WIFEXITED(sys_status)) {
  98. return WEXITSTATUS(sys_status);
  99. } else {
  100. if (! _force) {
  101. fprintf(stderr, "%s: system(\"%s\"): %d\n",
  102. (char *)_process_name, (char *)cmd, sys_status);
  103. }
  104. return 1;
  105. }
  106. }
  107. /*
  108. * mover::do_ttmv() - Use tt_file_move() on the things to move.
  109. */
  110. Tt_status mover::
  111. do_ttmv()
  112. {
  113. Tt_status worst_err = TT_OK;
  114. Tt_status err;
  115. _Tt_string full_to_path;
  116. bool_t abort = FALSE;
  117. bool_t are_more;
  118. full_to_path = _to_path;
  119. _Tt_string_list_cursor from_path_cursor( _from_paths );
  120. /*
  121. * call to next() must be first, so that are_more will be valid
  122. * if we abort. Why does the next() method wrap around?
  123. */
  124. while ((are_more = from_path_cursor.next()) && (! abort)) {
  125. if (! this->can_mv( *from_path_cursor )) {
  126. continue;
  127. }
  128. /*
  129. * tt_file_destroy() any path that mv(1) will delete
  130. */
  131. if (_to_path_is_dir) {
  132. full_to_path = _to_path.cat("/").
  133. cat(*from_path_cursor);
  134. }
  135. /*
  136. * mv(1) will overwrite any entry in _to_path that
  137. * has the same name as a _from_path.
  138. */
  139. err = tt_file_destroy( (char *)full_to_path );
  140. if ((err > TT_WRN_LAST) && (! _force)) {
  141. fprintf( stderr,
  142. catgets(_ttcatd, 8, 2,
  143. "%s: Could not remove "
  144. "ToolTalk objects of %s "
  145. "because %s\n"),
  146. (char *)_process_name,
  147. (char *)full_to_path,
  148. tt_status_message(err) );
  149. }
  150. err = tt_file_move( (char *)*from_path_cursor,
  151. (char *)full_to_path );
  152. if (err > TT_WRN_LAST) {
  153. worst_err = err;
  154. if (! _force) {
  155. fprintf( stderr,
  156. catgets(_ttcatd, 8, 3,
  157. "%s: Could not move ToolTalk "
  158. "objects of \"%s\" to \"%s\" "
  159. "because %s\n"),
  160. (char *)_process_name,
  161. (char *)*from_path_cursor,
  162. (char *)full_to_path,
  163. tt_status_message( err ));
  164. }
  165. switch (err) {
  166. case TT_ERR_DBAVAIL:
  167. case TT_ERR_PATH:
  168. break;
  169. case TT_ERR_NOMP:
  170. case TT_ERR_DBEXIST:
  171. default:
  172. abort = TRUE;
  173. break;
  174. }
  175. }
  176. }
  177. if (are_more && (! _force)) {
  178. from_path_cursor.prev();
  179. fprintf( stderr,
  180. catgets(_ttcatd, 8, 4,
  181. "%s: Will not attempt to move the ToolTalk "
  182. "objects of:\n"),
  183. (char *)_process_name );
  184. while (from_path_cursor.next()) {
  185. fprintf( stderr, "\t%s\n", (char *)*from_path_cursor );
  186. }
  187. }
  188. /*
  189. * TO_DO: This should be uncommented if you think that warning them
  190. * about hygiene is more important than obeying the -f flag.
  191. *
  192. if ((worst_err > TT_WRN_LAST) && _should_mv && _force) {
  193. fprintf( stderr, "%s: The ToolTalk objects of some files were "
  194. "not moved.\nSince you've told us to move the files "
  195. "anyway, you will need to\nuse %s -L to move "
  196. "the ToolTalk objects of the problem files.\n",
  197. (char *)_process_name, (char *)_prog_name );
  198. }
  199. */
  200. return worst_err;
  201. } /* do_ttmv() */
  202. /*
  203. * mover::can_mv() - Can we move this path to _to_path?
  204. *
  205. * TO_DO: Judging by mv.c, can_mv() can be as tricky as you like.
  206. * I'll count on tt_file_move() to Do The Right Thing.
  207. */
  208. bool_t mover::
  209. can_mv( _Tt_string from_path )
  210. {
  211. struct stat lstat_buf;
  212. if (lstat( (char *)from_path, &lstat_buf) == 0) {
  213. if (S_ISLNK(lstat_buf.st_mode)) {
  214. /*
  215. * Don't tt_file_move() a symlink, or TT will
  216. * tt_file_move() the linked file.
  217. */
  218. return FALSE;
  219. } else {
  220. return TRUE;
  221. }
  222. } else {
  223. /*
  224. * If we're trying to mv a file that doesn't exist,
  225. * let's not tt_file_move() the associated pathname.
  226. * But if we're trying to ttmv -L a file that doesn't
  227. * exist, we should probably tt_file_move() it anyway.
  228. */
  229. if (_should_mv) {
  230. return FALSE;
  231. } else {
  232. return TRUE;
  233. }
  234. }
  235. }
  236. /*
  237. * mover::open_tt()
  238. */
  239. Tt_status mover::
  240. open_tt()
  241. {
  242. char *process_id = tt_open();
  243. Tt_status err = tt_ptr_error( process_id );
  244. if (err == TT_OK) {
  245. _process_id = process_id;
  246. _tt_opened = TRUE;
  247. } else if (err > TT_WRN_LAST) {
  248. fprintf( stderr,
  249. "%s: tt_open(): %s\n",
  250. (char *)_process_name, tt_status_message(err) );
  251. }
  252. return err;
  253. }
  254. /*
  255. * mover::close_tt()
  256. */
  257. Tt_status mover::
  258. close_tt()
  259. {
  260. if (! _tt_opened) {
  261. return TT_OK;
  262. }
  263. Tt_status err = tt_close();
  264. if (err > TT_WRN_LAST) {
  265. fprintf( stderr,
  266. "%s: tt_close(): %s\n",
  267. (char *)_process_name, tt_status_message(err) );
  268. }
  269. return err;
  270. }
  271. /*
  272. * mover::parse_args()
  273. */
  274. void mover::
  275. parse_args( int argc, char **argv )
  276. {
  277. bool_t no_more_options = FALSE;
  278. for ( int arg_num = 1; arg_num < argc; arg_num++ ) {
  279. _Tt_string arg( argv[arg_num] );
  280. _args->append( arg );
  281. if ((arg[0] == '-') && (! no_more_options)) {
  282. if (arg[1] == '\0') {
  283. /*
  284. * The bare option "-" means take the
  285. * subsequent arguments to be pathnames.
  286. */
  287. no_more_options = TRUE;
  288. } else {
  289. for (int n = 1; n < arg.len(); n++) {
  290. switch (arg[n]) {
  291. case 'f':
  292. _force = TRUE;
  293. break;
  294. case 'L':
  295. _should_mv = FALSE;
  296. break;
  297. case 'v':
  298. _TT_PRINT_VERSIONS((char *)_prog_name)
  299. exit(0);
  300. break;
  301. case 'h':
  302. default:
  303. this->usage();
  304. exit(1);
  305. }
  306. }
  307. }
  308. } else {
  309. if (arg_num == argc - 1) {
  310. _to_path = arg;
  311. } else {
  312. _from_paths->append( arg );
  313. }
  314. }
  315. }
  316. if ((_to_path.len() <= 0) || (_from_paths->count() <= 0)) {
  317. this->usage();
  318. exit(1);
  319. }
  320. if (_from_paths->count() > 1) {
  321. /*
  322. * If multiple things to move, the place we're moving them to
  323. * must be a directory.
  324. */
  325. struct stat stat_buf;
  326. if (stat( (char *)_to_path, &stat_buf) != 0) {
  327. fprintf( stderr, "%s: \"%s\": ", (char *)_process_name,
  328. (char *)_to_path );
  329. perror(NULL);
  330. exit(2);
  331. }
  332. if (! S_ISDIR(stat_buf.st_mode)) {
  333. fprintf( stderr, "%s: \"%s\": %s\n",
  334. (char *)_process_name, (char *)_to_path,
  335. strerror(ENOTDIR) );
  336. this->usage();
  337. exit(2);
  338. }
  339. _to_path_is_dir = TRUE;
  340. } else {
  341. struct stat stat_buf;
  342. _to_path_is_dir = FALSE;
  343. if (stat( (char *)_to_path, &stat_buf) == 0) {
  344. _to_path_is_dir = S_ISDIR(stat_buf.st_mode);
  345. }
  346. }
  347. }
  348. /*
  349. * mover::usage()
  350. */
  351. void mover::
  352. usage(FILE *fs) const
  353. {
  354. fprintf( fs,
  355. catgets(_ttcatd, 8, 5,
  356. "Usage: %s [-] [-fL] path1 path2\n"
  357. " %s [-] [-fL] path1 [path2 ...] dir\n"
  358. " %s -v\n"
  359. " %s -h\n"),
  360. (char *)_prog_name, (char *)_prog_name, (char *)_prog_name,
  361. (char *)_prog_name );
  362. fprintf( fs, "%s",
  363. catgets(_ttcatd, 8, 6,
  364. "\t-L do not perform a mv(1)\n"
  365. "\t-v print the version number and quit\n"
  366. "\t-h print this message\n" ));
  367. }