]> git.ipfire.org Git - thirdparty/bash.git/blob - examples/loadables/realpath.c
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / examples / loadables / realpath.c
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
21 /*
22 Copyright (C) 1999-2009 Free Software Foundation, Inc.
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
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"
55 #include "common.h"
56
57 #ifndef errno
58 extern int errno;
59 #endif
60
61 extern char *sh_realpath();
62
63 int
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 CASE_HELPOPT;
90 default:
91 builtin_usage();
92 return (EX_USAGE);
93 }
94 }
95
96 list = loptend;
97
98 if (list == 0) {
99 builtin_usage();
100 return (EX_USAGE);
101 }
102
103 for (es = EXECUTION_SUCCESS; list; list = list->next) {
104 p = list->word->word;
105 r = sh_realpath(p, realbuf);
106 if (r == 0) {
107 es = EXECUTION_FAILURE;
108 if (sflag == 0)
109 builtin_error("%s: cannot resolve: %s", p, strerror(errno));
110 continue;
111 }
112 if (cflag && (stat(realbuf, &sb) < 0)) {
113 es = EXECUTION_FAILURE;
114 if (sflag == 0)
115 builtin_error("%s: %s", p, strerror(errno));
116 continue;
117 }
118 if (sflag == 0) {
119 if (vflag)
120 printf ("%s -> ", p);
121 printf("%s\n", realbuf);
122 }
123 }
124 return es;
125 }
126
127 char *realpath_doc[] = {
128 "Display pathname in canonical form.",
129 "",
130 "Display the canonicalized version of each PATHNAME argument, resolving",
131 "symbolic links. The -c option checks whether or not each resolved name",
132 "exists. The -s option produces no output; the exit status determines the",
133 "validity of each PATHNAME. The -v option produces verbose output. The",
134 "exit status is 0 if each PATHNAME was resolved; non-zero otherwise.",
135 (char *)NULL
136 };
137
138 struct builtin realpath_struct = {
139 "realpath", /* builtin name */
140 realpath_builtin, /* function implementing the builtin */
141 BUILTIN_ENABLED, /* initial flags for builtin */
142 realpath_doc, /* array of long documentation strings */
143 "realpath [-csv] pathname [pathname...]", /* usage synopsis */
144 0 /* reserved for internal use */
145 };