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/>.
29 #include <dpkg/dpkg.h>
30 #include <dpkg/string.h>
31 #include <dpkg/path.h>
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.
43 path_trim_slash_slashdot(char *path
)
47 if (str_is_unset(path
))
50 for (end
= path
+ strlen(path
) - 1; end
- path
>= 1; end
--) {
51 if (*end
== '/' || (*(end
- 1) == '/' && *end
== '.'))
57 return end
- path
+ 1;
61 * Skip ‘/’ and ‘./’ from the beginning of a pathname.
63 * @param path The pathname to skip.
65 * @return The new beginning of the pathname.
68 path_skip_slash_dotslash(const char *path
)
70 while (path
[0] == '/' || (path
[0] == '.' && path
[1] == '/'))
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.
84 path_basename(const char *path
)
86 const char *last_slash
;
88 last_slash
= strrchr(path
, '/');
89 if (last_slash
== NULL
)
92 return last_slash
+ 1;
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.
103 path_make_temp_template(const char *suffix
)
107 tmpdir
= getenv("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.
132 path_quote_filename(char *dst
, const char *src
, size_t n
)
135 ssize_t size
= (ssize_t
)n
;
149 } else if (((*src
) & 0x80) == '\0') {
160 sprintf(dst
, "\\%03o",
161 *(const unsigned char *)src
);