ln-stub.c (865B)
- #define _POSIX_C_SOURCE 200809L
- #include <stdio.h> // fprintf
- #include <unistd.h> // getopt, symlink, link
- #include <string.h> // strerror
- #include <errno.h>
- #include <stdbool.h>
- int
- main(int argc, char *argv[])
- {
- bool opt_s = false;
- int c = -1;
- while((c = getopt(argc, argv, ":s")) != -1)
- {
- switch(c)
- {
- case 's':
- opt_s = true;
- break;
- case '?':
- fprintf(stderr, "ln: Unknown option '-%c'\n", optopt);
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if(argc != 2)
- {
- fprintf(stderr, "Usage: ln [-s] src dest\n");
- return 1;
- }
- if(opt_s)
- {
- if(symlink(argv[0], argv[1]) < 0)
- {
- fprintf(stderr, "ln: Failed creating symlink: %s\n", strerror(errno));
- return 1;
- }
- }
- else
- {
- if(link(argv[0], argv[1]) < 0)
- {
- fprintf(stderr, "ln: Failed creating (hard) link: %s\n", strerror(errno));
- return 1;
- }
- }
- return 0;
- }