po: Update German programs translation
[dpkg.git] / src / realpath / main.c
blobbfa2aa2bc3f3d7c3c37084e4845d27399f970308
1 /*
2 * dpkg-realpath - print the resolved pathname with DPKG_ROOT support
4 * Copyright © 2020 Helmut Grohne <helmut@subdivi.de>
5 * Copyright © 2020-2024 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 #include <config.h>
22 #include <compat.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include <errno.h>
28 #ifdef HAVE_LOCALE_H
29 #include <locale.h>
30 #endif
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <stdio.h>
37 #include <dpkg/i18n.h>
38 #include <dpkg/dpkg.h>
39 #include <dpkg/dpkg-db.h>
40 #include <dpkg/debug.h>
41 #include <dpkg/string.h>
42 #include <dpkg/db-fsys.h>
43 #include <dpkg/options.h>
45 static const char printforhelp[] = N_(
46 "Use --help for help about this utility.");
48 static void DPKG_ATTR_NORET
49 printversion(const struct cmdinfo *cip, const char *value)
51 printf(_("Debian %s version %s.\n"), dpkg_get_progname(),
52 PACKAGE_RELEASE);
54 printf(_(
55 "This is free software; see the GNU General Public License version 2 or\n"
56 "later for copying conditions. There is NO warranty.\n"));
58 m_output(stdout, _("<standard output>"));
60 exit(0);
63 static void DPKG_ATTR_NORET
64 usage(const struct cmdinfo *cip, const char *value)
66 printf(_(
67 "Usage: %s [<option>...] <pathname>\n"
68 "\n"), dpkg_get_progname());
70 printf(_(
71 "Options:\n"
72 " -z, --zero end output line with NUL, not newline.\n"
73 " --instdir <directory> set the root directory.\n"
74 " --root <directory> set the root directory.\n"
75 " --version show the version.\n"
76 " --help show this help message.\n"
77 "\n"));
79 m_output(stdout, _("<standard output>"));
81 exit(0);
84 static int opt_eol = '\n';
86 static char *
87 realpath_relative_to(const char *pathname, const char *rootdir)
89 struct varbuf root = VARBUF_INIT;
90 struct varbuf src = VARBUF_INIT;
91 struct varbuf dst = VARBUF_INIT;
92 struct varbuf slink = VARBUF_INIT;
93 struct varbuf result = VARBUF_INIT;
94 struct varbuf prefix = VARBUF_INIT;
95 int loop = 0;
97 varbuf_init(&root, 32);
98 varbuf_init(&src, 32);
99 varbuf_init(&dst, 32);
100 varbuf_init(&result, 32);
102 varbuf_set_str(&root, rootdir);
103 varbuf_set_str(&src, pathname);
104 varbuf_set_str(&result, rootdir);
106 /* Check whether the path is relative, make it absolute otherwise. */
107 if (src.buf[0] != '/') {
108 struct varbuf abs_src = VARBUF_INIT;
110 file_getcwd(&abs_src);
111 varbuf_add_char(&abs_src, '/');
112 varbuf_add_varbuf(&abs_src, &src);
114 varbuf_set_varbuf(&src, &abs_src);
115 varbuf_destroy(&abs_src);
117 varbuf_trim_varbuf_prefix(&src, &root);
120 /* Remove prefixed slashes. */
121 varbuf_trim_char_prefix(&src, '/');
123 while (src.used) {
124 struct stat st;
125 const char *slash;
127 /* Get the first directory component. */
128 varbuf_set_varbuf(&prefix, &src);
129 slash = strchrnul(prefix.buf, '/');
130 varbuf_trunc(&prefix, slash - prefix.buf);
132 /* Remove the first directory component from src. */
133 varbuf_trim_varbuf_prefix(&src, &prefix);
135 /* Remove prefixed slashes. */
136 varbuf_trim_char_prefix(&src, '/');
138 varbuf_set_varbuf(&slink, &result);
139 varbuf_add_char(&slink, '/');
140 varbuf_add_varbuf(&slink, &prefix);
142 /* Resolve the first directory component. */
143 if (strcmp(prefix.buf, ".") == 0) {
144 /* Ignore, stay at the same directory. */
145 } else if (strcmp(prefix.buf, "..") == 0) {
146 /* Go up one directory. */
147 slash = strrchr(result.buf, '/');
148 if (slash != NULL)
149 varbuf_trunc(&result, slash - result.buf);
151 if (root.used && !varbuf_has_prefix(&result, &root))
152 varbuf_set_varbuf(&result, &root);
153 } else if (lstat(slink.buf, &st) == 0 && S_ISLNK(st.st_mode)) {
154 ssize_t linksize;
156 loop++;
157 if (loop > 25)
158 ohshit(_("too many levels of symbolic links"));
160 /* Resolve the symlink within result. */
161 linksize = file_readlink(slink.buf, &dst, st.st_size);
162 if (linksize < 0)
163 ohshite(_("cannot read link '%s'"), slink.buf);
164 else if ((off_t)linksize != st.st_size)
165 ohshit(_("symbolic link '%s' size has changed from %jd to %zd"),
166 slink.buf, (intmax_t)st.st_size, linksize);
168 if (dst.buf[0] == '/') {
169 /* Absolute pathname, reset result back to
170 * root. */
171 varbuf_set_varbuf(&result, &root);
173 if (src.used) {
174 varbuf_add_char(&dst, '/');
175 varbuf_add_varbuf(&dst, &src);
177 varbuf_set_varbuf(&src, &dst);
179 /* Remove prefixed slashes. */
180 varbuf_trim_char_prefix(&src, '/');
181 } else {
182 /* Relative pathname. */
183 if (src.used) {
184 varbuf_add_char(&dst, '/');
185 varbuf_add_varbuf(&dst, &src);
187 varbuf_set_varbuf(&src, &dst);
189 } else {
190 /* Otherwise append the prefix. */
191 varbuf_add_char(&result, '/');
192 varbuf_add_varbuf(&result, &prefix);
196 /* We are done, return the resolved pathname, w/o root. */
197 varbuf_trim_varbuf_prefix(&result, &root);
199 varbuf_destroy(&root);
200 varbuf_destroy(&src);
201 varbuf_destroy(&dst);
202 varbuf_destroy(&slink);
203 varbuf_destroy(&prefix);
205 return varbuf_detach(&result);
208 static const struct cmdinfo cmdinfos[] = {
209 { "zero", 'z', 0, &opt_eol, NULL, NULL, '\0' },
210 { "instdir", 0, 1, NULL, NULL, set_instdir, 0 },
211 { "root", 0, 1, NULL, NULL, set_instdir, 0 },
212 { "version", 0, 0, NULL, NULL, printversion },
213 { "help", '?', 0, NULL, NULL, usage },
214 { NULL, 0 }
218 main(int argc, const char *const *argv)
220 const char *instdir;
221 const char *pathname;
222 char *real_pathname;
224 dpkg_locales_init(PACKAGE);
225 dpkg_program_init("dpkg-realpath");
226 dpkg_options_parse(&argv, cmdinfos, printforhelp);
228 debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
230 pathname = argv[0];
231 if (str_is_unset(pathname))
232 badusage(_("need a pathname argument"));
234 instdir = dpkg_fsys_get_dir();
235 if (strlen(instdir) && strncmp(pathname, instdir, strlen(instdir)) == 0)
236 badusage(_("link '%s' includes root prefix '%s'"),
237 pathname, instdir);
239 real_pathname = realpath_relative_to(pathname, instdir);
240 printf("%s%c", real_pathname, opt_eol);
241 free(real_pathname);
243 dpkg_program_done();
244 dpkg_locales_done();
246 return 0;