[BusyBox] [PATCH] Add -b and -S flags to ln.c.
Matthew S. Wood
mwood at realmsys.com
Fri Apr 15 17:30:29 MDT 2005
Hello,
The following patch adds support for the -S and -b flags to `ln'. These flags [especially -b] are used extensively in Debian pre and post installation scripts.
Regards,
Matt
-------------- next part --------------
--- ./coreutils/ln.c.orig 2005-04-15 17:00:38.000000000 -0600
+++ ./coreutils/ln.c 2005-04-15 17:01:52.000000000 -0600
@@ -21,21 +21,31 @@
*/
/* 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 at codepoet.org)
+/* Apr 15, 2004 Matthew S. Wood (mwood at realmsys.com)
+ *
+ * Implement '-b' (backup) flag.
+ * Implement '-S' (backup suffix) flag.
+ *
+ *
+ * Mar 16, 2003 Manuel Novoa III (mjn3 at codepoet.org)
*
* Fixed bug involving -n option. Essentially, -n was always in effect.
*/
+#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
+#include <errno.h>
#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)
{
@@ -44,10 +54,11 @@
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();
@@ -80,7 +91,22 @@
continue;
}
- if (flag & LN_FORCE) {
+ if (flag & LN_BACKUP) {
+ int backup_len = strlen(src) + strlen(suffix) + 2;
+ char *backup = xmalloc(backup_len);
+ snprintf(backup, backup_len, "%s%s", src, suffix);
+ if (rename(src, backup) < 0 && errno != ENOENT) {
+ bb_perror_msg(src);
+ status = EXIT_FAILURE;
+ break;
+ }
+ /*
+ * When the source and dest are both hard links, a rename
+ * may succeed even though nothing happened. Therefore,
+ * always unlink().
+ */
+ unlink(src);
+ } else if (flag & LN_FORCE) {
unlink(src);
}
--- ./include/usage.h.orig 2005-04-15 17:00:15.000000000 -0600
+++ ./include/usage.h 2005-04-15 16:54:47.000000000 -0600
@@ -1415,7 +1415,9 @@
"Options:\n" \
"\t-s\tmake symbolic links instead of hard links\n" \
"\t-f\tremove existing destination files\n" \
- "\t-n\tno dereference symlinks - treat like normal file"
+ "\t-n\tno dereference symlinks - treat like normal file\n" \
+ "\t-b\tmake a backup of the target (if exists) before link operation\n" \
+ "\t-S suffix\tuse suffix instead of ~ when making backup files"
#define ln_example_usage \
"$ ln -s BusyBox /tmp/ls\n" \
"$ ls -l /tmp/ls\n" \
More information about the busybox
mailing list