3
3
* path.c - path handling functions
5
5
* Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
6
* Copyright © 2008, 2009 Guillem Jover <guillem@debian.org>
6
* Copyright © 2008-2010 Guillem Jover <guillem@debian.org>
8
8
* This is free software; you can redistribute it and/or modify
9
9
* it under the terms of the GNU General Public License as published by
22
22
#include <config.h>
23
23
#include <compat.h>
26
25
#include <stdlib.h>
27
26
#include <string.h>
30
#include <dpkg/varbuf.h>
29
#include <dpkg/dpkg.h>
31
30
#include <dpkg/path.h>
33
* Trim ‘/’ and ‘/.’ from the end of a pathname.
35
* The given string will get NUL-terminatd.
37
* @param path The pathname to trim.
39
* @return The size of the trimmed pathname.
34
path_rtrim_slash_slashdot(char *path)
42
path_trim_slash_slashdot(char *path)
81
varbufprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
96
m_asprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
83
return varbuf_detach(&template);
87
* snprintf(3) doesn't work if format contains %.<nnn>s and an argument has
88
* invalid char for locale, then it returns -1.
89
* ohshite() is ok, but fd_fd_copy(), which is used in tarobject(), is not
92
* - fd_fd_copy() == buffer_copy_TYPE() ‘lib/dpkg/buffer.h’.
93
* - buffer_copy_TYPE() uses varbufvprintf(&v, desc, al); ‘lib/dpkg/buffer.c’.
94
* - varbufvprintf() fails, because it calls with:
95
* fmt = "backend dpkg-deb during '%.255s'"
96
* arg may contain some invalid char, for example,
97
* «/usr/share/doc/console-tools/examples/unicode/\342\231\252\342\231\254»
100
* In this case, if the user uses some locale which doesn't support
101
* “\342\231...”, vsnprintf() always returns -1 and varbufextend() fails.
103
* So, we need to escape invalid char, probably as in
104
* ‘tar-1.13.19/lib/quotearg.c: quotearg_buffer_restyled()’
105
* but here we escape all 8 bit chars, in order make it simple.
102
* Escape characters in a pathname for safe locale printing.
104
* We need to quote paths so that they do not cause problems when printing
105
* them, for example with snprintf(3) which does not work if the format
106
* string contains %s and an argument has invalid characters for the
107
* current locale, it will then return -1.
109
* To simplify things, we just escape all 8 bit characters, instead of
110
* just invalid characters.
112
* @param dst The escaped destination string.
113
* @param src The source string to escape.
114
* @param n The size of the destination buffer.
116
* @return The destination string.
108
119
path_quote_filename(char *dst, const char *src, size_t n)