No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / rcs / src / curdir.c
blob61af1b5ed0b4bc818041ab5e58e863c1467199ea
1 /* Copyright (C) 1982, 1988, 1989 Walter Tichy
2 * All rights reserved.
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.
14 * Report all problems and direct all questions to:
15 * rcs-bugs@cs.purdue.edu
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.
35 static char rcsid[]=
36 "$Header: /pub/NetBSD/misc/repositories/cvsroot/src/usr.bin/rcs/src/Attic/curdir.c,v 1.1 1993/03/21 09:58:06 cgd Exp $";
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
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
47 * Revision 1.1 84/01/23 14:50:01 kcs
48 * Initial revision
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().
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.
59 *******************************************************************/
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 ".."
70 static char cwd[NCPPN];
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.
77 FILE *file;
78 struct stat d, dd;
79 struct direct dir;
81 int rdev, rino;
82 int off;
83 register i,j;
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;
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;
107 do {
108 if (fread((char *)&dir, sizeof(dir), 1, file) !=1)
109 goto fail;
110 } while (dir.d_ino != d.st_ino);
112 else do {
113 if(fread((char *)&dir, sizeof(dir), 1, file) != 1) {
114 goto fail;
116 stat(dir.d_name, &dd);
117 } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
118 fclose(file);
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 */
131 fail: fclose(file);
132 return NULL;
136 #ifdef TEST
137 main ()
139 printf ("pwd = %s\n", curdir());
141 #endif TEST