]>
git.ipfire.org Git - thirdparty/bash.git/blob - examples/loadables/realpath.c
2 * realpath -- canonicalize pathnames, resolving symlinks
4 * usage: realpath [-csv] pathname [pathname...]
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
11 * exit status: 0 if all pathnames resolved
12 * 1 if any of the pathname arguments could not be resolved
15 * Bash loadable builtin version
23 #include <sys/types.h>
36 #include "bashgetopt.h"
42 extern char *sh_realpath();
44 realpath_builtin(list
)
47 int opt
, cflag
, vflag
, sflag
, es
;
48 char *r
, realbuf
[PATH_MAX
], *p
;
56 vflag
= cflag
= sflag
= 0;
57 reset_internal_getopt();
58 while ((opt
= internal_getopt (list
, "csv")) != -1) {
79 for (es
= EXECUTION_SUCCESS
; list
; list
= list
->next
) {
81 r
= sh_realpath(p
, realbuf
);
83 es
= EXECUTION_FAILURE
;
85 builtin_error("%s: cannot resolve: %s", p
, strerror(errno
));
88 if (cflag
&& (stat(realbuf
, &sb
) < 0)) {
89 es
= EXECUTION_FAILURE
;
91 builtin_error("%s: %s", p
, strerror(errno
));
97 printf("%s\n", realbuf
);
103 char *realpath_doc
[] = {
104 "Display the canonicalized version of each PATHNAME argument, resolving",
105 "symbolic links. The -c option checks whether or not each resolved name",
106 "exists. The -s option produces no output; the exit status determines the",
107 "valididty of each PATHNAME. The -v option produces verbose output. The",
108 "exit status is 0 if each PATHNAME was resolved; non-zero otherwise.",
112 struct builtin realpath_struct
= {
113 "realpath", /* builtin name */
114 realpath_builtin
, /* function implementing the builtin */
115 BUILTIN_ENABLED
, /* initial flags for builtin */
116 realpath_doc
, /* array of long documentation strings */
117 "realpath [-csv] pathname [pathname...]", /* usage synopsis */
118 0 /* reserved for internal use */