source: rtems/cpukit/libmisc/shell/main_mv.c @ b079fe33

4.104.115
Last change on this file since b079fe33 was b079fe33, checked in by Joel Sherrill <joel.sherrill@…>, on 10/02/08 at 20:16:16

2008-10-02 Joel Sherrill <joel.sherrill@…>

  • libmisc/shell/main_cp.c, libmisc/shell/main_ls.c, libmisc/shell/main_mv.c, libmisc/shell/main_netstats.c, libmisc/shell/main_rm.c, libmisc/shell/shell_script.c: Newlib > 1.16.0 requires need_getopt_newlib to be defined to get visibility on the reentrancy extensions.
  • Property mode set to 100644
File size: 12.5 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#define __need_getopt_newlib
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
66#include <err.h>
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
77#include "pathnames-mv.h"
78
79/* RTEMS specific changes */
80typedef struct {
81  int fflg, iflg, vflg;
82  int stdin_ok;
83  int exit_code;
84  jmp_buf exit_jmp;
85} rtems_shell_mv_globals;
86
87int     copy(char *, char *);
88int     do_move(char *, char *);
89int     fastcopy(char *, char *, struct stat *);
90void    usage(void);
91int     main(int, char *[]);
92
93#define fflg      globals->fflg
94#define iflg      globals->iflg
95#define vflg      globals->vflg
96#define stdin_ok  globals->stdin_ok
97#define exit_jump &(globals->exit_jmp)
98
99#include <setjmp.h>
100
101#define exit(ec) rtems_shell_mv_exit(globals, ec)
102
103void
104rtems_shell_mv_exit (rtems_shell_mv_globals* globals, int code)
105{
106  globals->exit_code = code;
107  longjmp (globals->exit_jmp, 1);
108}
109
110void strmode(mode_t mode, char *p);
111const char *user_from_uid(uid_t uid, int nouser);
112char *group_from_gid(gid_t gid, int nogroup);
113
114static int main_mv(rtems_shell_mv_globals* globals, int argc, char *argv[]);
115
116int rtems_shell_main_cp(int argc, char *argv[]);
117int rtems_shell_main_rm(int argc, char *argv[]);
118
119int
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)
131#define fastcopy(a1, a2, a3) fastcopy_mv(globals, a1, a2, a3)
132#define copy(a1, a2)         copy_mv(globals, a1, a2)
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));
154 
155/*      setprogname(argv[0]); */
156 
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{
328        struct timeval tval[2];
329        uint32_t blen;
330        static char *bp;
331        int nread, from_fd, to_fd;
332
333  blen = 0;
334 
335        if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
336                warn("%s", from);
337                return (1);
338        }
339        if ((to_fd =
340            open(to, O_CREAT | O_TRUNC | O_WRONLY, sbp->st_mode)) < 0) {
341                warn("%s", to);
342                (void)close(from_fd);
343                return (1);
344        }
345        if (!blen && !(bp = malloc(blen = sbp->st_blksize))) {
346                warn(NULL);
347                blen = 0;
348                (void)close(from_fd);
349                (void)close(to_fd);
350                return (1);
351        }
352        while ((nread = read(from_fd, bp, blen)) > 0)
353                if (write(to_fd, bp, nread) != nread) {
354                        warn("%s", to);
355                        goto err;
356                }
357        if (nread < 0) {
358                warn("%s", from);
359err:            if (unlink(to))
360                        warn("%s: remove", to);
361    (void)free(bp);
362                (void)close(from_fd);
363                (void)close(to_fd);
364                return (1);
365        }
366 
367  (void)free(bp);
368        (void)close(from_fd);
369#ifdef xBSD4_4
370        TIMESPEC_TO_TIMEVAL(&tval[0], &sbp->st_atimespec);
371        TIMESPEC_TO_TIMEVAL(&tval[1], &sbp->st_mtimespec);
372#else
373        tval[0].tv_sec = sbp->st_atime;
374        tval[1].tv_sec = sbp->st_mtime;
375        tval[0].tv_usec = 0;
376        tval[1].tv_usec = 0;
377#endif
378#if 0
379#ifdef _SRV5
380        if (utimes(to, tval))
381#else
382        if (futimes(to_fd, tval))
383#endif
384                warn("%s: set times", to);
385#endif
386        if (fchown(to_fd, sbp->st_uid, sbp->st_gid)) {
387                if (errno != EPERM)
388                        warn("%s: set owner/group", to);
389                sbp->st_mode &= ~(S_ISUID | S_ISGID);
390        }
391        if (fchmod(to_fd, sbp->st_mode))
392                warn("%s: set mode", to);
393#if 0
394        if (fchflags(to_fd, sbp->st_flags) && (errno != EOPNOTSUPP))
395                warn("%s: set flags (was: 0%07o)", to, sbp->st_flags);
396#endif
397        if (close(to_fd)) {
398                warn("%s", to);
399                return (1);
400        }
401
402        if (unlink(from)) {
403                warn("%s: remove", from);
404                return (1);
405        }
406
407        if (vflg)
408                printf("%s -> %s\n", from, to);
409
410        return (0);
411}
412
413int
414copy_mv(rtems_shell_mv_globals* globals, char *from, char *to)
415{
416  char* cp_argv[5] = { "mv", vflg ? "-PRpv" : "-PRp", "--", from, to };
417  char* rm_argv[4] = { "mv", "-rf", "--", from };
418  int result;
419
420  result = rtems_shell_main_cp(5, cp_argv);
421  if (result) {
422                warnx("%s: did not terminate normally", _PATH_CP);
423                return (1);
424  }
425  result = rtems_shell_main_rm(4, rm_argv);
426  if (result) {
427                warnx("%s: did not terminate normally", _PATH_RM);
428                return (1);
429  }
430#if 0
431        pid_t pid;
432        int status;
433
434        if ((pid = vfork()) == 0) {
435                execl(_PATH_CP, "mv", vflg ? "-PRpv" : "-PRp", "--", from, to, NULL);
436                warn("%s", _PATH_CP);
437                _exit(1);
438        }
439        if (waitpid(pid, &status, 0) == -1) {
440                warn("%s: waitpid", _PATH_CP);
441                return (1);
442        }
443        if (!WIFEXITED(status)) {
444                warnx("%s: did not terminate normally", _PATH_CP);
445                return (1);
446        }
447        if (WEXITSTATUS(status)) {
448                warnx("%s: terminated with %d (non-zero) status",
449                    _PATH_CP, WEXITSTATUS(status));
450                return (1);
451        }
452        if (!(pid = vfork())) {
453                execl(_PATH_RM, "mv", "-rf", "--", from, NULL);
454                warn("%s", _PATH_RM);
455                _exit(1);
456        }
457        if (waitpid(pid, &status, 0) == -1) {
458                warn("%s: waitpid", _PATH_RM);
459                return (1);
460        }
461        if (!WIFEXITED(status)) {
462                warnx("%s: did not terminate normally", _PATH_RM);
463                return (1);
464        }
465        if (WEXITSTATUS(status)) {
466                warnx("%s: terminated with %d (non-zero) status",
467                    _PATH_RM, WEXITSTATUS(status));
468                return (1);
469        }
470#endif
471        return (0);
472}
473
474void
475usage_mv(rtems_shell_mv_globals* globals)
476{
477        (void)fprintf(stderr, "usage: %s [-fiv] source target\n"
478                "       %s [-fiv] source ... directory\n",
479                "mv", "mv");
480        exit(1);
481        /* NOTREACHED */
482}
483
484rtems_shell_cmd_t rtems_shell_MV_Command = {
485  "mv",                                      /* name */
486  "[-fiv] source target ...",                /* usage */
487  "files",                                   /* topic */
488  rtems_shell_main_mv,                       /* command */
489  NULL,                                      /* alias */
490  NULL                                       /* next */
491};
Note: See TracBrowser for help on using the repository browser.