home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / vers_con / dosrcs / curdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-18  |  5.1 KB  |  156 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. #ifndef lint
  35. static char rcsid[]=
  36.  "$Header: /site/tmp/dosrcs/src/RCS/curdir.c,v 5.3 90/07/15 20:23:48 lfk Release $";
  37. #endif
  38.  
  39. /*******************************************************************
  40.  *      $Log:    curdir.c,v $
  41.  * Revision 5.3  90/07/15  20:23:48  lfk
  42.  * Most major fixes added between rev 5.1 and rev 5.5:
  43.  *     signals fixed so they work on MS-DOS
  44.  *     Added MKS arguments code so argv can be large
  45.  *     added code to handle slashes a'la Unix
  46.  *     added more file extensions to system from MS-DOS
  47.  * 
  48.  * Revision 5.2  90/07/15  11:31:24  ROOT_DOS
  49.  * DOS version of RCS 4.0 checked in for MODS
  50.  * by lfk@athena.mit.edu
  51.  * Also update to MSC 6.0
  52.  * 
  53.  * Revision 3.3  89/05/01  15:11:49  narten
  54.  * changed copyright header to reflect current distribution rules
  55.  * 
  56.  * Revision 3.2  87/10/18  10:21:49  narten
  57.  * Updating version numbers. Changes relative to 1.1 are actually 
  58.  * relative to 3.2
  59.  * 
  60.  * Revision 1.1  84/01/23  14:50:01  kcs
  61.  * Initial revision
  62.  * 
  63.  * Revision 3.2  82/12/24  15:41:51  wft
  64.  * Changed curdir() such that it returns a pointer to the pathname of
  65.  * the current working directory, just as Berkeley's getcwd().
  66.  * 
  67.  * Revision 3.1  82/10/18  21:16:21  wft
  68.  * curdir() now uses stdio buffering for directory reads,
  69.  * returns to working directory after done, and closes the directories.
  70.  * A testprogram was also added.
  71.  * 
  72.  *******************************************************************/
  73.  
  74.  
  75. #include        "rcsbase.h"
  76. #include        <sys/param.h>
  77. #include        <sys/stat.h>
  78. #include        <sys/dir.h>
  79. #define dot     "."
  80. #define dotdot  ".."
  81.  
  82.  
  83. static char cwd[NCPPN];
  84.  
  85. char * curdir()
  86. /* Function: places the pathname of the current directory into cwd
  87.  * and returns a pointer to it. Returns NULL on failure.
  88.  */
  89. {
  90.         FILE    *file;
  91.         struct  stat    d, dd;
  92.         struct  direct  dir;
  93.  
  94.         int rdev, rino;
  95.         int off;
  96.         register i,j;
  97.  
  98.         cwd[off= 0] = '/';
  99.         cwd[1] = '\0';
  100.         stat("/", &d);
  101.         rdev = d.st_dev;
  102.         rino = d.st_ino;
  103.         for (;;) {
  104.                 if (stat(dot, &d)<0) return NULL;
  105.                 if (d.st_ino==rino && d.st_dev==rdev) {
  106.                         if (cwd[off] == '/') cwd[off] = '\0';
  107.                         chdir(cwd); /*change back to current directory*/
  108.                         return cwd;
  109.                 }
  110.                 if ((file = fopen(dotdot,"r")) == NULL) return NULL;
  111.                 if (fstat(fileno(file), &dd)<0) goto fail;
  112.                 chdir(dotdot);
  113.                 if(d.st_dev == dd.st_dev) {
  114.                         if(d.st_ino == dd.st_ino) {
  115.                             if (cwd[off] == '/') cwd[off] = '\0';
  116.                             chdir(cwd); /*change back to current directory*/
  117.                             fclose(file);
  118.                             return cwd;
  119.                         }
  120.                         do {
  121.                             if (fread((char *)&dir, sizeof(dir), 1, file) !=1)
  122.                                 goto fail;
  123.                         } while (dir.d_ino != d.st_ino);
  124.                 }
  125.                 else do {
  126.                         if(fread((char *)&dir, sizeof(dir), 1, file) != 1) {
  127.                             goto fail;
  128.                         }
  129.                         stat(dir.d_name, &dd);
  130.                 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  131.                 fclose(file);
  132.  
  133.                 /* concatenate file name */
  134.                 i = -1;
  135.                 while (dir.d_name[++i] != 0);
  136.                 for(j=off+1; j>0; --j)
  137.                         cwd[j+i+1] = cwd[j];
  138.                 off=i+off+1;
  139.                 cwd[i+1] = '/';
  140.                 for(--i; i>=0; --i)
  141.                         cwd[i+1] = dir.d_name[i];
  142.         } /* end for */
  143.  
  144. fail:   fclose(file);
  145.         return NULL;
  146. }
  147.  
  148.  
  149. #ifdef TEST
  150. main ()
  151. {
  152.         printf ("pwd = %s\n", curdir());
  153. }
  154. #endif TEST
  155.  
  156.