home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1982, 1988, 1989 Walter Tichy
- Distributed under license by the Free Software Foundation, Inc.
-
- This file is part of RCS.
-
- RCS is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- RCS is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with RCS; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- Report problems and direct all questions to:
-
- rcs-bugs@cs.purdue.edu
-
- */
-
- /*******************************************************************
- * 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.
- */
- #ifndef lint
- static char rcsid[]=
- "$Header: /site/tmp/dosrcs/src/RCS/curdir.c,v 5.3 90/07/15 20:23:48 lfk Release $";
- #endif
-
- /*******************************************************************
- * $Log: curdir.c,v $
- * Revision 5.3 90/07/15 20:23:48 lfk
- * Most major fixes added between rev 5.1 and rev 5.5:
- * signals fixed so they work on MS-DOS
- * Added MKS arguments code so argv can be large
- * added code to handle slashes a'la Unix
- * added more file extensions to system from MS-DOS
- *
- * Revision 5.2 90/07/15 11:31:24 ROOT_DOS
- * DOS version of RCS 4.0 checked in for MODS
- * by lfk@athena.mit.edu
- * Also update to MSC 6.0
- *
- * Revision 3.3 89/05/01 15:11:49 narten
- * changed copyright header to reflect current distribution rules
- *
- * Revision 3.2 87/10/18 10:21:49 narten
- * Updating version numbers. Changes relative to 1.1 are actually
- * relative to 3.2
- *
- * Revision 1.1 84/01/23 14:50:01 kcs
- * Initial revision
- *
- * Revision 3.2 82/12/24 15:41:51 wft
- * Changed curdir() such that it returns a pointer to the pathname of
- * the current working directory, just as Berkeley's getcwd().
- *
- * Revision 3.1 82/10/18 21:16:21 wft
- * curdir() now uses stdio buffering for directory reads,
- * returns to working directory after done, and closes the directories.
- * A testprogram was also added.
- *
- *******************************************************************/
-
-
- #include "rcsbase.h"
- #include <sys/param.h>
- #include <sys/stat.h>
- #include <sys/dir.h>
- #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;
-
- 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
-
-