|
|
1.1 root 1: static char sccsid[] = "@(#)ln.c 4.1 10/1/80";
2: /*
3: * ln
4: */
5: #include <stdio.h>
6: #include <sys/types.h>
7: #include <sys/stat.h>
8:
9: struct stat stb;
10: int fflag; /* force flag set? */
11: char name[BUFSIZ];
12: char *rindex();
13:
14: main(argc, argv)
15: int argc;
16: register char **argv;
17: {
18: register int i, r;
19:
20: argc--, argv++;
21: if (argc && strcmp(argv[0], "-f") == 0) {
22: fflag++;
23: argv++;
24: argc--;
25: }
26: if (argc == 0)
27: goto usage;
28: else if (argc == 1) {
29: argv[argc] = ".";
30: argc++;
31: }
32: if (argc > 2) {
33: if (stat(argv[argc-1], &stb) < 0)
34: goto usage;
35: if ((stb.st_mode&S_IFMT) != S_IFDIR)
36: goto usage;
37: }
38: r = 0;
39: for(i = 0; i < argc-1; i++)
40: r |= linkit(argv[i], argv[argc-1]);
41: exit(r);
42: usage:
43: fprintf(stderr, "Usage: ln f1\nor: ln f1 f2\nln f1 ... fn d2\n");
44: exit(1);
45: }
46:
47: linkit(from, to)
48: char *from, *to;
49: {
50: char *tail;
51:
52: /* is target a directory? */
53: if (fflag == 0 && stat(from, &stb) >= 0
54: && (stb.st_mode&S_IFMT) == S_IFDIR) {
55: printf("%s is a directory\n", from);
56: return (1);
57: }
58: if (stat(to, &stb) >=0 && (stb.st_mode&S_IFMT) == S_IFDIR) {
59: tail = rindex(from, '/');
60: if (tail == 0)
61: tail = from;
62: else
63: tail++;
64: sprintf(name, "%s/%s", to, tail);
65: to = name;
66: }
67: if (link(from, to) < 0) {
68: perror(from);
69: return (1);
70: }
71: return(0);
72: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.