|
|
1.1 root 1: /*
2: * libc/gen/getcwd.c
3: */
4:
5: #include <stddef.h>
6: #include <sys/types.h>
7: #include <string.h>
8: #include <errno.h>
9:
10: extern char *_getwd();
11: extern char *malloc();
12:
13: /*
14: * Set errno and return NULL.
15: */
16: static
17: char *
18: error(n) register int n;
19: {
20: errno = n;
21: return NULL;
22: }
23:
24: /*
25: * Get current working directory.
26: * Cf. SVID Appendix 2.2 and Posix 5.2.2.
27: * This calls the COHERENT function _getwd(),
28: * which takes no args and returns a pointer
29: * to a statically allocated string.
30: */
31: char *
32: getcwd(buf, size) char *buf; size_t size;
33: {
34: register char *dir;
35: register size_t len;
36:
37: if (size == 0)
38: return error(EINVAL);
39: if ((dir = _getwd()) == NULL)
40: return NULL; /* cannot find cwd */
41: len = strlen(dir);
42: if (size < len + 1)
43: return error(ERANGE);
44: /*
45: * The following behavior is specified by SVID,
46: * Posix says the behavior is undefined in this case.
47: */
48: if (buf == NULL && (buf = malloc(size)) == NULL)
49: return error(ENOMEM);
50: return strcpy(buf, dir);
51: }
52:
53: /* end of libc/gen/getcwd.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.