Annotation of 43BSDReno/contrib/rcs/src/curdir.c, revision 1.1

1.1     ! root        1: /* Copyright (C) 1982, 1988, 1989 Walter Tichy
        !             2:  * All rights reserved.
        !             3:  *
        !             4:  * Redistribution and use in source and binary forms are permitted
        !             5:  * provided that the above copyright notice and this paragraph are
        !             6:  * duplicated in all such forms and that any documentation,
        !             7:  * advertising materials, and other materials related to such
        !             8:  * distribution and use acknowledge that the software was developed
        !             9:  * by Walter Tichy.
        !            10:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            11:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            13:  *
        !            14:  * Report all problems and direct all questions to:
        !            15:  *   [email protected]
        !            16:  * 
        !            17: 
        !            18: 
        !            19: 
        !            20: 
        !            21: 
        !            22: 
        !            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: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.