¡¾ÇóÖú¡¿linux
´ó¼ÒºÃ£¬Ã°ÃÁÌá³öÒ»¸öÎÊÌâ.ÔÚlinuxϵͳÖУ¬Èç¹ûʵÏÖÎļþµÄ¿½±´,ʹÓõÄÃüÁîÊÇcp£¬ÎÒÔÚÄÚºËÔ´´úÂëÖÐûÓÐÕÒµ½Õâ¸öÃüÁîµÄÔ´´úÂ룬ÇëÎÊÓÐÄÄλ¸ßÊÖÖªµÀ£¿ÎÒÔÚÄÚºËÔ´´úÂëµÄÔĶÁÖз¢ÏÖÁË´ò¿ª¹Ø±ÕÎļþÒÔ¼°¶ÁдÎļþµÄÔ´´úÂ룬µ«ÊÇûÓÐ˵ʵÏÖÎļþ¿½±´µÄÔ´´úÂë¡£Õâ¸ö¿½±´µÄÖ÷Òª¹ý³ÌÊÇʲô£¿ÊDz»ÊÇͬʱ´ò¿ªÁ½¸öÎļþ£¬Ò»¸ö¶ÁÒ»¸öд£¬ÕâÑù¾ÍÍê³ÉÁËÎļþµÄ¿½±´ÄØ£¿ Ï£ÍûÖªÇéÈËÏà¸æ£¬Ð»Ð»£¡ extern int cp_main(int argc, char **argv){
struct stat source_stat;
struct stat dest_stat;
const char *last;
const char *dest;
int s_flags;
int d_flags;
int flags;
int status = 0;
/* Since these are enums, #if tests will not work. So use assert()s. */
assert(FILEUTILS_PRESERVE_STATUS == 1);
assert(FILEUTILS_DEREFERENCE == 2);
assert(FILEUTILS_RECUR == 4);
assert(FILEUTILS_FORCE == 8);
assert(FILEUTILS_INTERACTIVE == 16);
flags = bb_getopt_ulflags(argc, argv, cp_opts);
if (flags & 32) {
flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
}
flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
if (optind + 2 > argc) {
bb_show_usage();
}
last = argv[argc - 1];
argv += optind;
/* If there are only two arguments and... */
if (optind + 2 == argc) {
s_flags = cp_mv_stat2(*argv, &source_stat,
(flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
exit(EXIT_FAILURE);
}
/* ...if neither is a directory or... */
if ( !((s_flags | d_flags) & 2) ||
/* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
/* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
/* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
) {
/* ...do a simple copy. */
dest = last;
goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
}
}
do {
dest = concat_path_file(last, bb_get_last_path_component(*argv));
DO_COPY:
if (copy_file(*argv, dest, flags) < 0) {
status = 1;
}
if (*++argv == last) {
break;
}
free((void *) dest);
} while (1);
exit(status);
} ÉÏÃæµÄÊÇÎÒÔÚbusyboxÖи´ÖƹýÀ´µÄ¡£ ÔÚÄĸöĿ¼ÏÂÁË cpµÄÔ´Âë²»ÊÇÔÚfileutilsÖÐÂð£¿ÈçÏ£º
[FONT=Courier New][COLOR=RoyalBlue]
int fd1, fd2, len;
char buf[BUF_SIZ];
fd1 = open("filename1", O_READONLY);
fd2 = open("filename2", O_WRITEONLY);
while ((len = read(fd1, buf, BUF_SIZ)) > 0) {
write(fd2, buf, len);
if (len < BUF_SIZ)
break;
}
close(fd2);
close(fd1);
[/COLOR][/FONT]
ÓбØÒªÄÇô¸´ÔÓÂ𣿲»¹ýÕâ¸öÌû×Ó¸úÄÚºËÎ޹أ¬Ó¦¸Ã×ªÒÆÁË¡£ ÓÐûÓÐÈ˸æËßÎÒFILEUTILS_PRESERVE_STATUSÕ⼸¸öºê±íʾʲôÒâ˼£¿
ÎÒÖ»ÖªµÀlinkÆäËû¼¸¸ö²»ÈÏʶ£¡
Ò³:
[1]