Debian Codegraph @

src/readlink.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2013 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 #include <sys/types.h>
24 #include <stddef.h>
25 #include "libfakechroot.h"
26
27
28 wrapper(readlink, READLINK_TYPE_RETURN, (const char * path, char * buf, READLINK_TYPE_ARG3(bufsiz)))
29 {
30 char fakechroot_abspath[FAKECHROOT_PATH_MAX];
31 char fakechroot_buf[FAKECHROOT_PATH_MAX];
32
33 int linksize;
34 char tmp[FAKECHROOT_PATH_MAX], *tmpptr;
35
36 const char *fakechroot_base = getenv("FAKECHROOT_BASE");
37
38 debug("readlink(\"%s\", &buf, %zd)", path, bufsiz);
39 if (!strcmp(path, "/etc/malloc.conf")) {
40 errno = ENOENT;
41 return -1;
42 }
43 expand_chroot_path(path);
44
45 if ((linksize = nextcall(readlink)(path, tmp, FAKECHROOT_PATH_MAX-1)) == -1) {
46 return -1;
47 }
48 tmp[linksize] = '\0';
49
50 if (fakechroot_base != NULL) {
51 tmpptr = strstr(tmp, fakechroot_base);
52 if (tmpptr != tmp) {
53 tmpptr = tmp;
54 }
55 else if (tmp[strlen(fakechroot_base)] == '\0') {
56 tmpptr = "/";
57 linksize = strlen(tmpptr);
58 }
59 else if (tmp[strlen(fakechroot_base)] == '/') {
60 tmpptr = tmp + strlen(fakechroot_base);
61 linksize -= strlen(fakechroot_base);
62 }
63 else {
64 tmpptr = tmp;
65 }
66 if (strlen(tmpptr) > bufsiz) {
67 linksize = bufsiz;
68 }
69 strncpy(buf, tmpptr, linksize);
70 }
71 else {
72 if (linksize > bufsiz) {
73 linksize = bufsiz;
74 }
75 strncpy(buf, tmp, linksize);
76 }
77 return linksize;
78 }