BSD 4_4 release
[unix-history] / usr / src / usr.bin / f77 / libU77 / getcwd_.c
index 715cceb..a9b0831 100644 (file)
@@ -1,96 +1,17 @@
-/** F77 NOTE: the getcwd() routine should be in libc.a ! **/
-/*
- * return name of working (current) directory
+/*-
+ * Copyright (c) 1980 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This module is believed to contain source code proprietary to AT&T.
+ * Use and redistribution is subject to the Berkeley Software License
+ * Agreement and your Software Agreement with AT&T (Western Electric).
  */
  */
-#include       <stdio.h>
-#include       <sys/param.h>
-#include       <sys/stat.h>
-#include       <sys/dir.h>
-
-static char    dot[]   = ".";
-static char    dotdot[] = "..";
-static char    name[128];
 
 
-char *
-getcwd()
-{
-       int     rdev, rino;
-       int     fd;
-       struct  stat    d, dd;
-       struct  direct  dir;
-       char    *prepend();
-       char    *namep = &name[(sizeof name)-1];
-
-       *namep = '\0';
-       stat("/", &d);
-       rdev = d.st_dev;
-       rino = d.st_ino;
-       for (;;)
-       {
-               stat(dot, &d);
-               if (d.st_ino == rino && d.st_dev == rdev)
-               {
-                       chdir(namep);
-                       return(namep);
-               }
-               if ((fd = open(dotdot,0)) < 0)
-               {
-                       chdir(prepend(namep, dot));
-                       return((char *)0);
-               }
-               chdir(dotdot);
-               fstat(fd, &dd);
-               if(d.st_dev == dd.st_dev)
-               {
-                       if(d.st_ino == dd.st_ino)
-                       {
-                               close(fd);
-                               chdir(namep);
-                               return(namep);
-                       }
-                       do
-                       {
-                               if (read(fd, (char *)&dir, sizeof(dir)) < sizeof(dir))
-                               {
-                                       close(fd);
-                                       chdir(prepend(namep, dot));
-                                       return((char *)0);
-                               }
-                       } while (dir.d_ino != d.st_ino);
-               }
-               else do
-               {
-                               if(read(fd, (char *)&dir, sizeof(dir)) < sizeof(dir))
-                               {
-                                       close(fd);
-                                       chdir(prepend(namep, dot));
-                                       return((char *)0);
-                               }
-                               stat(dir.d_name, &dd);
-                       } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
-               close(fd);
-               namep = prepend(prepend(namep, dir.d_name), "/");
-       }
-}
-
-char *
-prepend(p, n)
-char *p;
-char *n;
-{
-       int i = 0;
-
-       while (i < DIRSIZ && *n)
-       {
-               n++; i++;
-       }
-       while (i--)
-               *--p = *--n;
-       return(p);
-}
+#ifndef lint
+static char sccsid[] = "@(#)getcwd_.c  5.2 (Berkeley) 4/12/91";
+#endif /* not lint */
 
 /*
 
 /*
-char id_getcwd[] = "@(#)getcwd_.c      1.1";
  * Get pathname of current working directory.
  *
  * calling sequence:
  * Get pathname of current working directory.
  *
  * calling sequence:
@@ -101,20 +22,26 @@ char id_getcwd[] = "@(#)getcwd_.c  1.1";
  *     ierr will be 0 if successful, a system error code otherwise.
  */
 
  *     ierr will be 0 if successful, a system error code otherwise.
  */
 
+#include <sys/param.h>
+#ifndef        MAXPATHLEN
+#define MAXPATHLEN     128
+#endif
+
 extern int errno;
 extern int errno;
+char   *getwd();
 
 long
 getcwd_(path, len)
 char *path;
 
 long
 getcwd_(path, len)
 char *path;
-int len;
+long len;
 {
 {
-       char *p;
+       char    *p;
+       char    pathname[MAXPATHLEN];
 
 
-       p = getcwd();
+       p = getwd(pathname);
+       b_char(pathname, path, len);
        if (p)
        if (p)
-       {
-               b_char(p, path, len);
                return(0L);
                return(0L);
-       }
-       return((long)errno);
+       else
+               return((long)errno);
 }
 }