Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/coreutils/cp.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 815 by niro, Sat Sep 1 22:45:15 2007 UTC revision 816 by niro, Fri Apr 24 18:33:46 2009 UTC
# Line 3  Line 3 
3   * Mini cp implementation for busybox   * Mini cp implementation for busybox
4   *   *
5   * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>   * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6     * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
7   *   *
8   * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.   * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
9   */   */
# Line 14  Line 15 
15   * Size reduction.   * Size reduction.
16   */   */
17    
18  #include "busybox.h"  #include "libbb.h"
19  #include "libcoreutils/coreutils.h"  #include "libcoreutils/coreutils.h"
20    
21    /* This is a NOEXEC applet. Be very careful! */
22    
23    
24    int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25  int cp_main(int argc, char **argv)  int cp_main(int argc, char **argv)
26  {  {
27   struct stat source_stat;   struct stat source_stat;
# Line 32  int cp_main(int argc, char **argv) Line 37  int cp_main(int argc, char **argv)
37   OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),   OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
38   OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),   OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
39   OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),   OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
  OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),  
40   };   };
41    
42   // Soft- and hardlinking don't mix   // Need at least two arguments
43     // Soft- and hardlinking doesn't mix
44   // -P and -d are the same (-P is POSIX, -d is GNU)   // -P and -d are the same (-P is POSIX, -d is GNU)
45   // -r and -R are the same   // -r and -R are the same
46     // -R (and therefore -r) turns on -d (coreutils does this)
47   // -a = -pdR   // -a = -pdR
48   opt_complementary = "?:l--s:s--l:Pd:rR:apdR";   opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL";
49   flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL");   flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPH");
50   /* Default behavior of cp is to dereference, so we don't have to do   argc -= optind;
51   * anything special when we are given -L.   argv += optind;
52   * The behavior of -H is *almost* like -L, but not quite, so let's   flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */
53   * just ignore it too for fun.   /* coreutils 6.9 compat:
54   if (flags & OPT_L) ...   * by default, "cp" derefs symlinks (creates regular dest files),
55     * but "cp -R" does not. We switch off deref if -r or -R (see above).
56     * However, "cp -RL" must still deref symlinks: */
57     if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
58     flags |= FILEUTILS_DEREFERENCE;
59     /* The behavior of -H is *almost* like -L, but not quite, so let's
60     * just ignore it too for fun. TODO.
61   if (flags & OPT_H) ... // deref command-line params only   if (flags & OPT_H) ... // deref command-line params only
62   */   */
63    
64   flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */  #if ENABLE_SELINUX
65     if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
66   if (optind + 2 > argc) {   selinux_or_die();
  bb_show_usage();  
67   }   }
68    #endif
69    
70   last = argv[argc - 1];   last = argv[argc - 1];
  argv += optind;  
   
71   /* If there are only two arguments and...  */   /* If there are only two arguments and...  */
72   if (optind + 2 == argc) {   if (argc == 2) {
73   s_flags = cp_mv_stat2(*argv, &source_stat,   s_flags = cp_mv_stat2(*argv, &source_stat,
74        (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);        (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
75   if (s_flags < 0)   if (s_flags < 0)
# Line 74  int cp_main(int argc, char **argv) Line 84  int cp_main(int argc, char **argv)
84   ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)   ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
85   ) {   ) {
86   /* ...do a simple copy.  */   /* ...do a simple copy.  */
87   dest = xstrdup(last);   dest = last;
88   goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */   goto DO_COPY; /* NB: argc==2 -> *++argv==last */
89   }   }
90   }   }
91    
92   do {   while (1) {
93   dest = concat_path_file(last, bb_get_last_path_component(*argv));   dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
94   DO_COPY:   DO_COPY:
95   if (copy_file(*argv, dest, flags) < 0) {   if (copy_file(*argv, dest, flags) < 0) {
96   status = 1;   status = 1;
97   }   }
98     if (*++argv == last) {
99     /* possibly leaking dest... */
100     break;
101     }
102   free((void*)dest);   free((void*)dest);
103   } while (*++argv != last);   }
104    
105     /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
106   return status;   return status;
107  }  }

Legend:
Removed from v.815  
changed lines
  Added in v.816