aboutsummaryrefslogtreecommitdiffstats
path: root/guts/getcwd.c
blob: 18c47480f5415196b22e794f8e2ad3f104d5bc2e (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
/* 
 * Copyright (c) 2008-2010 Wind River Systems; see
 * guts/COPYRIGHT for information.
 *
 * static char *
 * wrap_getcwd(char *buf, size_t size) {
 *	char * rc = NULL;
 */
	pseudo_debug(3, "wrap_getcwd: %p, %lu\n",
		(void *) buf, (unsigned long) size);
	if (!pseudo_cwd) {
		pseudo_diag("Asked for CWD, but don't have it!\n");
		errno = EACCES;
		return NULL;
	}
	/* emulate Linux semantics in case of non-Linux systems. */
	if (!buf) {
		/* if we don't have a cwd, something's very wrong... */
		if (!size) {
			size = pseudo_cwd_len + 1;
			if (pseudo_chroot_len && size >= pseudo_chroot_len &&
				!memcmp(pseudo_cwd, pseudo_chroot, pseudo_chroot_len)) {
				size -= pseudo_chroot_len;	
				/* if cwd is precisely the same as chroot, we
				 * actually want a /, not an empty string
				 */
				if (size < 2)
					size = 2;
			}
		}
		if (size) {
			buf = malloc(size);
		} else {
			pseudo_diag("can't figure out CWD: length %ld + 1 - %ld => %ld\n",
				(unsigned long) pseudo_cwd_len,
				(unsigned long) pseudo_chroot_len,
				(unsigned long) size);
		}
		if (!buf) {
			pseudo_diag("couldn't allocate requested CWD buffer - need %ld byes\n",
				(unsigned long) size);
			errno = ENOMEM;
			return NULL;
		}
	}
	if (pseudo_cwd_len - (pseudo_cwd_rel - pseudo_cwd) >= size) {
		pseudo_diag("only %ld bytes available, need %ld (%ld + 1 - %ld)\n",
			(unsigned long) size,
			(unsigned long) pseudo_cwd_len + 1 - pseudo_chroot_len,
			(unsigned long) pseudo_cwd_len,
			(unsigned long) pseudo_chroot_len);
		errno = ENAMETOOLONG;
		return NULL;
	}
	rc = buf;
	pseudo_debug(3, "getcwd: copying %d (%d + 1 - %d) characters from <%s>.\n",
		(int) ((pseudo_cwd_len + 1) - pseudo_chroot_len),
		pseudo_cwd_len, pseudo_chroot_len,
		pseudo_cwd_rel);
	memcpy(buf, pseudo_cwd_rel, (pseudo_cwd_len + 1) - (pseudo_cwd_rel - pseudo_cwd));
	if (!*buf) {
		strcpy(buf, "/");
	}

/*	return rc;
 * }
 */