1: #ifndef lint
2: static char sccsid[] = "@(#)ln.c 4.1 10/1/80";
3: #endif
4: /*
5: * ln
6: */
7: #include <whoami.h>
8: #include <stdio.h>
9: #include <sys/types.h>
10: #include <sys/stat.h>
11: #include <errno.h>
12:
13: struct stat stb;
14: int fflag; /* force flag set? */
15: int sflag;
16: char name[BUFSIZ];
17: char *rindex();
18: extern int errno;
19:
20: main(argc, argv)
21: int argc;
22: register char **argv;
23: {
24: register int i, r;
25:
26: argc--, argv++;
27: if (argc && strcmp(argv[0], "-f") == 0) {
28: fflag++;
29: argv++;
30: argc--;
31: }
32: #ifdef UCB_SYMLINKS
33: if (argc && strcmp(argv[0], "-s") == 0) {
34: sflag++;
35: argv++;
36: argc--;
37: }
38: #endif
39: if (argc == 0)
40: goto usage;
41: else
42: if (argc == 1) {
43: argv[argc] = ".";
44: argc++;
45: }
46: if (sflag == 0 && argc > 2) {
47: if (stat(argv[argc-1], &stb) < 0)
48: goto usage;
49: if ((stb.st_mode&S_IFMT) != S_IFDIR)
50: goto usage;
51: }
52: r = 0;
53: for(i = 0; i < argc-1; i++)
54: r |= linkit(argv[i], argv[argc-1]);
55: exit(r);
56: usage:
57: #ifdef UCB_SYMLINKS
58: fprintf(stderr, "Usage: ln [ -s ] f1\nor: ln [ -s ] f1 f2\nln [ -s ] f1 ... fn d2\n");
59: #else
60: fprintf(stderr, "Usage: ln f1\nor: ln f1 f2\nln f1 ... fn d2\n");
61: #endif
62: exit(1);
63: }
64:
65: int link();
66: #ifdef UCB_SYMLINKS
67: int symlink();
68: #endif
69:
70: linkit(from, to)
71: char *from, *to;
72: {
73: char *tail;
74: #ifdef UCB_SYMLINKS
75: int (*linkf)() = sflag ? symlink : link;
76: #else
77: int (*linkf)() = link;
78: #endif
79:
80: /* is target a directory? */
81: if (sflag == 0 && fflag == 0 && stat(from, &stb) >= 0
82: && (stb.st_mode&S_IFMT) == S_IFDIR) {
83: printf("%s is a directory\n", from);
84: return (1);
85: }
86: if (stat(to, &stb) >=0 && (stb.st_mode&S_IFMT) == S_IFDIR) {
87: tail = rindex(from, '/');
88: if (tail == 0)
89: tail = from;
90: else
91: tail++;
92: sprintf(name, "%s/%s", to, tail);
93: to = name;
94: }
95: if ((*linkf)(from, to) < 0) {
96: if (errno == EEXIST)
97: perror(to);
98: else
99: perror(from);
100: return (1);
101: }
102: return(0);
103: }
Defined functions
main
defined in line
20;
never used
Defined variables
fflag
defined in line
14; used 2 times
name
defined in line
16; used 2 times
sccsid
defined in line
2;
never used
sflag
defined in line
15; used 4 times
stb
defined in line
13; used 6 times