1 /* 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. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static char sccsid[] = "@(#)getcwd.c 5.4 (Berkeley) 03/18/89"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <sys/param.h> 23 #include <sys/stat.h> 24 #include <dirent.h> 25 26 char * 27 getwd(store) 28 char *store; 29 { 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(); 39 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); 53 } 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); 74 break; 75 } 76 } 77 closedir(dir); 78 *pu = '\0'; 79 if (lstat(up, &s)) { 80 err: (void)sprintf(path, "getwd: %s", strerror(errno)); 81 return((char *)NULL); 82 } 83 } 84 } 85