source: rtems/cpukit/libmisc/shell/main_mv.c @ 6cdaa85

5
Last change on this file since 6cdaa85 was 6cdaa85, checked in by Sebastian Huber <sebastian.huber@…>, on 10/04/18 at 18:16:45

shell: Use #include "..." for local header files

Update #3375.

  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[8192e4ff]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
[575babc]35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
[8192e4ff]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>
[b079fe33]58#define __need_getopt_newlib
[8192e4ff]59#include <getopt.h>
60
61#include <sys/param.h>
62#include <sys/time.h>
63#include <sys/wait.h>
64#include <sys/stat.h>
65
[6cdaa85]66#include "err.h"
[8192e4ff]67#include <errno.h>
68#include <fcntl.h>
69#include <grp.h>
70#include <locale.h>
71#include <pwd.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <unistd.h>
76
[3421e520]77#include "internal.h"
78
[8192e4ff]79#include "pathnames-mv.h"
80
81/* RTEMS specific changes */
82typedef struct {
83  int fflg, iflg, vflg;
84  int stdin_ok;
85  int exit_code;
86  jmp_buf exit_jmp;
87} rtems_shell_mv_globals;
88
89int     copy(char *, char *);
90int     do_move(char *, char *);
91int     fastcopy(char *, char *, struct stat *);
92void    usage(void);
93int     main(int, char *[]);
94
95#define fflg      globals->fflg
96#define iflg      globals->iflg
97#define vflg      globals->vflg
98#define stdin_ok  globals->stdin_ok
99#define exit_jump &(globals->exit_jmp)
100
101#include <setjmp.h>
102
103#define exit(ec) rtems_shell_mv_exit(globals, ec)
104
[3237e88]105static int
106main_mv(rtems_shell_mv_globals* globals, int argc, char *argv[]);
107
108
[3421e520]109static void
[8192e4ff]110rtems_shell_mv_exit (rtems_shell_mv_globals* globals, int code)
111{
112  globals->exit_code = code;
113  longjmp (globals->exit_jmp, 1);
114}
115
116const char *user_from_uid(uid_t uid, int nouser);
117char *group_from_gid(gid_t gid, int nogroup);
118
[43b09a98]119int
[8192e4ff]120rtems_shell_main_mv(int argc, char *argv[])
121{
122  rtems_shell_mv_globals mv_globals;
123  memset (&mv_globals, 0, sizeof (mv_globals));
124  mv_globals.exit_code = 1;
125  if (setjmp (mv_globals.exit_jmp) == 0)
126    return main_mv (&mv_globals, argc, argv);
127  return mv_globals.exit_code;
128}
129
130#define do_move(a1, a2)      do_move_mv(globals, a1, a2)
[0893220]131#define fastcopy(a1, a2, a3) fastcopy_mv(globals, a1, a2, a3)
132#define copy(a1, a2)         copy_mv(globals, a1, a2)
[8192e4ff]133#define usage()              usage_mv(globals)
134
135static int do_move_mv(rtems_shell_mv_globals* globals, char *from, char *to);
136static int fastcopy_mv(rtems_shell_mv_globals* globals,
137                       char *from, char *to, struct stat *sbp);
138static int copy_mv(rtems_shell_mv_globals* globals, char *from, char *to);
139static void usage_mv(rtems_shell_mv_globals* globals);
140
141/* RTEMS changes */
142
143int
144main_mv(rtems_shell_mv_globals* globals, int argc, char *argv[])
145{
146        int ch, len, rval;
147        char *p, *endp;
148        struct stat sb;
149        char path[MAXPATHLEN + 1];
150        size_t baselen;
151
152  struct getopt_data getopt_reent;
153  memset(&getopt_reent, 0, sizeof(getopt_data));
[0893220]154
[8192e4ff]155/*      setprogname(argv[0]); */
[0893220]156
[8192e4ff]157        (void)setlocale(LC_ALL, "");
158
159        while ((ch = getopt_r(argc, argv, "ifv", &getopt_reent)) != -1)
160                switch (ch) {
161                case 'i':
162                        fflg = 0;
163                        iflg = 1;
164                        break;
165                case 'f':
166                        iflg = 0;
167                        fflg = 1;
168                        break;
169                case 'v':
170                        vflg = 1;
171                        break;
172                default:
173                        usage();
174                }
175        argc -= getopt_reent.optind;
176        argv += getopt_reent.optind;
177
178        if (argc < 2)
179                usage();
180
181        stdin_ok = isatty(STDIN_FILENO);
182
183        /*
184         * If the stat on the target fails or the target isn't a directory,
185         * try the move.  More than 2 arguments is an error in this case.
186         */
187        if (stat(argv[argc - 1], &sb) || !S_ISDIR(sb.st_mode)) {
188                if (argc > 2)
189                        usage();
190                exit(do_move(argv[0], argv[1]));
191        }
192
193        /* It's a directory, move each file into it. */
194        baselen = strlcpy(path, argv[argc - 1], sizeof(path));
195        if (baselen >= sizeof(path))
196                errx(exit_jump, 1, "%s: destination pathname too long", argv[argc - 1]);
197        endp = &path[baselen];
198        if (!baselen || *(endp - 1) != '/') {
199                *endp++ = '/';
200                ++baselen;
201        }
202        for (rval = 0; --argc; ++argv) {
203                p = *argv + strlen(*argv) - 1;
204                while (*p == '/' && p != *argv)
205                        *p-- = '\0';
206                if ((p = strrchr(*argv, '/')) == NULL)
207                        p = *argv;
208                else
209                        ++p;
210
211                if ((baselen + (len = strlen(p))) >= MAXPATHLEN) {
212                        warnx("%s: destination pathname too long", *argv);
213                        rval = 1;
214                } else {
215                        memmove(endp, p, len + 1);
216                        if (do_move(*argv, path))
217                                rval = 1;
218                }
219        }
220  return rval;
221}
222
223int
224do_move_mv(rtems_shell_mv_globals* globals, char *from, char *to)
225{
226        struct stat sb;
227        char modep[15];
228
229        /*
230         * (1)  If the destination path exists, the -f option is not specified
231         *      and either of the following conditions are true:
232         *
233         *      (a) The permissions of the destination path do not permit
234         *          writing and the standard input is a terminal.
235         *      (b) The -i option is specified.
236         *
237         *      the mv utility shall write a prompt to standard error and
238         *      read a line from standard input.  If the response is not
239         *      affirmative, mv shall do nothing more with the current
240         *      source file...
241         */
242        if (!fflg && !access(to, F_OK)) {
243                int ask = 1;
244                int ch;
245
246                if (iflg) {
247                        if (access(from, F_OK)) {
248                                warn("rename %s", from);
249                                return (1);
250                        }
251                        (void)fprintf(stderr, "overwrite %s? ", to);
252                } else if (stdin_ok && access(to, W_OK) && !stat(to, &sb)) {
253                        if (access(from, F_OK)) {
254                                warn("rename %s", from);
255                                return (1);
256                        }
257                        strmode(sb.st_mode, modep);
258                        (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
259                            modep + 1, modep[9] == ' ' ? "" : " ",
260                            user_from_uid(sb.st_uid, 0),
261                            group_from_gid(sb.st_gid, 0), to);
262                } else
263                        ask = 0;
264                if (ask) {
265                        if ((ch = getchar()) != EOF && ch != '\n') {
266                                int ch2;
267                                while ((ch2 = getchar()) != EOF && ch2 != '\n')
268                                        continue;
269                        }
270                        if (ch != 'y' && ch != 'Y')
271                                return (0);
272                }
273        }
274
275        /*
276         * (2)  If rename() succeeds, mv shall do nothing more with the
277         *      current source file.  If it fails for any other reason than
278         *      EXDEV, mv shall write a diagnostic message to the standard
279         *      error and do nothing more with the current source file.
280         *
281         * (3)  If the destination path exists, and it is a file of type
282         *      directory and source_file is not a file of type directory,
283         *      or it is a file not of type directory, and source file is
284         *      a file of type directory, mv shall write a diagnostic
285         *      message to standard error, and do nothing more with the
286         *      current source file...
287         */
288        if (!rename(from, to)) {
289                if (vflg)
290                        printf("%s -> %s\n", from, to);
291                return (0);
292        }
293
294        if (errno != EXDEV) {
295                warn("rename %s to %s", from, to);
296                return (1);
297        }
298
299        /*
300         * (4)  If the destination path exists, mv shall attempt to remove it.
301         *      If this fails for any reason, mv shall write a diagnostic
302         *      message to the standard error and do nothing more with the
303         *      current source file...
304         */
305        if (!lstat(to, &sb)) {
306                if ((S_ISDIR(sb.st_mode)) ? rmdir(to) : unlink(to)) {
307                        warn("can't remove %s", to);
308                        return (1);
309                }
310        }
311
312        /*
313         * (5)  The file hierarchy rooted in source_file shall be duplicated
314         *      as a file hierarchy rooted in the destination path...
315         */
316        if (lstat(from, &sb)) {
317                warn("%s", from);
318                return (1);
319        }
320
321        return (S_ISREG(sb.st_mode) ?
322            fastcopy(from, to, &sb) : copy(from, to));
323}
324
325int
326fastcopy_mv(rtems_shell_mv_globals* globals, char *from, char *to, struct stat *sbp)
327{
[2cdace7]328#ifndef __rtems__
[8192e4ff]329        struct timeval tval[2];
[2cdace7]330#endif
[8192e4ff]331        uint32_t blen;
332        static char *bp;
333        int nread, from_fd, to_fd;
334
335  blen = 0;
[0893220]336
[8192e4ff]337        if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
338                warn("%s", from);
339                return (1);
340        }
341        if ((to_fd =
342            open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
343                warn("%s", to);
344                (void)close(from_fd);
345                return (1);
346        }
347        if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
348                warn(NULL);
349                blen = 0;
350                (void)close(from_fd);
351                (void)close(to_fd);
352                return (1);
353        }
354        while ((nread = read(from_fd, bp, blen)) > 0)
355                if (write(to_fd, bp, nread) != nread) {
356                        warn("%s", to);
357                        goto err;
358                }
359        if (nread < 0) {
360                warn("%s", from);
361err:            if (unlink(to))
362                        warn("%s: remove", to);
363    (void)free(bp);
364                (void)close(from_fd);
365                (void)close(to_fd);
366                return (1);
367        }
[0893220]368
[8192e4ff]369  (void)free(bp);
370        (void)close(from_fd);
[2cdace7]371#ifndef __rtems__
[8192e4ff]372#ifdef xBSD4_4
373        TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
374        TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
375#else
376        tval[0].tv_sec = sbp->st_atime;
377        tval[1].tv_sec = sbp->st_mtime;
378        tval[0].tv_usec = 0;
379        tval[1].tv_usec = 0;
380#endif
[2cdace7]381#endif
382#ifndef __rtems__
[8192e4ff]383#ifdef _SRV5
384        if (utimes(to, tval))
385#else
386        if (futimes(to_fd, tval))
387#endif
388                warn("%s: set times", to);
389#endif
390        if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
391                if (errno != EPERM)
392                        warn("%s: set owner/group", to);
393                sbp->st_mode &= ~(S_ISUID | S_ISGID);
394        }
395        if (fchmod(to_fd, sbp->st_mode))
396                warn("%s: set mode", to);
397#if 0
398        if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
399                warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
400#endif
401        if (close(to_fd)) {
402                warn("%s", to);
403                return (1);
404        }
405
406        if (unlink(from)) {
407                warn("%s: remove", from);
408                return (1);
409        }
410
411        if (vflg)
412                printf("%s -> %s\n", from, to);
413
414        return (0);
415}
416
417int
418copy_mv(rtems_shell_mv_globals* globals, char *from, char *to)
419{
420  char* cp_argv[5] = { "mv", vflg ? "-PRpv" : "-PRp", "--", from, to };
421  char* rm_argv[4] = { "mv", "-rf", "--", from };
422  int result;
423
424  result = rtems_shell_main_cp(5, cp_argv);
425  if (result) {
426                warnx("%s: did not terminate normally", _PATH_CP);
427                return (1);
428  }
429  result = rtems_shell_main_rm(4, rm_argv);
430  if (result) {
431                warnx("%s: did not terminate normally", _PATH_RM);
432                return (1);
433  }
434#if 0
435        pid_t pid;
436        int status;
437
438        if ((pid = vfork()) == 0) {
439                execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to, NULL);
440                warn("%s", _PATH_CP);
441                _exit(1);
442        }
443        if (waitpid(pid, &status, 0) == -1) {
444                warn("%s: waitpid", _PATH_CP);
445                return (1);
446        }
447        if (!WIFEXITED(status)) {
448                warnx("%s: did not terminate normally", _PATH_CP);
449                return (1);
450        }
451        if (WEXITSTATUS(status)) {
452                warnx("%s: terminated with %d (non-zero) status",
453                    _PATH_CP, WEXITSTATUS(status));
454                return (1);
455        }
456        if (!(pid = vfork())) {
457                execl(_PATH_RM, "mv", "-rf", "--", from, NULL);
458                warn("%s", _PATH_RM);
459                _exit(1);
460        }
461        if (waitpid(pid, &status, 0) == -1) {
462                warn("%s: waitpid", _PATH_RM);
463                return (1);
464        }
465        if (!WIFEXITED(status)) {
466                warnx("%s: did not terminate normally", _PATH_RM);
467                return (1);
468        }
469        if (WEXITSTATUS(status)) {
470                warnx("%s: terminated with %d (non-zero) status",
471                    _PATH_RM, WEXITSTATUS(status));
472                return (1);
473        }
474#endif
475        return (0);
476}
477
478void
479usage_mv(rtems_shell_mv_globals* globals)
480{
481        (void)fprintf(stderr, "usage: %s [-fiv] source target\n"
482                "       %s [-fiv] source ... directory\n",
483                "mv", "mv");
484        exit(1);
485        /* NOTREACHED */
486}
487
488rtems_shell_cmd_t rtems_shell_MV_Command = {
489  "mv",                                      /* name */
490  "[-fiv] source target ...",                /* usage */
491  "files",                                   /* topic */
492  rtems_shell_main_mv,                       /* command */
493  NULL,                                      /* alias */
494  NULL                                       /* next */
495};
Note: See TracBrowser for help on using the repository browser.