|
|
1.1 ! root 1: static char sccsid[] = "@(#)ln.c 4.3 3/31/82"; ! 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; ! 11: int sflag; ! 12: char name[BUFSIZ]; ! 13: char *strrchr(); ! 14: ! 15: main(argc, argv) ! 16: int argc; ! 17: register char **argv; ! 18: { ! 19: register int i, r; ! 20: ! 21: argc--, argv++; ! 22: again: ! 23: if (argc && strcmp(argv[0], "-f") == 0) { ! 24: fflag++; ! 25: argv++; ! 26: argc--; ! 27: } ! 28: if (argc && strcmp(argv[0], "-s") == 0) { ! 29: sflag++; ! 30: argv++; ! 31: argc--; ! 32: } ! 33: if (argc <= 1) ! 34: goto usage; ! 35: if (sflag == 0 && argc > 2) { ! 36: if (stat(argv[argc-1], &stb) < 0) ! 37: goto usage; ! 38: if ((stb.st_mode&S_IFMT) != S_IFDIR) ! 39: goto usage; ! 40: } ! 41: r = 0; ! 42: for(i = 0; i < argc-1; i++) ! 43: r |= linkit(argv[i], argv[argc-1]); ! 44: exit(r); ! 45: usage: ! 46: fprintf(stderr, "Usage: ln [ -s ] f1 f2\nor: ln [ -s ] f1 ... fn d2\n"); ! 47: exit(1); ! 48: } ! 49: ! 50: int link(), symlink(); ! 51: ! 52: linkit(from, to) ! 53: char *from, *to; ! 54: { ! 55: char *tail; ! 56: int (*linkf)() = sflag ? symlink : link; ! 57: ! 58: /* is target a directory? */ ! 59: if (sflag == 0 && fflag == 0 && stat(from, &stb) >= 0 ! 60: && (stb.st_mode&S_IFMT) == S_IFDIR) { ! 61: fprintf(stderr, "ln: %s is a directory\n", from); ! 62: return (1); ! 63: } ! 64: if (stat(to, &stb) >= 0 && (stb.st_mode&S_IFMT) == S_IFDIR) { ! 65: tail = strrchr(from, '/'); ! 66: if (tail == 0) ! 67: tail = from; ! 68: else ! 69: tail++; ! 70: sprintf(name, "%s/%s", to, tail); ! 71: to = name; ! 72: } ! 73: if ((*linkf)(from, to) < 0) { ! 74: lnerror(from, to); ! 75: return (1); ! 76: } ! 77: return (0); ! 78: } ! 79: ! 80: lnerror(from, to) ! 81: char *from, *to; ! 82: { ! 83: char buf[BUFSIZ]; ! 84: ! 85: sprintf(buf, "ln %s %s", from, to); ! 86: perror(buf); ! 87: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.