summaryrefslogtreecommitdiff
path: root/getwd.c
blob: 4bf57d59ea42a223ee211f088c908dd1d86b8c42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *  CU sudo version 1.4
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Please send bugs, changes, problems to sudo-bugs@cs.colorado.edu
 *
 *******************************************************************
 *
 *  This module contains getwd(3) for those systems that lack it.
 *  getwd(3) returns a pointer to the current working dir.  It uses
 *  path as a copy-out parameter.
 *
 *  Todd C. Miller (millert@colorado.edu) Fri Jun  3 18:32:19 MDT 1994
 */

#ifndef lint
static char rcsid[] = "$Id$";
#endif /* lint */

#include "config.h"

#include <stdio.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif /* STDC_HEADERS */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <netinet/in.h>

#include <pathnames.h>
#include "compat.h"

#ifndef STDC_HEADERS
extern char *strcpy	__P((char *, const char *));
extern int   strlen	__P((const char *));
extern char *getwd	__P((char *));
extern FILE *popen	__P((const char *, const char *));
extern int   pclose	__P((FILE *));
extern char *fgets	__P((char *, int, FILE *));
#endif /* !STDC_HEADERS */


#ifndef _PATH_PWD
#define _PATH_PWD	"pwd"
#endif /* _PATH_PWD */


/*
 * Since we can't count on this being defined...
 */
extern int errno;


/******************************************************************
 *
 *  getwd()
 *
 *  getwd() returns a pointer to the current working dir.  It uses
 *  path as a copy-out parameter.
 *  getwd() will use getcwd() if available, else it will use pwd(1).
 */

char * getwd(path)
    char * path;				/* path to copy into */
{
#ifndef HAVE_GETCWD
    char buf[MAXPATHLEN+1];			/* temp buffer */
    FILE * pwd;					/* for popen */
#endif /* HAVE_GETCWD */

    if (path == NULL) {
	errno = EINVAL;
	return(NULL);
    }

#ifdef HAVE_GETCWD
    return(getcwd(path, MAXPATHLEN));
#else
    /*
     * open a pipe to pwd and read a line
     */
    if (!(pwd = popen(_PATH_PWD, "r")))
	return(NULL);

    if (!fgets(buf, sizeof(buf), pwd)) {
	errno = EACCES;				/* what an assumption... */
	pclose(pwd); 
	return(NULL);
    }
    pclose(pwd); 

    buf[strlen(buf)-1] = '\0';			/* remove newline */

    (void) strcpy(path, buf);
    return(path);
#endif /* HAVE_GETCWD */
}