X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fln.c;h=229c682185ef029a0bb80deeacc05b30c23d453e;hb=e2e56c7c4129de7d20df42e8239fd304c81ef29b;hp=6d41cce62c67dd07602379ebaf7f697d971b2997;hpb=cbe31dace5fb24304694d399b9eb267fbe752516;p=oweals%2Fbusybox.git diff --git a/coreutils/ln.c b/coreutils/ln.c index 6d41cce62..229c68218 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c @@ -2,8 +2,7 @@ /* * Mini ln implementation for busybox * - * Copyright (C) 1999,2000,2001 by Lineo, inc. - * Written by Erik Andersen , + * Copyright (C) 1999-2004 by Erik Andersen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,114 +20,99 @@ * */ +/* BB_AUDIT SUSv3 compliant */ +/* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */ +/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */ + #include -#include -#include #include -#include #include +#include #include "busybox.h" -#define BB_DECLARE_EXTERN -#define bb_need_not_a_directory -#include "messages.c" - -static const int LN_SYMLINK = 1; -static const int LN_FORCE = 2; -static const int LN_NODEREFERENCE = 4; +#define LN_SYMLINK 1 +#define LN_FORCE 2 +#define LN_NODEREFERENCE 4 +#define LN_BACKUP 8 +#define LN_SUFFIX 16 -/* - * linkDestName is where the link points to, - * linkSrcName is the name of the link to be created. - */ -static int fs_link(const char *link_destname, const char *link_srcname, - const int flag) +int ln_main(int argc, char **argv) { - int status; - int src_is_dir; + int status = EXIT_SUCCESS; + int flag; + char *last; char *src_name; + char *src; + char *suffix = "~"; + struct stat statbuf; + int (*link_func)(const char *, const char *); - if (link_destname==NULL) - return(FALSE); - - src_name = (char *) xmalloc(strlen(link_srcname)+strlen(link_destname)+1); + flag = bb_getopt_ulflags(argc, argv, "sfnbS:", &suffix); - if (link_srcname==NULL) - strcpy(src_name, link_destname); - else - strcpy(src_name, link_srcname); + if (argc == optind) { + bb_show_usage(); + } - if (flag&LN_NODEREFERENCE) - src_is_dir = is_directory(src_name, TRUE, NULL); - else - src_is_dir = is_directory(src_name, FALSE, NULL); - - if ((src_is_dir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) { - char* srcdir_name; + last = argv[argc - 1]; + argv += optind; - srcdir_name = xstrdup(link_destname); - strcat(src_name, "/"); - strcat(src_name, get_last_path_component(srcdir_name)); - free(srcdir_name); - } - - if (flag&LN_FORCE) - unlink(src_name); - - if (flag&LN_SYMLINK) - status = symlink(link_destname, src_name); - else - status = link(link_destname, src_name); - - if (status != 0) { - perror_msg(src_name); - return(FALSE); + if (argc == optind + 1) { + *--argv = last; + last = bb_get_last_path_component(bb_xstrdup(last)); } - return(TRUE); -} -extern int ln_main(int argc, char **argv) -{ - int status = EXIT_SUCCESS; - int flag = 0; - int opt; - - /* Parse any options */ - while ((opt=getopt(argc, argv, "sfn")) != -1) { - switch(opt) { - case 's': - flag |= LN_SYMLINK; - break; - case 'f': - flag |= LN_FORCE; - break; - case 'n': - flag |= LN_NODEREFERENCE; - break; - default: - show_usage(); + do { + src_name = NULL; + src = last; + + if (is_directory(src, + (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, + NULL)) { + src_name = bb_xstrdup(*argv); + src = concat_path_file(src, bb_get_last_path_component(src_name)); + free(src_name); + src_name = src; } - } - if (optind > (argc-1)) { - show_usage(); - } - if (optind == (argc-1)) { - if (fs_link(argv[optind], - get_last_path_component(argv[optind]), flag)==FALSE) + if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) { + bb_perror_msg("%s", *argv); status = EXIT_FAILURE; - } - while(optind<(argc-1)) { - if (fs_link(argv[optind], argv[argc-1], flag)==FALSE) + free(src_name); + continue; + } + + if (flag & LN_BACKUP) { + char *backup; + backup = bb_xasprintf("%s%s", src, suffix); + if (rename(src, backup) < 0 && errno != ENOENT) { + bb_perror_msg("%s", src); + status = EXIT_FAILURE; + free(backup); + continue; + } + free(backup); + /* + * When the source and dest are both hard links to the same + * inode, a rename may succeed even though nothing happened. + * Therefore, always unlink(). + */ + unlink(src); + } else if (flag & LN_FORCE) { + unlink(src); + } + + link_func = link; + if (flag & LN_SYMLINK) { + link_func = symlink; + } + + if (link_func(*argv, src) != 0) { + bb_perror_msg("%s", src); status = EXIT_FAILURE; - optind++; - } - exit(status); -} + } -/* -Local Variables: -c-file-style: "linux" -c-basic-offset: 4 -tab-width: 4 -End: -*/ + free(src_name); + + } while ((++argv)[1]); + + return status; +}