File indexing completed on 2025-05-11 08:24:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #ifdef HAVE_CONFIG_H
0036 #include "config.h"
0037 #endif
0038
0039 #if 0
0040 #ifndef lint
0041 __COPYRIGHT(
0042 "@(#) Copyright (c) 1988, 1993, 1994\n\
0043 The Regents of the University of California. All rights reserved.\n");
0044 #endif
0045
0046 #ifndef lint
0047 #if 0
0048 static char sccsid[] = "@(#)cp.c 8.5 (Berkeley) 4/29/95";
0049 #else
0050 __RCSID("$NetBSD: cp.c,v 1.39 2005/10/24 12:59:07 kleink Exp $");
0051 #endif
0052 #endif
0053 #endif
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 #include <rtems.h>
0070 #include <rtems/shell.h>
0071 #include <rtems/shellconfig.h>
0072 #define __need_getopt_newlib
0073 #include <getopt.h>
0074
0075 #include <sys/param.h>
0076 #include <sys/stat.h>
0077
0078 #include "err.h"
0079 #include <errno.h>
0080 #include "fts.h"
0081 #include <limits.h>
0082 #include <signal.h>
0083 #include <stdio.h>
0084 #include <stdlib.h>
0085 #include <string.h>
0086 #include <unistd.h>
0087
0088 #include "internal.h"
0089
0090 #include "extern-cp.h"
0091
0092 #define S_ISTXT 0
0093
0094 #define STRIP_TRAILING_SLASH(p) { \
0095 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
0096 *--(p).p_end = 0; \
0097 }
0098
0099 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
0100
0101 static int Rflag, rflag;
0102
0103 static int main_cp(rtems_shell_cp_globals* cp_globals, int, char *[]);
0104 static int copy(rtems_shell_cp_globals* cp_globals, char *[], enum op, int);
0105 static int mastercmp(const FTSENT **, const FTSENT **);
0106
0107 int
0108 rtems_shell_main_cp(int argc, char *argv[])
0109 {
0110 rtems_shell_cp_globals cp_globals;
0111 memset (&cp_globals, 0, sizeof (cp_globals));
0112 Rflag = rflag = 0;
0113 if (setjmp (cp_globals.exit_jmp) == 0)
0114 return main_cp (&cp_globals, argc, argv);
0115 return 1;
0116 }
0117
0118 int
0119 main_cp(rtems_shell_cp_globals* cp_globals, int argc, char *argv[])
0120 {
0121 struct stat to_stat, tmp_stat;
0122 enum op type;
0123 int Hflag, Lflag, Pflag, ch, fts_options, r, have_trailing_slash;
0124 char *target;
0125 struct getopt_data getopt_reent;
0126
0127 Hflag = Lflag = Pflag = 0;
0128 memset(&getopt_reent, 0, sizeof(getopt_data));
0129
0130 while ((ch = getopt_r(argc, argv, "HLPRafilnprv", &getopt_reent)) != -1)
0131 switch (ch) {
0132 case 'H':
0133 Hflag = 1;
0134 Lflag = Pflag = 0;
0135 break;
0136 case 'L':
0137 Lflag = 1;
0138 Hflag = Pflag = 0;
0139 break;
0140 case 'P':
0141 Pflag = 1;
0142 Hflag = Lflag = 0;
0143 break;
0144 case 'R':
0145 Rflag = 1;
0146 break;
0147 case 'a':
0148 Pflag = 1;
0149 pflag = 1;
0150 Rflag = 1;
0151 Hflag = Lflag = 0;
0152 break;
0153 case 'f':
0154 fflag = 1;
0155 iflag = nflag = 0;
0156 break;
0157 case 'i':
0158 iflag = 1;
0159 fflag = nflag = 0;
0160 break;
0161 case 'l':
0162 lflag = 1;
0163 break;
0164 case 'n':
0165 nflag = 1;
0166 fflag = iflag = 0;
0167 break;
0168 case 'p':
0169 pflag = 1;
0170 break;
0171 case 'r':
0172 rflag = Lflag = 1;
0173 Hflag = Pflag = 0;
0174 break;
0175 case 'v':
0176 vflag = 1;
0177 break;
0178 default:
0179 usage(cp_globals);
0180 break;
0181 }
0182 argc -= getopt_reent.optind;
0183 argv += getopt_reent.optind;
0184
0185 if (argc < 2)
0186 usage(cp_globals);
0187
0188 fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
0189 if (Rflag && rflag)
0190 errx(exit_jump, 1, "the -R and -r options may not be specified together");
0191 if (rflag)
0192 Rflag = 1;
0193 if (Rflag) {
0194 if (Hflag)
0195 fts_options |= FTS_COMFOLLOW;
0196 if (Lflag) {
0197 fts_options &= ~FTS_PHYSICAL;
0198 fts_options |= FTS_LOGICAL;
0199 }
0200 } else {
0201 fts_options &= ~FTS_PHYSICAL;
0202 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
0203 }
0204 #if 0
0205 (void)signal(SIGINFO, siginfo);
0206 #endif
0207
0208
0209 target = argv[--argc];
0210 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
0211 errx(exit_jump, 1, "%s: name too long", target);
0212 to.p_end = to.p_path + strlen(to.p_path);
0213 if (to.p_path == to.p_end) {
0214 *to.p_end++ = '.';
0215 *to.p_end = 0;
0216 }
0217 have_trailing_slash = (to.p_end[-1] == '/');
0218 if (have_trailing_slash)
0219 STRIP_TRAILING_SLASH(to);
0220 to.target_end = to.p_end;
0221
0222
0223 argv[argc] = NULL;
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 r = stat(to.p_path, &to_stat);
0240 if (r == -1 && errno != ENOENT)
0241 err(exit_jump, 1, "%s", to.p_path);
0242 if (r == -1 || !S_ISDIR(to_stat.st_mode)) {
0243
0244
0245
0246 if (argc > 1)
0247 errx(exit_jump, 1, "%s is not a directory", to.p_path);
0248
0249
0250
0251
0252
0253
0254
0255
0256 if (r == -1) {
0257 if (Rflag && (Lflag || Hflag))
0258 #ifdef __rtems__
0259 (void)
0260 #endif
0261 stat(*argv, &tmp_stat);
0262 else
0263 #ifdef __rtems__
0264 (void)
0265 #endif
0266 lstat(*argv, &tmp_stat);
0267
0268 if (S_ISDIR(tmp_stat.st_mode) && Rflag)
0269 type = DIR_TO_DNE;
0270 else
0271 type = FILE_TO_FILE;
0272 } else
0273 type = FILE_TO_FILE;
0274
0275 if (have_trailing_slash && type == FILE_TO_FILE) {
0276 if (r == -1)
0277 errx(exit_jump, 1, "directory %s does not exist",
0278 to.p_path);
0279 else
0280 errx(exit_jump, 1, "%s is not a directory", to.p_path);
0281 }
0282 } else
0283
0284
0285
0286 type = FILE_TO_DIR;
0287
0288 return copy(cp_globals, argv, type, fts_options);
0289 }
0290
0291 int
0292 copy(rtems_shell_cp_globals* cp_globals,
0293 char *argv[], enum op type, int fts_options)
0294 {
0295 struct stat to_stat;
0296 FTS *ftsp;
0297 FTSENT *curr;
0298 int base = 0, dne, badcp, rval;
0299 size_t nlen;
0300 char *p, *target_mid;
0301 mode_t mask, mode;
0302
0303
0304
0305
0306
0307 mask = ~umask(0777);
0308 umask(~mask);
0309
0310 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
0311 err(exit_jump, 1, "fts_open");
0312 for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
0313 switch (curr->fts_info) {
0314 case FTS_NS:
0315 case FTS_DNR:
0316 case FTS_ERR:
0317 warnx("%s: %s",
0318 curr->fts_path, strerror(curr->fts_errno));
0319 #ifdef __rtems__
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332 rval = 1;
0333 #else
0334 badcp = rval = 1;
0335 #endif
0336 continue;
0337 case FTS_DC:
0338 warnx("%s: directory causes a cycle", curr->fts_path);
0339 #ifdef __rtems__
0340 rval = 1;
0341 #else
0342 badcp = rval = 1;
0343 #endif
0344 continue;
0345 default:
0346 ;
0347 }
0348
0349
0350
0351
0352
0353 if (type != FILE_TO_FILE) {
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373 if (curr->fts_level == FTS_ROOTLEVEL) {
0374 if (type != DIR_TO_DNE) {
0375 p = strrchr(curr->fts_path, '/');
0376 base = (p == NULL) ? 0 :
0377 (int)(p - curr->fts_path + 1);
0378
0379 if (!strcmp(&curr->fts_path[base],
0380 ".."))
0381 base += 1;
0382 } else
0383 base = curr->fts_pathlen;
0384 }
0385
0386 p = &curr->fts_path[base];
0387 nlen = curr->fts_pathlen - base;
0388 target_mid = to.target_end;
0389 if (*p != '/' && target_mid[-1] != '/')
0390 *target_mid++ = '/';
0391 *target_mid = 0;
0392 if (target_mid - to.p_path + nlen >= PATH_MAX) {
0393 warnx("%s%s: name too long (not copied)",
0394 to.p_path, p);
0395 #ifdef __rtems__
0396 rval = 1;
0397 #else
0398 badcp = rval = 1;
0399 #endif
0400 continue;
0401 }
0402 (void)strncat(target_mid, p, nlen);
0403 to.p_end = target_mid + nlen;
0404 *to.p_end = 0;
0405 STRIP_TRAILING_SLASH(to);
0406 }
0407
0408 if (curr->fts_info == FTS_DP) {
0409
0410
0411
0412
0413
0414 if (!curr->fts_number)
0415 continue;
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425 if (pflag) {
0426 if (setfile(cp_globals, curr->fts_statp, -1))
0427 rval = 1;
0428 if (preserve_dir_acls(curr->fts_statp,
0429 curr->fts_accpath, to.p_path) != 0)
0430 rval = 1;
0431 } else {
0432 mode = curr->fts_statp->st_mode;
0433 if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
0434 ((mode | S_IRWXU) & mask) != (mode & mask))
0435 if (chmod(to.p_path, mode & mask) != 0){
0436 warn("chmod: %s", to.p_path);
0437 rval = 1;
0438 }
0439 }
0440 continue;
0441 }
0442
0443
0444 if (stat(to.p_path, &to_stat) == -1)
0445 dne = 1;
0446 else {
0447 if (to_stat.st_dev == curr->fts_statp->st_dev &&
0448 to_stat.st_ino == curr->fts_statp->st_ino) {
0449 warnx("%s and %s are identical (not copied).",
0450 to.p_path, curr->fts_path);
0451 #ifdef __rtems__
0452 rval = 1;
0453 #else
0454 badcp = rval = 1;
0455 #endif
0456 if (S_ISDIR(curr->fts_statp->st_mode))
0457 (void)fts_set(ftsp, curr, FTS_SKIP);
0458 continue;
0459 }
0460 if (!S_ISDIR(curr->fts_statp->st_mode) &&
0461 S_ISDIR(to_stat.st_mode)) {
0462 warnx("cannot overwrite directory %s with "
0463 "non-directory %s",
0464 to.p_path, curr->fts_path);
0465 #ifdef __rtems__
0466 rval = 1;
0467 #else
0468 badcp = rval = 1;
0469 #endif
0470 continue;
0471 }
0472 dne = 0;
0473 }
0474
0475 switch (curr->fts_statp->st_mode & S_IFMT) {
0476 case S_IFLNK:
0477
0478 if ((fts_options & FTS_LOGICAL) ||
0479 ((fts_options & FTS_COMFOLLOW) &&
0480 curr->fts_level == 0)) {
0481 if (copy_file(cp_globals, curr, dne))
0482 badcp = rval = 1;
0483 } else {
0484 if (copy_link(cp_globals, curr, !dne))
0485 badcp = rval = 1;
0486 }
0487 break;
0488 case S_IFDIR:
0489 if (!Rflag) {
0490 warnx("%s is a directory (not copied).",
0491 curr->fts_path);
0492 (void)fts_set(ftsp, curr, FTS_SKIP);
0493 badcp = rval = 1;
0494 break;
0495 }
0496
0497
0498
0499
0500
0501
0502
0503
0504 if (dne) {
0505 if (mkdir(to.p_path,
0506 curr->fts_statp->st_mode | S_IRWXU) < 0)
0507 err(exit_jump, 1, "%s", to.p_path);
0508 } else if (!S_ISDIR(to_stat.st_mode)) {
0509 errno = ENOTDIR;
0510 err(exit_jump, 1, "%s", to.p_path);
0511 }
0512
0513
0514
0515
0516
0517 curr->fts_number = pflag || dne;
0518 break;
0519 case S_IFBLK:
0520 case S_IFCHR:
0521 if (Rflag) {
0522 if (copy_special(cp_globals, curr->fts_statp, !dne))
0523 badcp = rval = 1;
0524 } else {
0525 if (copy_file(cp_globals, curr, dne))
0526 badcp = rval = 1;
0527 }
0528 break;
0529 case S_IFSOCK:
0530 warnx("%s is a socket (not copied).",
0531 curr->fts_path);
0532 case S_IFIFO:
0533 if (Rflag) {
0534 if (copy_fifo(cp_globals, curr->fts_statp, !dne))
0535 badcp = rval = 1;
0536 } else {
0537 if (copy_file(cp_globals, curr, dne))
0538 badcp = rval = 1;
0539 }
0540 break;
0541 default:
0542 if (copy_file(cp_globals, curr, dne))
0543 badcp = rval = 1;
0544 break;
0545 }
0546 if (vflag && !badcp)
0547 (void)printf("%s -> %s\n", curr->fts_path, to.p_path);
0548 }
0549 if (errno)
0550 err(exit_jump, 1, "fts_read");
0551 fts_close(ftsp);
0552 return (rval);
0553 }
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563 int
0564 mastercmp(const FTSENT **a, const FTSENT **b)
0565 {
0566 int a_info, b_info;
0567
0568 a_info = (*a)->fts_info;
0569 if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
0570 return (0);
0571 b_info = (*b)->fts_info;
0572 if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
0573 return (0);
0574 if (a_info == FTS_D)
0575 return (-1);
0576 if (b_info == FTS_D)
0577 return (1);
0578 return (0);
0579 }
0580
0581 rtems_shell_cmd_t rtems_shell_CP_Command = {
0582 "cp",
0583 "cp [-R [-H | -L | -P]] [-f | -i] [-pv] src target",
0584 "files",
0585 rtems_shell_main_cp,
0586 NULL,
0587 NULL
0588 };
0589