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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
|
diff --git a/ln.c b/ln.c
index 3055c75..63e1d27 100644
--- a/ln.c
+++ b/ln.c
@@ -3,6 +3,8 @@
*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
+ * Copyright (c) 2026
+ * Project Tick. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -29,350 +31,600 @@
* SUCH DAMAGE.
*/
-#include <sys/param.h>
-#include <sys/stat.h>
+#define _POSIX_C_SOURCE 200809L
-#include <err.h>
#include <errno.h>
#include <fcntl.h>
-#include <libgen.h>
-#include <limits.h>
+#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
#include <unistd.h>
-static bool fflag; /* Unlink existing files. */
-static bool Fflag; /* Remove empty directories also. */
-static bool hflag; /* Check new name for symlink first. */
-static bool iflag; /* Interactive mode. */
-static bool Pflag; /* Create hard links to symlinks. */
-static bool sflag; /* Symbolic, not hard, link. */
-static bool vflag; /* Verbose output. */
-static bool wflag; /* Warn if symlink target does not
- * exist, and -f is not enabled. */
-static char linkch;
-
-static int linkit(const char *, const char *, bool);
-static void link_usage(void) __dead2;
-static void usage(void) __dead2;
+struct ln_options {
+ bool force;
+ bool remove_dir;
+ bool no_target_follow;
+ bool interactive;
+ bool follow_source_symlink;
+ bool symbolic;
+ bool verbose;
+ bool warn_missing;
+ char linkch;
+};
+
+static const char *progname;
+
+static void error_errno(const char *fmt, ...);
+static void error_msg(const char *fmt, ...);
+static char *join_path(const char *dir, const char *name);
+static int link_usage(void);
+static int ln_usage(void);
+static int linkit(const struct ln_options *options, const char *source,
+ const char *target, bool target_is_dir);
+static const char *path_basename_start(const char *path);
+static size_t path_basename_len(const char *path);
+static char *path_basename_dup(const char *path);
+static char *path_dirname_dup(const char *path);
+static const char *program_name(const char *argv0);
+static int prompt_replace(const char *target);
+static int remove_existing_target(const struct ln_options *options,
+ const char *target, const struct stat *target_sb);
+static int samedirent(const char *path1, const char *path2);
+static bool should_append_basename(const struct ln_options *options,
+ const char *target, bool target_is_dir);
+static int stat_parent_dir(const char *path, struct stat *sb);
+static void warn_missing_symlink_source(const char *source,
+ const char *target);
+
+static const char *
+program_name(const char *argv0)
+{
+ const char *name;
+
+ if (argv0 == NULL || argv0[0] == '\0')
+ return ("ln");
+ name = strrchr(argv0, '/');
+ return (name == NULL ? argv0 : name + 1);
+}
+
+static void
+verror_message(bool with_errno, const char *fmt, va_list ap)
+{
+ int saved_errno;
+
+ saved_errno = errno;
+ (void)fprintf(stderr, "%s: ", progname);
+ (void)vfprintf(stderr, fmt, ap);
+ if (with_errno)
+ (void)fprintf(stderr, ": %s", strerror(saved_errno));
+ (void)fputc('\n', stderr);
+}
+
+static void
+error_errno(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ verror_message(true, fmt, ap);
+ va_end(ap);
+}
+
+static void
+error_msg(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ verror_message(false, fmt, ap);
+ va_end(ap);
+}
+
+static void *
+xmalloc(size_t size)
+{
+ void *ptr;
+
+ ptr = malloc(size);
+ if (ptr == NULL) {
+ error_msg("out of memory");
+ exit(1);
+ }
+ return (ptr);
+}
+
+static char *
+xstrdup(const char *text)
+{
+ size_t len;
+ char *copy;
+
+ len = strlen(text) + 1;
+ copy = xmalloc(len);
+ memcpy(copy, text, len);
+ return (copy);
+}
+
+static char *
+path_basename_dup(const char *path)
+{
+ const char *start;
+ size_t len;
+ char *name;
+
+ start = path_basename_start(path);
+ len = path_basename_len(path);
+ name = xmalloc(len + 1);
+ memcpy(name, start, len);
+ name[len] = '\0';
+ return (name);
+}
+
+static const char *
+path_basename_start(const char *path)
+{
+ const char *end;
+ const char *start;
+
+ if (path[0] == '\0')
+ return (path);
+
+ end = path + strlen(path);
+ while (end > path && end[-1] == '/')
+ end--;
+ if (end == path)
+ return (path);
+
+ start = end;
+ while (start > path && start[-1] != '/')
+ start--;
+ return (start);
+}
+
+static size_t
+path_basename_len(const char *path)
+{
+ const char *end;
+ const char *start;
+
+ if (path[0] == '\0')
+ return (1);
+
+ end = path + strlen(path);
+ while (end > path && end[-1] == '/')
+ end--;
+ if (end == path)
+ return (1);
+
+ start = path_basename_start(path);
+ return ((size_t)(end - start));
+}
+
+static char *
+path_dirname_dup(const char *path)
+{
+ const char *end;
+ const char *slash;
+ size_t len;
+ char *dir;
+
+ if (path[0] == '\0')
+ return (xstrdup("."));
+
+ end = path + strlen(path);
+ while (end > path && end[-1] == '/')
+ end--;
+ if (end == path)
+ return (xstrdup("/"));
+
+ slash = end;
+ while (slash > path && slash[-1] != '/')
+ slash--;
+ if (slash == path)
+ return (xstrdup("."));
+
+ while (slash > path && slash[-1] == '/')
+ slash--;
+ if (slash == path)
+ return (xstrdup("/"));
+
+ len = (size_t)(slash - path);
+ dir = xmalloc(len + 1);
+ memcpy(dir, path, len);
+ dir[len] = '\0';
+ return (dir);
+}
+
+static char *
+join_path(const char *dir, const char *name)
+{
+ size_t dir_len;
+ size_t name_len;
+ bool need_sep;
+ char *path;
+
+ dir_len = strlen(dir);
+ name_len = strlen(name);
+ need_sep = dir_len != 0 && dir[dir_len - 1] != '/';
+
+ path = xmalloc(dir_len + (need_sep ? 1U : 0U) + name_len + 1U);
+ memcpy(path, dir, dir_len);
+ if (need_sep)
+ path[dir_len++] = '/';
+ memcpy(path + dir_len, name, name_len);
+ path[dir_len + name_len] = '\0';
+ return (path);
+}
+
+static int
+samedirent(const char *path1, const char *path2)
+{
+ const char *base1;
+ const char *base2;
+ struct stat sb1;
+ struct stat sb2;
+ size_t base1_len;
+ size_t base2_len;
+
+ if (strcmp(path1, path2) == 0)
+ return (1);
+
+ base1 = path_basename_start(path1);
+ base2 = path_basename_start(path2);
+ base1_len = path_basename_len(path1);
+ base2_len = path_basename_len(path2);
+ if (base1_len != base2_len || memcmp(base1, base2, base1_len) != 0)
+ return (0);
+
+ if (stat_parent_dir(path1, &sb1) != 0 || stat_parent_dir(path2, &sb2) != 0)
+ return (0);
+
+ return (sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino);
+}
+
+static bool
+should_append_basename(const struct ln_options *options, const char *target,
+ bool target_is_dir)
+{
+ struct stat sb;
+ const char *base;
+
+ base = strrchr(target, '/');
+ base = base == NULL ? target : base + 1;
+ if (base[0] == '\0' || (base[0] == '.' && base[1] == '\0'))
+ return (true);
+
+ if (options->remove_dir)
+ return (false);
+ if (target_is_dir)
+ return (true);
+ if (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode))
+ return (true);
+ if (options->no_target_follow)
+ return (false);
+ if (stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))
+ return (true);
+ return (false);
+}
+
+static int
+stat_parent_dir(const char *path, struct stat *sb)
+{
+ char *dir;
+ size_t dir_len;
+ const char *base;
+
+ base = path_basename_start(path);
+ if (base == path)
+ return (stat(".", sb));
+
+ dir_len = (size_t)(base - path);
+ while (dir_len > 1 && path[dir_len - 1] == '/')
+ dir_len--;
+ if (dir_len == 1 && path[0] == '/')
+ return (stat("/", sb));
+
+ dir = xmalloc(dir_len + 1);
+ memcpy(dir, path, dir_len);
+ dir[dir_len] = '\0';
+ if (stat(dir, sb) != 0) {
+ free(dir);
+ return (-1);
+ }
+ free(dir);
+ return (0);
+}
+
+static int
+remove_existing_target(const struct ln_options *options, const char *target,
+ const struct stat *target_sb)
+{
+ if (options->remove_dir && S_ISDIR(target_sb->st_mode)) {
+ if (rmdir(target) != 0) {
+ error_errno("%s", target);
+ return (1);
+ }
+ return (0);
+ }
+
+ if (unlink(target) != 0) {
+ error_errno("%s", target);
+ return (1);
+ }
+ return (0);
+}
+
+static void
+warn_missing_symlink_source(const char *source, const char *target)
+{
+ char *dir;
+ char *resolved;
+ struct stat st;
+
+ if (source[0] == '/') {
+ if (stat(source, &st) != 0)
+ error_errno("warning: %s", source);
+ return;
+ }
+
+ dir = path_dirname_dup(target);
+ resolved = join_path(dir, source);
+ if (stat(resolved, &st) != 0)
+ error_errno("warning: %s", source);
+ free(dir);
+ free(resolved);
+}
+
+static int
+prompt_replace(const char *target)
+{
+ char answer[16];
+ bool stdin_is_tty;
+ int ch;
+
+ stdin_is_tty = isatty(STDIN_FILENO) == 1;
+ (void)stdin_is_tty;
+
+ (void)fflush(stdout);
+ (void)fprintf(stderr, "replace %s? ", target);
+ if (fgets(answer, sizeof(answer), stdin) == NULL) {
+ if (ferror(stdin)) {
+ error_errno("stdin");
+ return (-1);
+ }
+ if (!stdin_is_tty && feof(stdin)) {
+ (void)fprintf(stderr, "not replaced\n");
+ return (1);
+ }
+ (void)fprintf(stderr, "not replaced\n");
+ return (1);
+ }
+
+ if (strchr(answer, '\n') == NULL) {
+ while ((ch = getchar()) != '\n' && ch != EOF)
+ continue;
+ if (ferror(stdin)) {
+ error_errno("stdin");
+ return (-1);
+ }
+ }
+
+ if (answer[0] != 'y' && answer[0] != 'Y') {
+ (void)fprintf(stderr, "not replaced\n");
+ return (1);
+ }
+ return (0);
+}
+
+static int
+linkit(const struct ln_options *options, const char *source, const char *target,
+ bool target_is_dir)
+{
+ struct stat source_sb;
+ struct stat target_sb;
+ char *resolved_target;
+ bool exists;
+ int flags;
+ int ret;
+
+ resolved_target = NULL;
+ ret = 1;
+ if (!options->symbolic) {
+ if ((options->follow_source_symlink ? stat : lstat)(source,
+ &source_sb) != 0) {
+ error_errno("%s", source);
+ goto cleanup;
+ }
+ if (S_ISDIR(source_sb.st_mode)) {
+ errno = EISDIR;
+ error_errno("%s", source);
+ goto cleanup;
+ }
+ }
+
+ if (should_append_basename(options, target, target_is_dir)) {
+ char *base;
+
+ base = path_basename_dup(source);
+ resolved_target = join_path(target, base);
+ free(base);
+ } else {
+ resolved_target = xstrdup(target);
+ }
+
+ if (options->symbolic && options->warn_missing)
+ warn_missing_symlink_source(source, resolved_target);
+
+ exists = lstat(resolved_target, &target_sb) == 0;
+ if (exists && !options->symbolic && samedirent(source, resolved_target)) {
+ error_msg("%s and %s are the same directory entry", source,
+ resolved_target);
+ goto cleanup;
+ }
+
+ if (exists && options->force) {
+ if (remove_existing_target(options, resolved_target, &target_sb) != 0)
+ goto cleanup;
+ } else if (exists && options->interactive) {
+ ret = prompt_replace(resolved_target);
+ if (ret != 0)
+ goto cleanup;
+ if (remove_existing_target(options, resolved_target, &target_sb) != 0)
+ goto cleanup;
+ }
+
+ flags = options->follow_source_symlink ? AT_SYMLINK_FOLLOW : 0;
+ if (options->symbolic) {
+ if (symlink(source, resolved_target) != 0) {
+ error_errno("%s", resolved_target);
+ goto cleanup;
+ }
+ } else if (linkat(AT_FDCWD, source, AT_FDCWD, resolved_target, flags) != 0) {
+ error_errno("%s", resolved_target);
+ goto cleanup;
+ }
+
+ if (options->verbose)
+ (void)printf("%s %c> %s\n", resolved_target, options->linkch, source);
+
+ ret = 0;
+cleanup:
+ free(resolved_target);
+ return (ret);
+}
+
+static int
+link_usage(void)
+{
+ (void)fprintf(stderr, "usage: link source_file target_file\n");
+ return (2);
+}
+
+static int
+ln_usage(void)
+{
+ (void)fprintf(stderr,
+ "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnvw] "
+ "source_file [target_file]\n");
+ (void)fprintf(stderr,
+ " ln [-s [-F] | -L | -P] [-f | -i] [-hnvw] "
+ "source_file ... target_dir\n");
+ return (2);
+}
int
main(int argc, char *argv[])
{
+ struct ln_options options;
struct stat sb;
char *targetdir;
- int ch, exitval;
-
- /*
- * Test for the special case where the utility is called as
- * "link", for which the functionality provided is greatly
- * simplified.
- */
- if (strcmp(getprogname(), "link") == 0) {
- while (getopt(argc, argv, "") != -1)
- link_usage();
+ int ch;
+ int exitval;
+
+ progname = program_name(argv[0]);
+ memset(&options, 0, sizeof(options));
+ /* FreeBSD hard-link semantics default to -L, unlike GNU ln's -P. */
+ options.follow_source_symlink = true;
+
+ if (strcmp(progname, "link") == 0) {
+ opterr = 0;
+ while ((ch = getopt(argc, argv, "")) != -1)
+ return (link_usage());
argc -= optind;
argv += optind;
if (argc != 2)
- link_usage();
- if (lstat(argv[1], &sb) == 0)
- errc(1, EEXIST, "%s", argv[1]);
- /*
- * We could simply call link(2) here, but linkit()
- * performs additional checks and gives better
- * diagnostics.
- */
- exit(linkit(argv[0], argv[1], false));
+ return (link_usage());
+ if (lstat(argv[1], &sb) == 0) {
+ errno = EEXIST;
+ error_errno("%s", argv[1]);
+ return (1);
+ }
+ return (linkit(&options, argv[0], argv[1], false));
}
- while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
+ opterr = 0;
+ while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1) {
switch (ch) {
case 'F':
- Fflag = true;
+ options.remove_dir = true;
break;
case 'L':
- Pflag = false;
+ options.follow_source_symlink = true;
break;
case 'P':
- Pflag = true;
+ options.follow_source_symlink = false;
break;
case 'f':
- fflag = true;
- iflag = false;
- wflag = false;
+ options.force = true;
+ options.interactive = false;
+ options.warn_missing = false;
break;
case 'h':
case 'n':
- hflag = true;
+ options.no_target_follow = true;
break;
case 'i':
- iflag = true;
- fflag = false;
+ options.interactive = true;
+ options.force = false;
break;
case 's':
- sflag = true;
+ options.symbolic = true;
break;
case 'v':
- vflag = true;
+ options.verbose = true;
break;
case 'w':
- wflag = true;
+ options.warn_missing = true;
break;
case '?':
default:
- usage();
+ if (optopt != 0)
+ error_msg("unknown option -- %c", optopt);
+ return (ln_usage());
}
+ }
argv += optind;
argc -= optind;
- linkch = sflag ? '-' : '=';
- if (!sflag)
- Fflag = false;
- if (Fflag && !iflag) {
- fflag = true;
- wflag = false; /* Implied when fflag is true */
+ options.linkch = options.symbolic ? '-' : '=';
+ if (!options.symbolic)
+ options.remove_dir = false;
+ if (options.remove_dir && !options.interactive) {
+ options.force = true;
+ options.warn_missing = false;
}
switch (argc) {
case 0:
- usage();
- /* NOTREACHED */
- case 1: /* ln source */
- exit(linkit(argv[0], ".", true));
- case 2: /* ln source target */
- exit(linkit(argv[0], argv[1], false));
+ return (ln_usage());
+ case 1:
+ return (linkit(&options, argv[0], ".", true));
+ case 2:
+ return (linkit(&options, argv[0], argv[1], false));
default:
- ;
+ break;
}
- /* ln source1 source2 directory */
+
targetdir = argv[argc - 1];
- if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
- /*
- * We were asked not to follow symlinks, but found one at
- * the target--simulate "not a directory" error
- */
+ if (options.no_target_follow && lstat(targetdir, &sb) == 0 &&
+ S_ISLNK(sb.st_mode)) {
errno = ENOTDIR;
- err(1, "%s", targetdir);
- }
- if (stat(targetdir, &sb))
- err(1, "%s", targetdir);
- if (!S_ISDIR(sb.st_mode))
- usage();
- for (exitval = 0; *argv != targetdir; ++argv)
- exitval |= linkit(*argv, targetdir, true);
- exit(exitval);
-}
-
-/*
- * Two pathnames refer to the same directory entry if the directories match
- * and the final components' names match.
- */
-static int
-samedirent(const char *path1, const char *path2)
-{
- const char *file1, *file2;
- char pathbuf[PATH_MAX];
- struct stat sb1, sb2;
-
- if (strcmp(path1, path2) == 0)
- return 1;
- file1 = strrchr(path1, '/');
- if (file1 != NULL)
- file1++;
- else
- file1 = path1;
- file2 = strrchr(path2, '/');
- if (file2 != NULL)
- file2++;
- else
- file2 = path2;
- if (strcmp(file1, file2) != 0)
- return 0;
- if (file1 - path1 >= PATH_MAX || file2 - path2 >= PATH_MAX)
- return 0;
- if (file1 == path1)
- memcpy(pathbuf, ".", 2);
- else {
- memcpy(pathbuf, path1, file1 - path1);
- pathbuf[file1 - path1] = '\0';
- }
- if (stat(pathbuf, &sb1) != 0)
- return 0;
- if (file2 == path2)
- memcpy(pathbuf, ".", 2);
- else {
- memcpy(pathbuf, path2, file2 - path2);
- pathbuf[file2 - path2] = '\0';
- }
- if (stat(pathbuf, &sb2) != 0)
- return 0;
- return sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino;
-}
-
-/*
- * Create a link to source. If target is a directory (and some additional
- * conditions apply, see comments within) the link will be created within
- * target and have the basename of source. Otherwise, the link will be
- * named target. If isdir is true, target has already been determined to
- * be a directory; otherwise, we will check, if needed.
- */
-static int
-linkit(const char *source, const char *target, bool isdir)
-{
- char path[PATH_MAX];
- char wbuf[PATH_MAX];
- char bbuf[PATH_MAX];
- struct stat sb;
- const char *p;
- int ch, first;
- bool append, exists;
-
- if (!sflag) {
- /* If source doesn't exist, quit now. */
- if ((Pflag ? lstat : stat)(source, &sb)) {
- warn("%s", source);
- return (1);
- }
- /* Only symbolic links to directories. */
- if (S_ISDIR(sb.st_mode)) {
- errno = EISDIR;
- warn("%s", source);
- return (1);
- }
- }
-
- /*
- * Append a slash and the source's basename if:
- * - the target is "." or ends in "/" or "/.", or
- * - the target is a directory (and not a symlink if hflag) and
- * Fflag is not set
- */
- if ((p = strrchr(target, '/')) == NULL)
- p = target;
- else
- p++;
- append = false;
- if (p[0] == '\0' || (p[0] == '.' && p[1] == '\0')) {
- append = true;
- } else if (!Fflag) {
- if (isdir || (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
- (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))) {
- append = true;
- }
- }
- if (append) {
- if (strlcpy(bbuf, source, sizeof(bbuf)) >= sizeof(bbuf) ||
- (p = basename(bbuf)) == NULL /* can't happen */ ||
- snprintf(path, sizeof(path), "%s/%s", target, p) >=
- (ssize_t)sizeof(path)) {
- errno = ENAMETOOLONG;
- warn("%s", source);
- return (1);
- }
- target = path;
- }
-
- /*
- * If the link source doesn't exist, and a symbolic link was
- * requested, and -w was specified, give a warning.
- */
- if (sflag && wflag) {
- if (*source == '/') {
- /* Absolute link source. */
- if (stat(source, &sb) != 0)
- warn("warning: %s inaccessible", source);
- } else {
- /*
- * Relative symlink source. Try to construct the
- * absolute path of the source, by appending `source'
- * to the parent directory of the target.
- */
- strlcpy(bbuf, target, sizeof(bbuf));
- p = dirname(bbuf);
- if (p != NULL) {
- (void)snprintf(wbuf, sizeof(wbuf), "%s/%s",
- p, source);
- if (stat(wbuf, &sb) != 0)
- warn("warning: %s", source);
- }
- }
- }
-
- /*
- * If the file exists, first check it is not the same directory entry.
- */
- exists = lstat(target, &sb) == 0;
- if (exists) {
- if (!sflag && samedirent(source, target)) {
- warnx("%s and %s are the same directory entry",
- source, target);
- return (1);
- }
- }
- /*
- * Then unlink it forcibly if -f was specified
- * and interactively if -i was specified.
- */
- if (fflag && exists) {
- if (Fflag && S_ISDIR(sb.st_mode)) {
- if (rmdir(target)) {
- warn("%s", target);
- return (1);
- }
- } else if (unlink(target)) {
- warn("%s", target);
- return (1);
- }
- } else if (iflag && exists) {
- fflush(stdout);
- fprintf(stderr, "replace %s? ", target);
-
- first = ch = getchar();
- while(ch != '\n' && ch != EOF)
- ch = getchar();
- if (first != 'y' && first != 'Y') {
- fprintf(stderr, "not replaced\n");
- return (1);
- }
-
- if (Fflag && S_ISDIR(sb.st_mode)) {
- if (rmdir(target)) {
- warn("%s", target);
- return (1);
- }
- } else if (unlink(target)) {
- warn("%s", target);
- return (1);
- }
+ error_errno("%s", targetdir);
+ return (1);
}
-
- /* Attempt the link. */
- if (sflag ? symlink(source, target) :
- linkat(AT_FDCWD, source, AT_FDCWD, target,
- Pflag ? 0 : AT_SYMLINK_FOLLOW)) {
- warn("%s", target);
+ if (stat(targetdir, &sb) != 0) {
+ error_errno("%s", targetdir);
return (1);
}
- if (vflag)
- (void)printf("%s %c> %s\n", target, linkch, source);
- return (0);
-}
-
-static void
-link_usage(void)
-{
- (void)fprintf(stderr, "usage: link source_file target_file\n");
- exit(1);
-}
+ if (!S_ISDIR(sb.st_mode))
+ return (ln_usage());
-static void
-usage(void)
-{
- (void)fprintf(stderr, "%s\n%s\n",
- "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]",
- " ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir");
- exit(1);
+ exitval = 0;
+ for (int i = 0; i < argc - 1; i++)
+ exitval |= linkit(&options, argv[i], targetdir, true);
+ return (exitval);
}
|