Contents of /trunk/mkinitrd-magellan/klibc/usr/utils/ln.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 1282 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 1282 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <string.h> |
4 | #include <sys/types.h> |
5 | #include <sys/stat.h> |
6 | #include <unistd.h> |
7 | |
8 | #include <linux/limits.h> |
9 | |
10 | int main(int argc, char *argv[]) |
11 | { |
12 | int c, s, f; |
13 | char *p; |
14 | struct stat sb; |
15 | |
16 | s = f = 0; |
17 | do { |
18 | c = getopt(argc, argv, "sf"); |
19 | if (c == EOF) |
20 | break; |
21 | |
22 | switch (c) { |
23 | |
24 | case 's': |
25 | s = 1; |
26 | break; |
27 | case 'f': |
28 | f = 1; |
29 | break; |
30 | case '?': |
31 | fprintf(stderr, "%s: invalid option -%c\n", |
32 | argv[0], optopt); |
33 | return 1; |
34 | } |
35 | |
36 | } while (1); |
37 | |
38 | if (optind == argc) { |
39 | fprintf(stderr, "Usage: %s [-s] [-f] target link\n", argv[0]); |
40 | return 1; |
41 | } |
42 | |
43 | memset(&sb, 0, sizeof(struct stat)); |
44 | if (stat(argv[argc - 1], &sb) < 0 && argc - optind > 2) { |
45 | if (!(S_ISDIR(sb.st_mode))) { |
46 | fprintf(stderr, |
47 | "multiple targets and %s is not a directory\n", |
48 | argv[argc - 1]); |
49 | return 1; |
50 | } |
51 | } |
52 | |
53 | for (c = optind; c < argc - 1; c++) { |
54 | char target[PATH_MAX]; |
55 | |
56 | p = strrchr(argv[c], '/'); |
57 | p++; |
58 | |
59 | if (S_ISDIR(sb.st_mode)) |
60 | snprintf(target, PATH_MAX, "%s/%s", argv[argc - 1], p); |
61 | else |
62 | snprintf(target, PATH_MAX, "%s", argv[argc - 1]); |
63 | |
64 | if (f) |
65 | unlink(target); |
66 | |
67 | if (s) { |
68 | if (symlink(argv[c], target) == -1) |
69 | perror(target); |
70 | } else { |
71 | if (link(argv[c], target) == -1) |
72 | perror(target); |
73 | } |
74 | } |
75 | |
76 | return 0; |
77 | } |