-
Notifications
You must be signed in to change notification settings - Fork 0
/
ln.c
128 lines (122 loc) · 3.98 KB
/
ln.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2021 Benjamin Stürz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#define PROG_NAME "ln"
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include "errprintf.h"
int main(int argc, char* argv[]) {
bool force = false, symbolic = false;
int opt_LP = '\0';
int option;
while ((option = getopt(argc, argv, ":fsLP")) != -1) {
switch (option) {
case 'f': force = true; break;
case 's': symbolic = true; break;
case 'L':
case 'P': opt_LP = option; break;
default: goto print_usage;
}
}
if ((argc - optind) < 2) {
print_usage:
fputs("Usage: ln [-sf] [-L|-P] source_file target_file\n", stderr);
fputs(" ln [-sf] [-L|-P] source_file... target_dir\n", stderr);
return 1;
}
const char* target = argv[argc - 1];
struct stat st_target;
int error = stat(target, &st_target) ? errno : 0;
if ((argc - optind) == 2 && (!error || ((st_target.st_mode & S_IFMT) != S_IFDIR))) {
const char* path = argv[optind];
if (symbolic) {
if (symlink(path, target) != 0) {
errprintf("failed to create symbolic link '%s'", target);
return 1;
} else return 0;
} else {
if (link(path, target) != 0) {
errprintf("failed to create hard-link '%s'", target);
return 1;
} else return 0;
}
}
if (error) {
fprintf(stderr, "ln: failed to access '%s': %s\n", target, strerror(error));
return 1;
} else if ((st_target.st_mode & S_IFMT) != S_IFDIR) {
fprintf(stderr, "ln: target '%s': %s\n", target, strerror(ENOTDIR));
return 1;
}
const size_t len_target = strlen(target);
char* buffer = malloc(len_target + PATH_MAX + 2);
if (!buffer) {
perror("ln: failed to allocate buffer");
return 1;
}
int ec = 0;
memcpy(buffer, target, len_target);
buffer[len_target] = '/';
for (; optind < (argc - 1); ++optind) {
struct stat st;
const char* path = argv[optind];
strncpy(buffer + len_target + 1, path, PATH_MAX);
error = stat(buffer, &st) ? errno : 0;
if (!error && !force) {
fprintf(stderr, "ln: file '%s' already exists.\n", buffer);
ec = 1;
continue;
}
if (st.st_ino == st_target.st_ino) {
fprintf(stderr, "ln: target_dir '%s' names the same file as '%s'\n", target, path);
ec = 1;
continue;
}
if (!error && unlink(buffer) != 0) {
errprintf("failed to unlink '%s'", buffer);
ec = 1;
continue;
}
if (symbolic) {
if (symlink(path, buffer) != 0) {
errprintf("failed to create symbolic link '%s'", buffer);
ec = 1;
}
continue;
}
struct stat st_src;
if (lstat(path, &st_src) != 0 && (st_src.st_mode & S_IFMT) == S_IFLNK) {
if (linkat(AT_FDCWD, path, AT_FDCWD, buffer, opt_LP == 'P' ? 0 : AT_SYMLINK_FOLLOW) != 0) {
errprintf("failed to create hard-link '%s'", buffer);
ec = 1;
}
continue;
}
if (link(path, buffer) != 0) {
errprintf("failed to create hard-link '%s'", buffer);
ec = 1;
continue;
}
}
free(buffer);
return ec;
}