source: rtems/cpukit/libmisc/shell/main_mv.c @ 575babc

4.104.114.95
Last change on this file since 575babc was 575babc, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/21/08 at 12:29:02

Include "config.h".

  • Property mode set to 100644
File size: 12.4 KB
Line 
1/* $NetBSD: mv.c,v 1.41 2008/07/20 00:52:40 lukem Exp $ */
2
3/*
4 * Copyright (c) 1989, 1993, 1994
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ken Smith of The State University of New York at Buffalo.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#if 0
40#include <sys/cdefs.h>
41#ifndef lint
42__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
43 The Regents of the University of California.  All rights reserved.");
44#endif /* not lint */
45
46#ifndef lint
47#if 0
48static char sccsid[] = "@(#)mv.c        8.2 (Berkeley) 4/2/94";
49#else
50__RCSID("$NetBSD: mv.c,v 1.41 2008/07/20 00:52:40 lukem Exp $");
51#endif
52#endif /* not lint */
53#endif
54
55#include <rtems.h>
56#include <rtems/shell.h>
57#include <rtems/shellconfig.h>
58#include <getopt.h>
59
60#include <sys/param.h>
61#include <sys/time.h>
62#include <sys/wait.h>
63#include <sys/stat.h>
64
65#include <err.h>
66#include <errno.h>
67#include <fcntl.h>
68#include <grp.h>
69#include <locale.h>
70#include <pwd.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74#include <unistd.h>
75
76#include "pathnames-mv.h"
77
78/* RTEMS specific changes */
79typedef struct {
80  int fflg, iflg, vflg;
81  int stdin_ok;
82  int exit_code;
83  jmp_buf exit_jmp;
84} rtems_shell_mv_globals;
85
86int     copy(char *, char *);
87int     do_move(char *, char *);
88int     fastcopy(char *, char *, struct stat *);
89void    usage(void);
90int     main(int, char *[]);
91
92#define fflg      globals->fflg
93#define iflg      globals->iflg
94#define vflg      globals->vflg
95#define stdin_ok  globals->stdin_ok
96#define exit_jump &(globals->exit_jmp)
97
98#include <setjmp.h>
99
100#define exit(ec) rtems_shell_mv_exit(globals, ec)
101
102void
103rtems_shell_mv_exit (rtems_shell_mv_globals* globals, int code)
104{
105  globals->exit_code = code;
106  longjmp (globals->exit_jmp, 1);
107}
108
109void strmode(mode_t mode, char *p);
110const char *user_from_uid(uid_t uid, int nouser);
111char *group_from_gid(gid_t gid, int nogroup);
112
113static int main_mv(rtems_shell_mv_globals* globals, int argc, char *argv[]);
114
115int rtems_shell_main_cp(int argc, char *argv[]);
116int rtems_shell_main_rm(int argc, char *argv[]);
117
118int
119rtems_shell_main_mv(int argc, char *argv[])
120{
121  rtems_shell_mv_globals mv_globals;
122  memset (&mv_globals, 0, sizeof (mv_globals));
123  mv_globals.exit_code = 1;
124  if (setjmp (mv_globals.exit_jmp) == 0)
125    return main_mv (&mv_globals, argc, argv);
126  return mv_globals.exit_code;
127}
128
129#define do_move(a1, a2)      do_move_mv(globals, a1, a2)
130#define fastcopy(a1, a2, a3) fastcopy_mv(globals, a1, a2, a3)
131#define copy(a1, a2)         copy_mv(globals, a1, a2)
132#define usage()              usage_mv(globals)
133
134static int do_move_mv(rtems_shell_mv_globals* globals, char *from, char *to);
135static int fastcopy_mv(rtems_shell_mv_globals* globals,
136                       char *from, char *to, struct stat *sbp);
137static int copy_mv(rtems_shell_mv_globals* globals, char *from, char *to);
138static void usage_mv(rtems_shell_mv_globals* globals);
139
140/* RTEMS changes */
141
142int
143main_mv(rtems_shell_mv_globals* globals, int argc, char *argv[])
144{
145        int ch, len, rval;
146        char *p, *endp;
147        struct stat sb;
148        char path[MAXPATHLEN + 1];
149        size_t baselen;
150
151  struct getopt_data getopt_reent;
152  memset(&getopt_reent, 0, sizeof(getopt_data));
153 
154/*      setprogname(argv[0]); */
155 
156        (void)setlocale(LC_ALL, "");
157
158        while ((ch = getopt_r(argc, argv, "ifv", &getopt_reent)) != -1)
159                switch (ch) {
160                case 'i':
161                        fflg = 0;
162                        iflg = 1;
163                        break;
164                case 'f':
165                        iflg = 0;
166                        fflg = 1;
167                        break;
168                case 'v':
169                        vflg = 1;
170                        break;
171                default:
172                        usage();
173                }
174        argc -= getopt_reent.optind;
175        argv += getopt_reent.optind;
176
177        if (argc < 2)
178                usage();
179
180        stdin_ok = isatty(STDIN_FILENO);
181
182        /*
183         * If the stat on the target fails or the target isn't a directory,
184         * try the move.  More than 2 arguments is an error in this case.
185         */
186        if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
187                if (argc > 2)
188                        usage();
189                exit(do_move(argv[0], argv[1]));
190        }
191
192        /* It's a directory, move each file into it. */
193        baselen = strlcpy(path, argv[argc - 1], sizeof(path));
194        if (baselen >= sizeof(path))
195                errx(exit_jump, 1, "%s: destination pathname too long", argv[argc - 1]);
196        endp = &path[baselen];
197        if (!baselen || *(endp - 1) != '/') {
198                *endp++ = '/';
199                ++baselen;
200        }
201        for (rval = 0; --argc; ++argv) {
202                p = *argv + strlen(*argv) - 1;
203                while (*p == '/' && p != *argv)
204                        *p-- = '\0';
205                if ((p = strrchr(*argv, '/')) == NULL)
206                        p = *argv;
207                else
208                        ++p;
209
210                if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
211                        warnx("%s: destination pathname too long", *argv);
212                        rval = 1;
213                } else {
214                        memmove(endp, p, len + 1);
215                        if (do_move(*argv, path))
216                                rval = 1;
217                }
218        }
219  return rval;
220}
221
222int
223do_move_mv(rtems_shell_mv_globals* globals, char *from, char *to)
224{
225        struct stat sb;
226        char modep[15];
227
228        /*
229         * (1)  If the destination path exists, the -f option is not specified
230         *      and either of the following conditions are true:
231         *
232         *      (a) The permissions of the destination path do not permit
233         *          writing and the standard input is a terminal.
234         *      (b) The -i option is specified.
235         *
236         *      the mv utility shall write a prompt to standard error and
237         *      read a line from standard input.  If the response is not
238         *      affirmative, mv shall do nothing more with the current
239         *      source file...
240         */
241        if (!fflg && !access(to, F_OK)) {
242                int ask = 1;
243                int ch;
244
245                if (iflg) {
246                        if (access(from, F_OK)) {
247                                warn("rename %s", from);
248                                return (1);
249                        }
250                        (void)fprintf(stderr, "overwrite %s? ", to);
251                } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
252                        if (access(from, F_OK)) {
253                                warn("rename %s", from);
254                                return (1);
255                        }
256                        strmode(sb.st_mode, modep);
257                        (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
258                            modep + 1, modep[9] == ' ' ? "" : " ",
259                            user_from_uid(sb.st_uid, 0),
260                            group_from_gid(sb.st_gid, 0), to);
261                } else
262                        ask = 0;
263                if (ask) {
264                        if ((ch = getchar()) != EOF && ch != '\n') {
265                                int ch2;
266                                while ((ch2 = getchar()) != EOF && ch2 != '\n')
267                                        continue;
268                        }
269                        if (ch != 'y' && ch != 'Y')
270                                return (0);
271                }
272        }
273
274        /*
275         * (2)  If rename() succeeds, mv shall do nothing more with the
276         *      current source file.  If it fails for any other reason than
277         *      EXDEV, mv shall write a diagnostic message to the standard
278         *      error and do nothing more with the current source file.
279         *
280         * (3)  If the destination path exists, and it is a file of type
281         *      directory and source_file is not a file of type directory,
282         *      or it is a file not of type directory, and source file is
283         *      a file of type directory, mv shall write a diagnostic
284         *      message to standard error, and do nothing more with the
285         *      current source file...
286         */
287        if (!rename(from, to)) {
288                if (vflg)
289                        printf("%s -> %s\n", from, to);
290                return (0);
291        }
292
293        if (errno != EXDEV) {
294                warn("rename %s to %s", from, to);
295                return (1);
296        }
297
298        /*
299         * (4)  If the destination path exists, mv shall attempt to remove it.
300         *      If this fails for any reason, mv shall write a diagnostic
301         *      message to the standard error and do nothing more with the
302         *      current source file...
303         */
304        if (!lstat(to, &sb)) {
305                if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
306                        warn("can't remove %s", to);
307                        return (1);
308                }
309        }
310
311        /*
312         * (5)  The file hierarchy rooted in source_file shall be duplicated
313         *      as a file hierarchy rooted in the destination path...
314         */
315        if (lstat(from, &sb)) {
316                warn("%s", from);
317                return (1);
318        }
319
320        return (S_ISREG(sb.st_mode) ?
321            fastcopy(from, to, &sb) : copy(from, to));
322}
323
324int
325fastcopy_mv(rtems_shell_mv_globals* globals, char *from, char *to, struct stat *sbp)
326{
327        struct timeval tval[2];
328        uint32_t blen;
329        static char *bp;
330        int nread, from_fd, to_fd;
331
332  blen = 0;
333 
334        if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
335                warn("%s", from);
336                return (1);
337        }
338        if ((to_fd =
339            open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
340                warn("%s", to);
341                (void)close(from_fd);
342                return (1);
343        }
344        if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
345                warn(NULL);
346                blen = 0;
347                (void)close(from_fd);
348                (void)close(to_fd);
349                return (1);
350        }
351        while ((nread = read(from_fd, bp, blen)) > 0)
352                if (write(to_fd, bp, nread) != nread) {
353                        warn("%s", to);
354                        goto err;
355                }
356        if (nread < 0) {
357                warn("%s", from);
358err:            if (unlink(to))
359                        warn("%s: remove", to);
360    (void)free(bp);
361                (void)close(from_fd);
362                (void)close(to_fd);
363                return (1);
364        }
365 
366  (void)free(bp);
367        (void)close(from_fd);
368#ifdef xBSD4_4
369        TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
370        TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
371#else
372        tval[0].tv_sec = sbp->st_atime;
373        tval[1].tv_sec = sbp->st_mtime;
374        tval[0].tv_usec = 0;
375        tval[1].tv_usec = 0;
376#endif
377#if 0
378#ifdef _SRV5
379        if (utimes(to, tval))
380#else
381        if (futimes(to_fd, tval))
382#endif
383                warn("%s: set times", to);
384#endif
385        if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
386                if (errno != EPERM)
387                        warn("%s: set owner/group", to);
388                sbp->st_mode &= ~(S_ISUID | S_ISGID);
389        }
390        if (fchmod(to_fd, sbp->st_mode))
391                warn("%s: set mode", to);
392#if 0
393        if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
394                warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
395#endif
396        if (close(to_fd)) {
397                warn("%s", to);
398                return (1);
399        }
400
401        if (unlink(from)) {
402                warn("%s: remove", from);
403                return (1);
404        }
405
406        if (vflg)
407                printf("%s -> %s\n", from, to);
408
409        return (0);
410}
411
412int
413copy_mv(rtems_shell_mv_globals* globals, char *from, char *to)
414{
415  char* cp_argv[5] = { "mv", vflg ? "-PRpv" : "-PRp", "--", from, to };
416  char* rm_argv[4] = { "mv", "-rf", "--", from };
417  int result;
418
419  result = rtems_shell_main_cp(5, cp_argv);
420  if (result) {
421                warnx("%s: did not terminate normally", _PATH_CP);
422                return (1);
423  }
424  result = rtems_shell_main_rm(4, rm_argv);
425  if (result) {
426                warnx("%s: did not terminate normally", _PATH_RM);
427                return (1);
428  }
429#if 0
430        pid_t pid;
431        int status;
432
433        if ((pid = vfork()) == 0) {
434                execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to, NULL);
435                warn("%s", _PATH_CP);
436                _exit(1);
437        }
438        if (waitpid(pid, &status, 0) == -1) {
439                warn("%s: waitpid", _PATH_CP);
440                return (1);
441        }
442        if (!WIFEXITED(status)) {
443                warnx("%s: did not terminate normally", _PATH_CP);
444                return (1);
445        }
446        if (WEXITSTATUS(status)) {
447                warnx("%s: terminated with %d (non-zero) status",
448                    _PATH_CP, WEXITSTATUS(status));
449                return (1);
450        }
451        if (!(pid = vfork())) {
452                execl(_PATH_RM, "mv", "-rf", "--", from, NULL);
453                warn("%s", _PATH_RM);
454                _exit(1);
455        }
456        if (waitpid(pid, &status, 0) == -1) {
457                warn("%s: waitpid", _PATH_RM);
458                return (1);
459        }
460        if (!WIFEXITED(status)) {
461                warnx("%s: did not terminate normally", _PATH_RM);
462                return (1);
463        }
464        if (WEXITSTATUS(status)) {
465                warnx("%s: terminated with %d (non-zero) status",
466                    _PATH_RM, WEXITSTATUS(status));
467                return (1);
468        }
469#endif
470        return (0);
471}
472
473void
474usage_mv(rtems_shell_mv_globals* globals)
475{
476        (void)fprintf(stderr, "usage: %s [-fiv] source target\n"
477                "       %s [-fiv] source ... directory\n",
478                "mv", "mv");
479        exit(1);
480        /* NOTREACHED */
481}
482
483rtems_shell_cmd_t rtems_shell_MV_Command = {
484  "mv",                                      /* name */
485  "[-fiv] source target ...",                /* usage */
486  "files",                                   /* topic */
487  rtems_shell_main_mv,                       /* command */
488  NULL,                                      /* alias */
489  NULL                                       /* next */
490};
Note: See TracBrowser for help on using the repository browser.