(safe_getcwd): New. When getcwd() is known to be able to allocate its

buffer, just call that.  Else wrap suitable allocation around it.
(main): Use it.
This commit is contained in:
Markus Armbruster 2004-03-01 07:07:27 +00:00
parent 5ce301e2fb
commit ef0ddff4c2

View file

@ -32,6 +32,7 @@
* Steve McClure, 1996-2000
*/
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
@ -56,6 +57,28 @@ char *_ipglob_copyright_header =
"/*\n * Empire - A multi-player, client/server Internet based war game.\n * Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,\n * Ken Stevens, Steve McClure\n *\n * This program is free software; you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation; either version 2 of the License, or\n * (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU General Public License for more details.\n *\n * You should have received a copy of the GNU General Public License\n * along with this program; if not, write to the Free Software\n * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n *\n * ---\n *\n * See the \"LEGAL\", \"LICENSE\", \"CREDITS\" and \"README\" files for all the\n * related information and legal notices. It is expected that any future\n * projects/authors will amend these files as needed.\n *\n * ---\n *\n * ipglob.c: This is an auto-generated file.\n * \n * Known contributors to this file:\n * Automatically generated by doconfig.c\n */\n\n";
#if defined(__GLIBC__) || defined(FBSD) || defined(__APPLE_) || defined(_WIN32)
#define safe_getcwd() getcwd(NULL, 0)
#else
static char *
safe_getcwd(void)
{
size_t size = 256;
for (;;) {
char *buf = malloc(size);
if (!buf)
return buf;
if (getcwd (buf, size))
return buf;
free (buf);
if (errno != ERANGE)
return NULL;
size *= 2;
}
}
#endif
int
main(void)
{
@ -63,7 +86,7 @@ main(void)
char *cp;
char *pathname;
if ((pathname = getcwd(NULL, 255)) == NULL) {
if ((pathname = safe_getcwd()) == NULL) {
printf("Can't get current path!\n");
exit(-1);
}