summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2001-06-20 13:07:18 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2001-06-20 13:07:18 +0000
commit8f95b30d1b432a64a1797b0957af01437ea8f0d4 (patch)
tree8035060ff97328c780ce76ecd35e0d4ffb9eb6ca /util.c
parenta957f605ba75f16b2eb6f85e3cfffa325e8c9168 (diff)
downloadperl-8f95b30d1b432a64a1797b0957af01437ea8f0d4.tar.gz
No point in going into memory-saving contortions
with getcwd() since there's a danger of buffer overflow. Also make the POSIX extension to use sv_getcwd(). Finally, a missed proto.h fragment. p4raw-id: //depot/perl@10748
Diffstat (limited to 'util.c')
-rw-r--r--util.c46
1 files changed, 29 insertions, 17 deletions
diff --git a/util.c b/util.c
index 1d69fe5602..838c3620fb 100644
--- a/util.c
+++ b/util.c
@@ -3604,36 +3604,48 @@ Fill the sv with current working directory
* because you might chdir out of a directory that you can't chdir
* back into. */
-/* XXX: this needs more porting #ifndef HAS_GETCWD */
int
Perl_sv_getcwd(pTHX_ register SV *sv)
{
#ifndef PERL_MICRO
-#ifndef HAS_GETCWD
+#ifdef HAS_GETCWD
+ {
+ char* buf;
+
+ SvPOK_off(sv);
+ New(0, buf, MAXPATHLEN, char);
+ if (buf) {
+ buf[MAXPATHLEN] = 0;
+ /* Yes, some getcwd()s automatically allocate a buffer
+ * if given a NULL one. Portability is the problem.
+ * XXX Configure probe needed. */
+ if (getcwd(buf, MAXPATHLEN - 1)) {
+ STRLEN len = strlen(buf);
+ sv_setpvn(sv, buf, len);
+ SvPOK_only(sv);
+ SvCUR_set(sv, len);
+ }
+ else
+ sv_setsv(sv, &PL_sv_undef);
+ Safefree(buf);
+ }
+ else
+ sv_setsv(sv, &PL_sv_undef);
+
+ return SvPOK(sv) ? TRUE : FALSE;
+ }
+
+#else
+
struct stat statbuf;
int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
int namelen, pathlen=0;
DIR *dir;
Direntry_t *dp;
-#endif
(void)SvUPGRADE(sv, SVt_PV);
-#ifdef HAS_GETCWD
-
- SvGROW(sv, 128);
- while ((getcwd(SvPVX(sv), SvLEN(sv)-1) == NULL) && errno == ERANGE) {
- if (SvLEN(sv) + 128 >= MAXPATHLEN) {
- SV_CWD_RETURN_UNDEF;
- }
- SvGROW(sv, SvLEN(sv) + 128);
- }
- SvCUR_set(sv, strlen(SvPVX(sv)));
- SvPOK_only(sv);
-
-#else
-
if (PerlLIO_lstat(".", &statbuf) < 0) {
SV_CWD_RETURN_UNDEF;
}