Commit | Line | Data |
---|---|---|
bb0cfa24 | 1 | /* |
8a4b7e23 KB |
2 | * Copyright (c) 1989 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
bb0cfa24 DF |
16 | */ |
17 | ||
2ce81398 | 18 | #if defined(LIBC_SCCS) && !defined(lint) |
ef9e360f | 19 | static char sccsid[] = "@(#)getcwd.c 5.5 (Berkeley) %G%"; |
8a4b7e23 | 20 | #endif /* LIBC_SCCS and not lint */ |
9b6e6036 | 21 | |
87dccba9 SL |
22 | #include <sys/param.h> |
23 | #include <sys/stat.h> | |
8a4b7e23 | 24 | #include <dirent.h> |
9b6e6036 SL |
25 | |
26 | char * | |
8a4b7e23 KB |
27 | getwd(store) |
28 | char *store; | |
9b6e6036 | 29 | { |
8a4b7e23 KB |
30 | extern int errno; |
31 | register DIR *dir; | |
32 | register struct dirent *dp; | |
33 | register int first; | |
34 | register char *pp, *pu; | |
35 | struct stat s, tmp; | |
36 | dev_t root_dev; | |
37 | ino_t root_ino; | |
38 | char path[MAXPATHLEN], up[MAXPATHLEN], *strerror(); | |
9b6e6036 | 39 | |
8a4b7e23 KB |
40 | if (stat("/", &s)) |
41 | goto err; | |
42 | root_dev = s.st_dev; | |
43 | root_ino = s.st_ino; | |
44 | if (stat(".", &s)) | |
45 | goto err; | |
46 | pp = path + sizeof(path) - 1; | |
47 | *pp = '\0'; | |
48 | for (pu = up, first = 1;; first = 0) { | |
49 | if (root_dev == s.st_dev && root_ino == s.st_ino) { | |
50 | *store = '/'; | |
51 | (void)strcpy(store + 1, pp); | |
52 | return(store); | |
90432f68 | 53 | } |
8a4b7e23 KB |
54 | *pu++ = '.'; |
55 | *pu++ = '.'; | |
56 | *pu = '\0'; | |
57 | if (!(dir = opendir(up))) { | |
58 | (void)strcpy(path, "getwd: opendir failed."); | |
59 | return((char *)NULL); | |
60 | } | |
61 | *pu++ = '/'; | |
62 | while (dp = readdir(dir)) { | |
63 | if (dp->d_name[0] == '.' && (!dp->d_name[1] || | |
64 | dp->d_name[1] == '.' && !dp->d_name[2])) | |
65 | continue; | |
66 | bcopy(dp->d_name, pu, dp->d_namlen + 1); | |
67 | if (lstat(up, &tmp)) | |
68 | goto err; | |
69 | if (tmp.st_dev == s.st_dev && tmp.st_ino == s.st_ino) { | |
70 | if (!first) | |
71 | *--pp = '/'; | |
72 | pp -= dp->d_namlen; | |
73 | bcopy(dp->d_name, pp, dp->d_namlen); | |
90432f68 SL |
74 | break; |
75 | } | |
8a4b7e23 KB |
76 | } |
77 | closedir(dir); | |
78 | *pu = '\0'; | |
79 | if (lstat(up, &s)) { | |
ef9e360f | 80 | err: (void)sprintf(store, "getwd: %s", strerror(errno)); |
8a4b7e23 KB |
81 | return((char *)NULL); |
82 | } | |
90432f68 | 83 | } |
9b6e6036 | 84 | } |