diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:06:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:06:04 +0000 |
commit | 2f0649f6fe411d7e07c8d56cf8ea56db53536da8 (patch) | |
tree | 778611fb52176dce1ad06c68e87b2cb348ca0f7b /usr/utils/ln.c | |
parent | Initial commit. (diff) | |
download | klibc-upstream/2.0.13.tar.xz klibc-upstream/2.0.13.zip |
Adding upstream version 2.0.13.upstream/2.0.13upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | usr/utils/ln.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/usr/utils/ln.c b/usr/utils/ln.c new file mode 100644 index 0000000..e826eb8 --- /dev/null +++ b/usr/utils/ln.c @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + +#include <linux/limits.h> + +int main(int argc, char *argv[]) +{ + int c, s, f; + char *p; + struct stat sb; + + s = f = 0; + do { + c = getopt(argc, argv, "sf"); + if (c == EOF) + break; + + switch (c) { + + case 's': + s = 1; + break; + case 'f': + f = 1; + break; + case '?': + fprintf(stderr, "%s: invalid option -%c\n", + argv[0], optopt); + return 1; + } + + } while (1); + + if (optind == argc) { + fprintf(stderr, "Usage: %s [-s] [-f] target link\n", argv[0]); + return 1; + } + + memset(&sb, 0, sizeof(struct stat)); + if (stat(argv[argc - 1], &sb) < 0 && argc - optind > 2) { + if (!(S_ISDIR(sb.st_mode))) { + fprintf(stderr, + "multiple targets and %s is not a directory\n", + argv[argc - 1]); + return 1; + } + } + + for (c = optind; c < argc - 1; c++) { + char target[PATH_MAX]; + + p = strrchr(argv[c], '/'); + p++; + + if (S_ISDIR(sb.st_mode)) + snprintf(target, PATH_MAX, "%s/%s", argv[argc - 1], p); + else + snprintf(target, PATH_MAX, "%s", argv[argc - 1]); + + if (f) + unlink(target); + + if (s) { + if (symlink(argv[c], target) == -1) + perror(target); + } else { + if (link(argv[c], target) == -1) + perror(target); + } + } + + return 0; +} |