]>
Commit | Line | Data |
---|---|---|
28ef6c31 JA |
1 | /* |
2 | * realpath -- canonicalize pathnames, resolving symlinks | |
3 | * | |
4 | * usage: realpath [-csv] pathname [pathname...] | |
5 | * | |
6 | * options: -c check whether or not each resolved path exists | |
7 | * -s no output, exit status determines whether path is valid | |
8 | * -v produce verbose output | |
9 | * | |
10 | * | |
11 | * exit status: 0 if all pathnames resolved | |
12 | * 1 if any of the pathname arguments could not be resolved | |
13 | * | |
14 | * | |
15 | * Bash loadable builtin version | |
16 | * | |
17 | * Chet Ramey | |
18 | * chet@po.cwru.edu | |
19 | */ | |
20 | ||
f1c4df24 | 21 | /* |
012bac39 | 22 | Copyright (C) 1999-2009 Free Software Foundation, Inc. |
f1c4df24 CR |
23 | |
24 | This file is part of GNU Bash. | |
25 | Bash is free software: you can redistribute it and/or modify | |
26 | it under the terms of the GNU General Public License as published by | |
27 | the Free Software Foundation, either version 3 of the License, or | |
28 | (at your option) any later version. | |
29 | ||
30 | Bash is distributed in the hope that it will be useful, | |
31 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
32 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
33 | GNU General Public License for more details. | |
34 | ||
35 | You should have received a copy of the GNU General Public License | |
36 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
37 | */ | |
38 | ||
28ef6c31 JA |
39 | #include "config.h" |
40 | ||
41 | #include <sys/types.h> | |
42 | #include <sys/stat.h> | |
43 | ||
44 | #include <stdio.h> | |
45 | #ifdef HAVE_UNISTD_H | |
46 | # include <unistd.h> | |
47 | #endif | |
48 | #include "bashansi.h" | |
49 | #include <maxpath.h> | |
50 | #include <errno.h> | |
51 | ||
52 | #include "builtins.h" | |
53 | #include "shell.h" | |
54 | #include "bashgetopt.h" | |
29f3080f | 55 | #include "common.h" |
28ef6c31 JA |
56 | |
57 | #ifndef errno | |
58 | extern int errno; | |
59 | #endif | |
60 | ||
61 | extern char *sh_realpath(); | |
62 | ||
54a5fbe1 | 63 | int |
28ef6c31 JA |
64 | realpath_builtin(list) |
65 | WORD_LIST *list; | |
66 | { | |
67 | int opt, cflag, vflag, sflag, es; | |
68 | char *r, realbuf[PATH_MAX], *p; | |
69 | struct stat sb; | |
70 | ||
71 | if (list == 0) { | |
72 | builtin_usage(); | |
73 | return (EX_USAGE); | |
74 | } | |
75 | ||
76 | vflag = cflag = sflag = 0; | |
77 | reset_internal_getopt(); | |
78 | while ((opt = internal_getopt (list, "csv")) != -1) { | |
79 | switch (opt) { | |
80 | case 'c': | |
81 | cflag = 1; | |
82 | break; | |
83 | case 's': | |
84 | sflag = 1; | |
85 | break; | |
86 | case 'v': | |
87 | vflag = 1; | |
88 | break; | |
89 | default: | |
29f3080f | 90 | builtin_usage(); |
28ef6c31 JA |
91 | } |
92 | } | |
93 | ||
94 | list = loptend; | |
95 | ||
96 | if (list == 0) | |
29f3080f | 97 | builtin_usage(); |
28ef6c31 JA |
98 | |
99 | for (es = EXECUTION_SUCCESS; list; list = list->next) { | |
100 | p = list->word->word; | |
101 | r = sh_realpath(p, realbuf); | |
102 | if (r == 0) { | |
103 | es = EXECUTION_FAILURE; | |
104 | if (sflag == 0) | |
105 | builtin_error("%s: cannot resolve: %s", p, strerror(errno)); | |
106 | continue; | |
107 | } | |
108 | if (cflag && (stat(realbuf, &sb) < 0)) { | |
109 | es = EXECUTION_FAILURE; | |
110 | if (sflag == 0) | |
111 | builtin_error("%s: %s", p, strerror(errno)); | |
112 | continue; | |
113 | } | |
114 | if (sflag == 0) { | |
115 | if (vflag) | |
116 | printf ("%s -> ", p); | |
117 | printf("%s\n", realbuf); | |
118 | } | |
119 | } | |
120 | return es; | |
121 | } | |
122 | ||
123 | char *realpath_doc[] = { | |
6a8fd0ed CR |
124 | "Display pathname in canonical form.", |
125 | "", | |
28ef6c31 JA |
126 | "Display the canonicalized version of each PATHNAME argument, resolving", |
127 | "symbolic links. The -c option checks whether or not each resolved name", | |
128 | "exists. The -s option produces no output; the exit status determines the", | |
129 | "valididty of each PATHNAME. The -v option produces verbose output. The", | |
130 | "exit status is 0 if each PATHNAME was resolved; non-zero otherwise.", | |
131 | (char *)NULL | |
132 | }; | |
133 | ||
134 | struct builtin realpath_struct = { | |
135 | "realpath", /* builtin name */ | |
136 | realpath_builtin, /* function implementing the builtin */ | |
137 | BUILTIN_ENABLED, /* initial flags for builtin */ | |
138 | realpath_doc, /* array of long documentation strings */ | |
139 | "realpath [-csv] pathname [pathname...]", /* usage synopsis */ | |
140 | 0 /* reserved for internal use */ | |
141 | }; |