micro-utils/coreutils/ln.c
Your Name e61dc1ffaa fix
2023-11-02 19:49:59 +03:00

60 lines
990 B
C

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "make_path.h"
/* Symbolic */
unsigned int s_flag;
unsigned int f_flag;
int ln(const char *src, const char *dst) {
if (f_flag)
unlink(dst);
if (s_flag)
return symlink(src, dst);
else
return link(src, dst);
}
int main(const int argc, const char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp("-s", argv[i]))
s_flag = 1;
else if (!strcmp("-f", argv[i]))
f_flag = 1;
else if (!strcmp("--help", argv[i])) {
printf("ln [-f force] [-s symbolic] [TARGET] [LINK/DIR]\n");
return 0;
}
}
if (argc - i == 2) {
if (ln(argv[i], argv[i + 1])) {
char *new_path = mu_make_path("ln", argv[i + 1], argv[i]);
if (new_path == NULL)
return 1;
if (ln(argv[i], new_path)) {
free(new_path);
fprintf(stderr, "ln: %s %s\n", argv[i], strerror(errno));
return 1;
}
free(new_path);
}
}
return 0;
}