po: Update German man pages translation
[dpkg.git] / lib / dpkg / path.c
blobd7b20d63381ca41854f800c2ef65e105abae4612
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * path.c - path handling functions
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
29 #include <dpkg/dpkg.h>
30 #include <dpkg/string.h>
31 #include <dpkg/path.h>
33 /**
34 * Trim ‘/’ and ‘/.’ from the end of a pathname.
36 * The given string will get NUL-terminatd.
38 * @param path The pathname to trim.
40 * @return The size of the trimmed pathname.
42 size_t
43 path_trim_slash_slashdot(char *path)
45 char *end;
47 if (str_is_unset(path))
48 return 0;
50 for (end = path + strlen(path) - 1; end - path >= 1; end--) {
51 if (*end == '/' || (*(end - 1) == '/' && *end == '.'))
52 *end = '\0';
53 else
54 break;
57 return end - path + 1;
60 /**
61 * Skip ‘/’ and ‘./’ from the beginning of a pathname.
63 * @param path The pathname to skip.
65 * @return The new beginning of the pathname.
67 const char *
68 path_skip_slash_dotslash(const char *path)
70 while (path[0] == '/' || (path[0] == '.' && path[1] == '/'))
71 path++;
73 return path;
76 /**
77 * Return the last component of a pathname.
79 * @param path The pathname to get the base name from.
81 * @return A pointer to the last component inside pathname.
83 const char *
84 path_basename(const char *path)
86 const char *last_slash;
88 last_slash = strrchr(path, '/');
89 if (last_slash == NULL)
90 return path;
91 else
92 return last_slash + 1;
95 /**
96 * Create a template for a temporary pathname.
98 * @param suffix The suffix to use for the template string.
100 * @return An allocated string with the created template.
102 char *
103 path_make_temp_template(const char *suffix)
105 const char *tmpdir;
107 tmpdir = getenv("TMPDIR");
108 if (!tmpdir)
109 tmpdir = P_tmpdir;
111 return str_fmt("%s/%s.XXXXXX", tmpdir, suffix);
115 * Escape characters in a pathname for safe locale printing.
117 * We need to quote paths so that they do not cause problems when printing
118 * them, for example with snprintf(3) which does not work if the format
119 * string contains %s and an argument has invalid characters for the
120 * current locale, it will then return -1.
122 * To simplify things, we just escape all 8 bit characters, instead of
123 * just invalid characters.
125 * @param dst The escaped destination string.
126 * @param src The source string to escape.
127 * @param n The size of the destination buffer.
129 * @return The destination string.
131 char *
132 path_quote_filename(char *dst, const char *src, size_t n)
134 char *ret = dst;
135 ssize_t size = (ssize_t)n;
137 if (size == 0)
138 return ret;
140 while (*src) {
141 if (*src == '\\') {
142 size -= 2;
143 if (size <= 0)
144 break;
146 *dst++ = '\\';
147 *dst++ = '\\';
148 src++;
149 } else if (((*src) & 0x80) == '\0') {
150 size--;
151 if (size <= 0)
152 break;
154 *dst++ = *src++;
155 } else {
156 size -= 4;
157 if (size <= 0)
158 break;
160 sprintf(dst, "\\%03o",
161 *(const unsigned char *)src);
162 dst += 4;
163 src++;
167 *dst = '\0';
169 return ret;