X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=coreutils%2Fln.c;h=229c682185ef029a0bb80deeacc05b30c23d453e;hb=e2e56c7c4129de7d20df42e8239fd304c81ef29b;hp=0640c666611e9a6d5d475643717864a5cac3ad9d;hpb=cfc0ad4260149f5cdad59c79051c3779d42b2ff5;p=oweals%2Fbusybox.git diff --git a/coreutils/ln.c b/coreutils/ln.c index 0640c6666..229c68218 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c @@ -2,7 +2,7 @@ /* * Mini ln implementation for busybox * - * Copyright (C) 1999-2003 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,33 +21,33 @@ */ /* BB_AUDIT SUSv3 compliant */ -/* BB_AUDIT GNU options missing: -b, -d, -F, -i, -S, and -v. */ +/* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */ /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */ -/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) - * - * Fixed bug involving -n option. Essentially, -n was always in effect. - */ - +#include #include #include +#include #include "busybox.h" #define LN_SYMLINK 1 #define LN_FORCE 2 #define LN_NODEREFERENCE 4 +#define LN_BACKUP 8 +#define LN_SUFFIX 16 -extern int ln_main(int argc, char **argv) +int ln_main(int argc, char **argv) { 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 *); - flag = bb_getopt_ulflags(argc, argv, "sfn"); + flag = bb_getopt_ulflags(argc, argv, "sfnbS:", &suffix); if (argc == optind) { bb_show_usage(); @@ -73,15 +73,30 @@ extern int ln_main(int argc, char **argv) free(src_name); src_name = src; } - - if (stat(*argv, &statbuf)) { - bb_perror_msg(*argv); + if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) { + bb_perror_msg("%s", *argv); status = EXIT_FAILURE; free(src_name); continue; } - if (flag & LN_FORCE) { + 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); } @@ -89,25 +104,15 @@ extern int ln_main(int argc, char **argv) if (flag & LN_SYMLINK) { link_func = symlink; } - + if (link_func(*argv, src) != 0) { - bb_perror_msg(src); + bb_perror_msg("%s", src); status = EXIT_FAILURE; } free(src_name); - + } while ((++argv)[1]); return status; } - - - - - - - - - -