386BSD 0.1 development
[unix-history] / usr / src / bin / pwd / pwd.c
index ce0ef5c..ac81eb6 100644 (file)
@@ -1,80 +1,60 @@
-static char *sccsid = "@(#)pwd.c       4.3 (Berkeley) %G%";
 /*
 /*
- * Print working (current) directory
+ * Copyright (c) 1991 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
  */
-#include       <stdio.h>
-#include       <sys/param.h>
-#include       <sys/stat.h>
-#include       <dir.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) 2/20/91";
+#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);
 }
 }