use the library globbing routines instead of rolling our own
[unix-history] / usr / src / bin / pwd / pwd.c
index ac6835d..b48dd3e 100644 (file)
@@ -1,80 +1,34 @@
-static char *sccsid = "@(#)pwd.c       4.2 (Berkeley) %G%";
 /*
 /*
- * Print working (current) directory
+ * Copyright (c) 1991 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
  */
-#include       <stdio.h>
-#include       <sys/param.h>
-#include       <sys/stat.h>
-#include       <ndir.h>
 
 
-char   dot[]   = ".";
-char   dotdot[] = "..";
-char   name[BUFSIZ];
-DIR    *file;
-int    off     = -1;
-struct stat    d, dd;
-struct direct  *dir;
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1991 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
 
 
-main()
-{
-       int rdev, rino;
+#ifndef lint
+static char sccsid[] = "@(#)pwd.c      5.4 (Berkeley) %G%";
+#endif /* not lint */
 
 
-       stat("/", &d);
-       rdev = d.st_dev;
-       rino = d.st_ino;
-       for (;;) {
-               stat(dot, &d);
-               if (d.st_ino==rino && d.st_dev==rdev)
-                       prname();
-               if ((file = opendir(dotdot)) == NULL) {
-                       fprintf(stderr,"pwd: cannot open ..\n");
-                       exit(1);
-               }
-               fstat(file->dd_fd, &dd);
-               chdir(dotdot);
-               if(d.st_dev == dd.st_dev) {
-                       if(d.st_ino == dd.st_ino)
-                               prname();
-                       do
-                               if ((dir = readdir(file)) == NULL) {
-                                       fprintf(stderr,"read error in ..\n");
-                                       exit(1);
-                               }
-                       while (dir->d_ino != d.st_ino);
-               } else
-                       do {
-                               if ((dir = readdir(file)) == NULL) {
-                                       fprintf(stderr,"read error in ..\n");
-                                       exit(1);
-                               }
-                               stat(dir->d_name, &dd);
-                       } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
-               closedir(file);
-               cat();
-       }
-}
+#include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
 
 
-prname()
-{
-       write(1, "/", 1);
-       if (off<0)
-               off = 0;
-       name[off] = '\n';
-       write(1, name, off+1);
-       exit(0);
-}
-
-cat()
+main()
 {
 {
-       register i, j;
+       char *p;
 
 
-       i = dir->d_namlen;
-       if ((off + dir->d_namlen + 2) > BUFSIZ-1)
-               prname();
-       for(j=off+1; j>=0; --j)
-               name[j+i+1] = name[j];
-       off=i+off+1;
-       name[i] = '/';
-       for(--i; i>=0; --i)
-               name[i] = dir->d_name[i];
+       p = getcwd((char *)NULL, 0);
+       if (p) {
+               (void)printf("%s\n", p);
+               exit(0);
+       }
+       (void)fprintf(stderr, "pwd: %s\n", strerror(errno));
+       exit(1);
 }
 }