Debian Codegraph @

src/mkstemps.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2013-2015 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #ifdef HAVE_MKSTEMPS
24
25 #define _BSD_SOURCE
26 #define _DEFAULT_SOURCE
27 #include <errno.h>
28 #include <stdlib.h>
29
30 #include "libfakechroot.h"
31 #include "strlcpy.h"
32
33
34 wrapper(mkstemps, int, (char * template, int suffixlen))
35 {
36 char tmp[FAKECHROOT_PATH_MAX], *tmpptr = tmp;
37 char *xxxsrc, *xxxdst;
38 int xxxlen = 0;
39 int fd;
40 char fakechroot_abspath[FAKECHROOT_PATH_MAX];
41 char fakechroot_buf[FAKECHROOT_PATH_MAX];
42
43 debug("mkstemps(\"%s\", %d)", template, suffixlen);
44
45 if (strlen(template)+suffixlen < 6) {
46 __set_errno(EINVAL);
47 return -1;
48 }
49
50 strlcpy(tmp, template, FAKECHROOT_PATH_MAX);
51
52 if (!fakechroot_localdir(tmp)) {
53 expand_chroot_path(tmpptr);
54 }
55
56 for (xxxdst = template; *xxxdst; xxxdst++);
57 for (xxxdst -= 1 + suffixlen; *xxxdst == 'X'; xxxdst--, xxxlen++);
58 xxxdst++;
59
60 for (xxxsrc = tmpptr; *xxxsrc; xxxsrc++);
61 for (xxxsrc -= 1 + suffixlen; *xxxsrc == 'X'; xxxsrc--);
62 xxxsrc++;
63
64 if ((fd = nextcall(mkstemps)(tmpptr, suffixlen)) == -1 || !*tmpptr) {
65 goto error;
66 }
67
68 memmove(xxxdst, xxxsrc, xxxlen);
69 return fd;
70
71 error:
72 *template = '\0';
73 return fd;
74 }
75
76 #else
77 typedef int empty_translation_unit;
78 #endif