From: Todd C. Miller Date: Mon, 6 Jun 1994 00:03:49 +0000 (+0000) Subject: Initial revision X-Git-Tag: SUDO_1_3_1~183 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1460112ba6baae3165eee3cbe7032d14cc0a7b60;p=sudo Initial revision --- diff --git a/getcwd.c b/getcwd.c new file mode 100644 index 000000000..c329f958b --- /dev/null +++ b/getcwd.c @@ -0,0 +1,126 @@ +/* + * CU sudo version 1.3.1 + * + * 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 getcwd(3) for those systems that lack it. + * getcwd(3) returns a pointer to the current working dir. It uses + * path as a copy-out parameter and malloc(3)s space if path is NULL. + * + * 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 "pathnames.h" + +#include +#ifdef STDC_HEADERS +#include +#endif /* STDC_HEADERS */ +#ifdef HAVE_UNISTD_H +#include +#endif /* HAVE_UNISTD_H */ +#ifdef HAVE_STRING_H +#include +#endif /* HAVE_STRING_H */ +#ifdef HAVE_STRINGS_H +#include +#endif /* HAVE_STRINGS_H */ +#ifdef HAVE_MALLOC_H +#include +#endif /* HAVE_MALLOC_H */ +#include +#include +#include +#include + +#ifndef STDC_HEADERS +extern char *strcpy(); +extern int readlink(); +extern int lstat(); +#endif /* !STDC_HEADERS */ + + +#ifndef _PATH_PWD +#define _PATH_PWD "pwd" +#endif /* _PATH_PWD */ + + +/****************************************************************** + * + * getcwd() + * + * getcwd() returns a pointer to the current working dir. It uses + * path as a copy-out parameter and malloc(3)s space if path is NULL. + * getcwd() will use getwd() if available, else it will use pwd(1). + */ + +char * getcwd(path, len) + char * path; /* path to copy into */ + size_t len; /* length of path */ +{ + char buf[MAXPATHLEN+1]; /* temp buffer */ +#ifndef HAVE_GETWD + FILE * pwd; /* for popen */ +#endif /* HAVE_GETWD */ + + if (path && len <= 0) { + errno = EINVAL; + return(NULL); + } + +#ifdef HAVE_GETWD + if (!getwd(buf)) + return(NULL); +#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 */ +#endif /* HAVE_GETWD */ + + if (len < strlen(buf) + 1) { + errno = ERANGE; + return(NULL); + } + + if (path == NULL) { + if (!(path = (char *) malloc(MAXPATHLEN))) { + errno = ENOMEM; + return(NULL); + } + } + + (void) strcpy(path, buf); + return(path); +} diff --git a/getwd.c b/getwd.c new file mode 100644 index 000000000..c329f958b --- /dev/null +++ b/getwd.c @@ -0,0 +1,126 @@ +/* + * CU sudo version 1.3.1 + * + * 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 getcwd(3) for those systems that lack it. + * getcwd(3) returns a pointer to the current working dir. It uses + * path as a copy-out parameter and malloc(3)s space if path is NULL. + * + * 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 "pathnames.h" + +#include +#ifdef STDC_HEADERS +#include +#endif /* STDC_HEADERS */ +#ifdef HAVE_UNISTD_H +#include +#endif /* HAVE_UNISTD_H */ +#ifdef HAVE_STRING_H +#include +#endif /* HAVE_STRING_H */ +#ifdef HAVE_STRINGS_H +#include +#endif /* HAVE_STRINGS_H */ +#ifdef HAVE_MALLOC_H +#include +#endif /* HAVE_MALLOC_H */ +#include +#include +#include +#include + +#ifndef STDC_HEADERS +extern char *strcpy(); +extern int readlink(); +extern int lstat(); +#endif /* !STDC_HEADERS */ + + +#ifndef _PATH_PWD +#define _PATH_PWD "pwd" +#endif /* _PATH_PWD */ + + +/****************************************************************** + * + * getcwd() + * + * getcwd() returns a pointer to the current working dir. It uses + * path as a copy-out parameter and malloc(3)s space if path is NULL. + * getcwd() will use getwd() if available, else it will use pwd(1). + */ + +char * getcwd(path, len) + char * path; /* path to copy into */ + size_t len; /* length of path */ +{ + char buf[MAXPATHLEN+1]; /* temp buffer */ +#ifndef HAVE_GETWD + FILE * pwd; /* for popen */ +#endif /* HAVE_GETWD */ + + if (path && len <= 0) { + errno = EINVAL; + return(NULL); + } + +#ifdef HAVE_GETWD + if (!getwd(buf)) + return(NULL); +#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 */ +#endif /* HAVE_GETWD */ + + if (len < strlen(buf) + 1) { + errno = ERANGE; + return(NULL); + } + + if (path == NULL) { + if (!(path = (char *) malloc(MAXPATHLEN))) { + errno = ENOMEM; + return(NULL); + } + } + + (void) strcpy(path, buf); + return(path); +}