micro-utils/coreutils/ln.c
2023-11-07 16:21:46 +03:00

67 lines
1019 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(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "sf")) != -1) {
switch (opt) {
case 's':
s_flag = 1;
break;
case 'f':
f_flag = 1;
break;
default:
printf("ln [src] [dst]\n\t[-f Force] [-s Symbolic]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc == 2) {
if (ln(argv[0], argv[1])) {
char *new_path = mu_make_path("ln", argv[1], argv[0]);
if (new_path == NULL)
return 1;
if (ln(argv[0], new_path)) {
free(new_path);
fprintf(stderr, "ln: %s %s\n", argv[1], strerror(errno));
return 1;
}
free(new_path);
}
}
else {
fprintf(stderr, "ln: missing operand\n");
return 1;
}
return 0;
}