1 /* pwd - print current directory
2 Copyright (C) 1994-1997, 1999-2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include <sys/types.h>
25 #include "long-options.h"
27 #include "root-dev-ino.h"
30 /* The official name of this program (e.g., no `g' prefix). */
31 #define PROGRAM_NAME "pwd"
33 #define AUTHORS "Jim Meyering"
44 NOT_AN_INODE_NUMBER = 0
47 #ifdef D_INO_IN_DIRENT
48 # define D_INO(dp) ((dp)->d_ino)
50 /* Some systems don't have inodes, so fake them to avoid lots of ifdefs. */
51 # define D_INO(dp) NOT_AN_INODE_NUMBER
54 /* The name this program was run with. */
60 if (status != EXIT_SUCCESS)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
65 printf (_("Usage: %s [OPTION]\n"), program_name);
67 Print the full filename of the current working directory.\n\
70 fputs (HELP_OPTION_DESCRIPTION, stdout);
71 fputs (VERSION_OPTION_DESCRIPTION, stdout);
72 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
78 path_free (struct Path *p)
87 struct Path *p = xmalloc (sizeof *p);
89 /* Start with a buffer larger than PATH_MAX, but beware of systems
90 on which PATH_MAX is very large -- e.g., INT_MAX. */
91 p->n_alloc = MIN (2 * PATH_MAX, 32 * 1024);
93 p->buf = xmalloc (p->n_alloc);
94 p->start = p->buf + (p->n_alloc - 1);
99 /* Prepend the name S of length S_LEN, to the growing path, P. */
101 path_prepend (struct Path *p, char const *s, size_t s_len)
103 size_t n_free = p->start - p->buf;
104 if (n_free < 1 + s_len)
106 size_t half = p->n_alloc + 1 + s_len;
107 /* Use xnmalloc+free rather than xnrealloc, since with the latter
108 we'd end up copying the data twice: once via realloc, then again
109 to align it with the end of the new buffer. With xnmalloc, we
110 copy it only once. */
111 char *q = xnmalloc (2, half);
112 size_t n_used = p->n_alloc - n_free;
113 p->start = q + 2 * half - n_used;
114 memcpy (p->start, p->buf + n_free, n_used);
117 p->n_alloc = 2 * half;
120 p->start -= 1 + s_len;
122 memcpy (p->start + 1, s, s_len);
125 /* Return a string (malloc'd) consisting of N `/'-separated ".." components. */
127 nth_parent (size_t n)
129 char *buf = xnmalloc (3, n);
133 for (i = 0; i < n; i++)
135 memcpy (p, "../", 3);
142 /* Return the basename of the current directory, where DOT_SB is the
143 result of lstat'ing ".".
144 Find the directory entry in `..' that matches the dev/i-node of DOT_SB.
145 Upon success, update *DOT_SB with stat information of `..', chdir to `..',
146 and prepend "/basename" to PATH.
147 Otherwise, exit with a diagnostic.
148 PARENT_HEIGHT is the number of levels `..' is above the starting directory.
149 The first time this function is called (from the initial directory),
150 PARENT_HEIGHT is 1. This is solely for diagnostics. */
153 find_dir_entry (struct stat *dot_sb, struct Path *path, size_t parent_height)
157 struct stat parent_sb;
161 dirp = opendir ("..");
163 error (EXIT_FAILURE, errno, _("cannot open directory %s"),
164 quote (nth_parent (parent_height)));
167 if ((0 <= fd ? fchdir (fd) : chdir ("..")) < 0)
168 error (EXIT_FAILURE, errno, _("failed to chdir to %s"),
169 quote (nth_parent (parent_height)));
171 if ((0 <= fd ? fstat (fd, &parent_sb) : stat (".", &parent_sb)) < 0)
172 error (EXIT_FAILURE, errno, _("failed to stat %s"),
173 quote (nth_parent (parent_height)));
175 /* If parent and child directory are on different devices, then we
176 can't rely on d_ino for useful i-node numbers; use lstat instead. */
177 use_lstat = (parent_sb.st_dev != dot_sb->st_dev);
182 struct dirent const *dp;
188 if ((dp = readdir_ignoring_dot_and_dotdot (dirp)) == NULL)
192 /* Save/restore errno across closedir call. */
197 /* Arrange to give a diagnostic after exiting this loop. */
205 ent_sb_valid = false;
206 if (ino == NOT_AN_INODE_NUMBER || use_lstat)
208 if (lstat (dp->d_name, &ent_sb) < 0)
210 /* Skip any entry we can't stat. */
217 if (ino != dot_sb->st_ino)
220 /* If we're not crossing a device boundary, then a simple i-node
222 if ( ! use_lstat || ent_sb.st_dev == dot_sb->st_dev)
224 path_prepend (path, dp->d_name, NLENGTH (dp));
230 if (dirp == NULL || CLOSEDIR (dirp) != 0)
232 /* Note that this diagnostic serves for both readdir
233 and closedir failures. */
234 error (EXIT_FAILURE, errno, _("reading directory %s"),
235 quote (nth_parent (parent_height)));
239 error (EXIT_FAILURE, 0,
240 _("couldn't find directory entry in %s with matching i-node"),
241 quote (nth_parent (parent_height)));
246 /* Print the full, absolute name of the current working directory.
247 The getcwd function does nearly the same task, but is typically
248 unable to handle names longer than PATH_MAX. This function has
249 no such limitation. However, this function *can* fail due to
250 permission problems or a lack of memory, while Linux's getcwd
251 function works regardless of restricted permissions on parent
252 directories. Upon failure, give a diagnostic and exit nonzero.
254 Note: although this function is similar to getcwd, it has a fundamental
255 difference in that it gives a diagnostic and exits upon failure.
256 I would have liked a function that did not exit, and that could be
257 used as a getcwd replacement. Unfortunately, considering all of
258 the information the caller would require in order to produce good
259 diagnostics, it doesn't seem worth the added complexity.
261 FIXME-maybe: if find_dir_entry fails due to permissions, try getcwd,
262 in case the unreadable directory is close enough to the root that
263 getcwd works from there. */
266 robust_getcwd (struct Path *path)
269 struct dev_ino dev_ino_buf;
270 struct dev_ino *root_dev_ino = get_root_dev_ino (&dev_ino_buf);
273 if (root_dev_ino == NULL)
274 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
277 if (stat (".", &dot_sb) < 0)
278 error (EXIT_FAILURE, errno, _("failed to stat %s"), quote ("."));
282 /* If we've reached the root, we're done. */
283 if (SAME_INODE (dot_sb, *root_dev_ino))
286 find_dir_entry (&dot_sb, path, height++);
289 if (path->start[0] == '\0')
290 path_prepend (path, "/", 1);
294 main (int argc, char **argv)
298 initialize_main (&argc, &argv);
299 program_name = argv[0];
300 setlocale (LC_ALL, "");
301 bindtextdomain (PACKAGE, LOCALEDIR);
302 textdomain (PACKAGE);
304 atexit (close_stdout);
306 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
307 usage, AUTHORS, (char const *) NULL);
310 error (0, 0, _("ignoring non-option arguments"));
320 struct Path *path = path_init ();
321 robust_getcwd (path);