/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred."
 *
 */
/* curdir.c */

#ident "$Header: curdir.c 1.4 89/10/10 $

/*******************************************************************
 *                     curdir: get current directory
 *******************************************************************
 * returns full pathname of working (current) directory.
 * This is an adaptation of pwd, and works for grafted directories.
 * Unlike pwd, returns to current directory after it is finished.
 * Uses stdio buffering for directory reads.
 */

 static char rcsid[]=
 "$Header: curdir.c 1.4 89/10/10 $";

/*******************************************************************
 *      $Log:	curdir.c,v $
 *******************************************************************/
 

#include        "rcsbase.h"
#include        <sys/param.h>
#include        <sys/stat.h>
/* #include        <sys/dir.h>		   XXX */
#include	<dirent.h>		/* XXX */
#define dot     "."
#define dotdot  ".."


static char cwd[NCPPN];

char * curdir()
/* Function: places the pathname of the current directory into cwd
 * and returns a pointer to it. Returns NULL on failure.
 */
{
        FILE    *file;
        struct  stat    d, dd;
   /*   struct  direct  dir;		   XXX */
        struct  dirent  dir;		/* XXX */

        int rdev, rino;
        int off;
        register i,j;

        cwd[off= 0] = '/';
        cwd[1] = '\0';
        stat("/", &d);
        rdev = d.st_dev;
        rino = d.st_ino;
        for (;;) {
                if (stat(dot, &d)<0) return NULL;
                if (d.st_ino==rino && d.st_dev==rdev) {
                        if (cwd[off] == '/') cwd[off] = '\0';
                        chdir(cwd); /*change back to current directory*/
                        return cwd;
                }
                if ((file = fopen(dotdot,"r")) == NULL) return NULL;
                if (fstat(fileno(file), &dd)<0) goto fail;
                chdir(dotdot);
                if(d.st_dev == dd.st_dev) {
                        if(d.st_ino == dd.st_ino) {
                            if (cwd[off] == '/') cwd[off] = '\0';
                            chdir(cwd); /*change back to current directory*/
                            fclose(file);
                            return cwd;
                        }
                        do {
                            if (fread((char *)&dir, sizeof(dir), 1, file) !=1)
                                goto fail;
                        } while (dir.d_ino != d.st_ino);
                }
                else do {
                        if(fread((char *)&dir, sizeof(dir), 1, file) != 1) {
                            goto fail;
                        }
                        stat(dir.d_name, &dd);
                } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
                fclose(file);

                /* concatenate file name */
                i = -1;
                while (dir.d_name[++i] != 0);
                for(j=off+1; j>0; --j)
                        cwd[j+i+1] = cwd[j];
                off=i+off+1;
                cwd[i+1] = '/';
                for(--i; i>=0; --i)
                        cwd[i+1] = dir.d_name[i];
        } /* end for */

fail:   fclose(file);
        return NULL;
}


#ifdef TEST
main ()
{
        printf ("pwd = %s\n", curdir());
}
#endif /* TEST */

