133965Sjdp/* Emulate getcwd using getwd. 233965Sjdp This function is in the public domain. */ 333965Sjdp 433965Sjdp/* 533965Sjdp 689857Sobrien@deftypefn Supplemental char* getcwd (char *@var{pathname}, int @var{len}) 733965Sjdp 889857SobrienCopy the absolute pathname for the current working directory into 989857Sobrien@var{pathname}, which is assumed to point to a buffer of at least 1089857Sobrien@var{len} bytes, and return a pointer to the buffer. If the current 1189857Sobriendirectory's path doesn't fit in @var{len} characters, the result is 1289857Sobrien@code{NULL} and @code{errno} is set. If @var{pathname} is a null pointer, 1389857Sobrien@code{getcwd} will obtain @var{len} bytes of space using 1489857Sobrien@code{malloc}. 1533965Sjdp 1689857Sobrien@end deftypefn 1760484Sobrien 1833965Sjdp*/ 1933965Sjdp 2060484Sobrien#include "config.h" 2160484Sobrien 2260484Sobrien#ifdef HAVE_SYS_PARAM_H 2333965Sjdp#include <sys/param.h> 2433965Sjdp#endif 2533965Sjdp#include <errno.h> 2677298Sobrien#ifdef HAVE_STRING_H 2777298Sobrien#include <string.h> 2877298Sobrien#endif 2977298Sobrien#ifdef HAVE_STDLIB_H 3077298Sobrien#include <stdlib.h> 3177298Sobrien#endif 3233965Sjdp 3333965Sjdpextern char *getwd (); 3433965Sjdpextern int errno; 3533965Sjdp 3633965Sjdp#ifndef MAXPATHLEN 3733965Sjdp#define MAXPATHLEN 1024 3833965Sjdp#endif 3933965Sjdp 4033965Sjdpchar * 41218822Sdimgetcwd (char *buf, size_t len) 4233965Sjdp{ 4333965Sjdp char ourbuf[MAXPATHLEN]; 4433965Sjdp char *result; 4533965Sjdp 4633965Sjdp result = getwd (ourbuf); 4733965Sjdp if (result) { 4833965Sjdp if (strlen (ourbuf) >= len) { 4933965Sjdp errno = ERANGE; 5033965Sjdp return 0; 5133965Sjdp } 5260484Sobrien if (!buf) { 5360484Sobrien buf = (char*)malloc(len); 5460484Sobrien if (!buf) { 5560484Sobrien errno = ENOMEM; 5660484Sobrien return 0; 5760484Sobrien } 5860484Sobrien } 5933965Sjdp strcpy (buf, ourbuf); 6033965Sjdp } 6133965Sjdp return buf; 6233965Sjdp} 63