1 /* Copyright (C) 1982, 1988, 1989 Walter Tichy
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
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.
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 /*******************************************************************
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
47 * Revision 1.1 84/01/23 14:50:01 kcs
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 *******************************************************************/
63 #include <sys/param.h>
70 static char cwd
[NCPPN
];
73 /* Function: places the pathname of the current directory into cwd
74 * and returns a pointer to it. Returns NULL on failure.
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*/
97 if ((file
= fopen(dotdot
,"r")) == NULL
) return NULL
;
98 if (fstat(fileno(file
), &dd
)<0) goto fail
;
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*/
108 if (fread((char *)&dir
, sizeof(dir
), 1, file
) !=1)
110 } while (dir
.d_ino
!= d
.st_ino
);
113 if(fread((char *)&dir
, sizeof(dir
), 1, file
) != 1) {
116 stat(dir
.d_name
, &dd
);
117 } while(dd
.st_ino
!= d
.st_ino
|| dd
.st_dev
!= d
.st_dev
);
120 /* concatenate file name */
122 while (dir
.d_name
[++i
] != 0);
123 for(j
=off
+1; j
>0; --j
)
128 cwd
[i
+1] = dir
.d_name
[i
];
139 printf ("pwd = %s\n", curdir());