2019-02-21 13:26:40 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
2019-03-02 01:50:34 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
[[noreturn]] static void print_usage_and_exit()
|
|
|
|
|
{
|
|
|
|
|
printf("usage: ln [-s] <target> <link-path>\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
2019-02-21 13:26:40 +01:00
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
2019-03-02 01:50:34 +01:00
|
|
|
bool flag_symlink = false;
|
|
|
|
|
int opt;
|
|
|
|
|
while ((opt = getopt(argc, argv, "s")) != -1) {
|
|
|
|
|
switch (opt) {
|
|
|
|
|
case 's':
|
|
|
|
|
flag_symlink = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
print_usage_and_exit();
|
|
|
|
|
}
|
2019-02-21 13:26:40 +01:00
|
|
|
}
|
2019-03-02 01:50:34 +01:00
|
|
|
|
|
|
|
|
if ((optind + 1) >= argc)
|
|
|
|
|
print_usage_and_exit();
|
|
|
|
|
|
|
|
|
|
if (flag_symlink) {
|
|
|
|
|
int rc = symlink(argv[optind], argv[optind + 1]);
|
|
|
|
|
if (rc < 0) {
|
|
|
|
|
perror("symlink");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 13:26:40 +01:00
|
|
|
int rc = link(argv[1], argv[2]);
|
|
|
|
|
if (rc < 0) {
|
|
|
|
|
perror("link");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|