summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/path.c
blob: 1a4dba182b0e17076499644e40e75653d8cdc314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * libdpkg - Debian packaging suite library routines
 * path.c - path handling functions
 *
 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <dpkg/dpkg.h>
#include <dpkg/string.h>
#include <dpkg/path.h>

/**
 * Trim ‘/’ and ‘/.’ from the end of a pathname.
 *
 * The given string will get NUL-terminatd.
 *
 * @param path The pathname to trim.
 *
 * @return The size of the trimmed pathname.
 */
size_t
path_trim_slash_slashdot(char *path)
{
	char *end;

	if (str_is_unset(path))
		return 0;

	for (end = path + strlen(path) - 1; end - path >= 1; end--) {
		if (*end == '/' || (*(end - 1) == '/' && *end == '.'))
			*end = '\0';
		else
			break;
	}

	return end - path + 1;
}

/**
 * Skip ‘/’ and ‘./’ from the beginning of a pathname.
 *
 * @param path The pathname to skip.
 *
 * @return The new beginning of the pathname.
 */
const char *
path_skip_slash_dotslash(const char *path)
{
	while (path[0] == '/' || (path[0] == '.' && path[1] == '/'))
		path++;

	return path;
}

/**
 * Return the last component of a pathname.
 *
 * @param path The pathname to get the base name from.
 *
 * @return A pointer to the last component inside pathname.
 */
const char *
path_basename(const char *path)
{
	const char *last_slash;

	last_slash = strrchr(path, '/');
	if (last_slash == NULL)
		return path;
	else
		return last_slash + 1;
}

/**
 * Create a template for a temporary pathname.
 *
 * @param suffix The suffix to use for the template string.
 *
 * @return An allocated string with the created template.
 */
char *
path_make_temp_template(const char *suffix)
{
	const char *tmpdir;

	tmpdir = getenv("TMPDIR");
	if (!tmpdir)
		tmpdir = P_tmpdir;

	return str_fmt("%s/%s.XXXXXX", tmpdir, suffix);
}

/**
 * Escape characters in a pathname for safe locale printing.
 *
 * We need to quote paths so that they do not cause problems when printing
 * them, for example with snprintf(3) which does not work if the format
 * string contains %s and an argument has invalid characters for the
 * current locale, it will then return -1.
 *
 * To simplify things, we just escape all 8 bit characters, instead of
 * just invalid characters.
 *
 * @param dst The escaped destination string.
 * @param src The source string to escape.
 * @param n The size of the destination buffer.
 *
 * @return The destination string.
 */
char *
path_quote_filename(char *dst, const char *src, size_t n)
{
	char *r = dst;
	ssize_t size = (ssize_t)n;

	if (size == 0)
		return r;

	while (*src) {
		if (*src == '\\') {
			size -= 2;
			if (size <= 0)
				break;

			*dst++ = '\\';
			*dst++ = '\\';
			src++;
		} else if (((*src) & 0x80) == '\0') {
			size--;
			if (size <= 0)
				break;

			*dst++ = *src++;
		} else {
			size -= 4;
			if (size <= 0)
				break;

			sprintf(dst, "\\%03o",
			        *(const unsigned char *)src);
			dst += 4;
			src++;
		}
	}

	*dst = '\0';

	return r;
}