home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RCS_SRC.ZIP / CURDIR.C < prev    next >
C/C++ Source or Header  |  1991-01-15  |  5KB  |  143 lines

  1. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  2.    Distributed under license by the Free Software Foundation, Inc.
  3.  
  4. This file is part of RCS.
  5.  
  6. RCS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. RCS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with RCS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. Report problems and direct all questions to:
  21.  
  22.     rcs-bugs@cs.purdue.edu
  23.  
  24. */
  25.  
  26. /*******************************************************************
  27.  *                     curdir: get current directory
  28.  *******************************************************************
  29.  * returns full pathname of working (current) directory.
  30.  * This is an adaptation of pwd, and works for grafted directories.
  31.  * Unlike pwd, returns to current directory after it is finished.
  32.  * Uses stdio buffering for directory reads.
  33.  */
  34.  
  35.  static char rcsid[]=
  36.  "$Header: /usr/src/local/bin/rcs/src/RCS/curdir.c,v 3.3 89/05/01 15:11:49 narten Exp $";
  37.  
  38. /*******************************************************************
  39.  *      $Log:    curdir.c,v $
  40.  * Revision 3.3  89/05/01  15:11:49  narten
  41.  * changed copyright header to reflect current distribution rules
  42.  * 
  43.  * Revision 3.2  87/10/18  10:21:49  narten
  44.  * Updating version numbers. Changes relative to 1.1 are actually 
  45.  * relative to 3.2
  46.  * 
  47.  * Revision 1.1  84/01/23  14:50:01  kcs
  48.  * Initial revision
  49.  * 
  50.  * Revision 3.2  82/12/24  15:41:51  wft
  51.  * Changed curdir() such that it returns a pointer to the pathname of
  52.  * the current working directory, just as Berkeley's getcwd().
  53.  * 
  54.  * Revision 3.1  82/10/18  21:16:21  wft
  55.  * curdir() now uses stdio buffering for directory reads,
  56.  * returns to working directory after done, and closes the directories.
  57.  * A testprogram was also added.
  58.  * 
  59.  *******************************************************************/
  60.  
  61.  
  62. #include        "rcsbase.h"
  63. #include        <sys/param.h>
  64. #include        <sys/stat.h>
  65. #include        <sys/dir.h>
  66. #define dot     "."
  67. #define dotdot  ".."
  68.  
  69.  
  70. static char cwd[NCPPN];
  71.  
  72. char * curdir()
  73. /* Function: places the pathname of the current directory into cwd
  74.  * and returns a pointer to it. Returns NULL on failure.
  75.  */
  76. {
  77.         FILE    *file;
  78.         struct  stat    d, dd;
  79.         struct  direct  dir;
  80.  
  81.         int rdev, rino;
  82.         int off;
  83.         register i,j;
  84.  
  85.         cwd[off= 0] = '/';
  86.         cwd[1] = '\0';
  87.         stat("/", &d);
  88.         rdev = d.st_dev;
  89.         rino = d.st_ino;
  90.         for (;;) {
  91.                 if (stat(dot, &d)<0) return NULL;
  92.                 if (d.st_ino==rino && d.st_dev==rdev) {
  93.                         if (cwd[off] == '/') cwd[off] = '\0';
  94.                         chdir(cwd); /*change back to current directory*/
  95.                         return cwd;
  96.                 }
  97.                 if ((file = fopen(dotdot,"r")) == NULL) return NULL;
  98.                 if (fstat(fileno(file), &dd)<0) goto fail;
  99.                 chdir(dotdot);
  100.                 if(d.st_dev == dd.st_dev) {
  101.                         if(d.st_ino == dd.st_ino) {
  102.                             if (cwd[off] == '/') cwd[off] = '\0';
  103.                             chdir(cwd); /*change back to current directory*/
  104.                             fclose(file);
  105.                             return cwd;
  106.                         }
  107.                         do {
  108.                             if (fread((char *)&dir, sizeof(dir), 1, file) !=1)
  109.                                 goto fail;
  110.                         } while (dir.d_ino != d.st_ino);
  111.                 }
  112.                 else do {
  113.                         if(fread((char *)&dir, sizeof(dir), 1, file) != 1) {
  114.                             goto fail;
  115.                         }
  116.                         stat(dir.d_name, &dd);
  117.                 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  118.                 fclose(file);
  119.  
  120.                 /* concatenate file name */
  121.                 i = -1;
  122.                 while (dir.d_name[++i] != 0);
  123.                 for(j=off+1; j>0; --j)
  124.                         cwd[j+i+1] = cwd[j];
  125.                 off=i+off+1;
  126.                 cwd[i+1] = '/';
  127.                 for(--i; i>=0; --i)
  128.                         cwd[i+1] = dir.d_name[i];
  129.         } /* end for */
  130.  
  131. fail:   fclose(file);
  132.         return NULL;
  133. }
  134.  
  135.  
  136. #ifdef TEST
  137. main ()
  138. {
  139.         printf ("pwd = %s\n", curdir());
  140. }
  141. #endif TEST
  142.  
  143.