improve of cmpl.
[bush.git] / examples / loadables / realpath.c
blobd98e111cebbe6959422d4ab85a29d0565796fd11
1 /*
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 * Bush loadable builtin version
17 * Chet Ramey
18 * chet@po.cwru.edu
22 Copyright (C) 1999-2009 Free Software Foundation, Inc.
24 This file is part of GNU Bush.
25 Bush 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.
30 Bush 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.
35 You should have received a copy of the GNU General Public License
36 along with Bush. If not, see <http://www.gnu.org/licenses/>.
39 #include "config.h"
41 #include <sys/types.h>
42 #include <sys/stat.h>
44 #include <stdio.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #include "bushansi.h"
49 #include <maxpath.h>
50 #include <errno.h>
52 #include "builtins.h"
53 #include "shell.h"
54 #include "bushgetopt.h"
55 #include "common.h"
57 #ifndef errno
58 extern int errno;
59 #endif
61 extern char *sh_realpath();
63 int
64 realpath_builtin(list)
65 WORD_LIST *list;
67 int opt, cflag, vflag, sflag, es;
68 char *r, realbuf[PATH_MAX], *p;
69 struct stat sb;
71 if (list == 0) {
72 builtin_usage();
73 return (EX_USAGE);
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);
96 list = loptend;
98 if (list == 0) {
99 builtin_usage();
100 return (EX_USAGE);
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;
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;
118 if (sflag == 0) {
119 if (vflag)
120 printf ("%s -> ", p);
121 printf("%s\n", realbuf);
124 return es;
127 char *realpath_doc[] = {
128 "Display pathname in canonical form.",
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
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 */