blob: c02f66bb9320d9c3fd89fda1d562ac275dca22ac [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* vi: set sw=4 ts=4: */
2/* Copyright (C) 1992, 1997, 1998, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <features.h>
21
22#ifdef __USE_GNU
23#include <unistd.h>
24#include <sys/stat.h>
25#include <stdlib.h>
26#include <string.h>
27
28/* Return a malloc'd string containing the current directory name.
29 If the environment variable `PWD' is set, and its value is correct,
30 that value is used. */
31
32char *
33get_current_dir_name (void)
34{
35 char *pwd;
36#ifdef __UCLIBC_HAS_LFS__
37 struct stat64 dotstat, pwdstat;
38#else
39 struct stat dotstat, pwdstat;
40#endif
41
42 pwd = getenv ("PWD");
43 if (pwd != NULL
44#ifdef __UCLIBC_HAS_LFS__
45 && stat64 (".", &dotstat) == 0
46 && stat64 (pwd, &pwdstat) == 0
47#else
48 && stat (".", &dotstat) == 0
49 && stat (pwd, &pwdstat) == 0
50#endif
51 && pwdstat.st_dev == dotstat.st_dev
52 && pwdstat.st_ino == dotstat.st_ino)
53 /* The PWD value is correct. Use it. */
54 return strdup (pwd);
55
56 return getcwd ((char *) NULL, 0);
57}
58#endif