| version 1.3.4.1, 1995/05/02 19:34:46 |
version 1.4, 1995/02/27 04:12:20 |
|
|
| |
/* $NetBSD$ */ |
| |
|
| /* |
/* |
| * Copyright (c) 1989, 1991 The Regents of the University of California. |
* Copyright (c) 1989, 1991, 1993 |
| * All rights reserved. |
* The Regents of the University of California. All rights reserved. |
| * |
* |
| * Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions |
* modification, are permitted provided that the following conditions |
|
|
| */ |
*/ |
| |
|
| #if defined(LIBC_SCCS) && !defined(lint) |
#if defined(LIBC_SCCS) && !defined(lint) |
| /*static char *sccsid = "from: @(#)getcwd.c 5.11 (Berkeley) 2/24/91";*/ |
#if 0 |
| static char *rcsid = "$Id$"; |
static char sccsid[] = "@(#)getcwd.c 8.1 (Berkeley) 6/4/93"; |
| |
#else |
| |
static char rcsid[] = "$NetBSD$"; |
| |
#endif |
| #endif /* LIBC_SCCS and not lint */ |
#endif /* LIBC_SCCS and not lint */ |
| |
|
| #include "namespace.h" |
|
| #include <sys/param.h> |
#include <sys/param.h> |
| #include <sys/stat.h> |
#include <sys/stat.h> |
| |
|
| #include <errno.h> |
#include <errno.h> |
| #include <dirent.h> |
#include <dirent.h> |
| #include <stdio.h> |
#include <stdio.h> |
|
|
| ptsize = 0; |
ptsize = 0; |
| if (!size) { |
if (!size) { |
| errno = EINVAL; |
errno = EINVAL; |
| return((char *)NULL); |
return (NULL); |
| } |
} |
| ept = pt + size; |
ept = pt + size; |
| } else { |
} else { |
| if (!(pt = (char *)malloc(ptsize = 1024 - 4))) |
if ((pt = malloc(ptsize = 1024 - 4)) == NULL) |
| return((char *)NULL); |
return (NULL); |
| ept = pt + ptsize; |
ept = pt + ptsize; |
| } |
} |
| bpt = ept - 1; |
bpt = ept - 1; |
|
|
| * Should always be enough (it's 340 levels). If it's not, allocate |
* Should always be enough (it's 340 levels). If it's not, allocate |
| * as necessary. Special * case the first stat, it's ".", not "..". |
* as necessary. Special * case the first stat, it's ".", not "..". |
| */ |
*/ |
| if (!(up = (char *)malloc(upsize = 1024 - 4))) |
if ((up = malloc(upsize = 1024 - 4)) == NULL) |
| goto err; |
goto err; |
| eup = up + MAXPATHLEN; |
eup = up + MAXPATHLEN; |
| bup = up; |
bup = up; |
| Line 127 getcwd(pt, size) |
|
| Line 132 getcwd(pt, size) |
|
| */ |
*/ |
| (void)bcopy(bpt, pt, ept - bpt); |
(void)bcopy(bpt, pt, ept - bpt); |
| free(up); |
free(up); |
| return(pt); |
return (pt); |
| } |
} |
| |
|
| /* |
/* |
| Line 136 getcwd(pt, size) |
|
| Line 141 getcwd(pt, size) |
|
| * possible component name, plus a trailing NULL. |
* possible component name, plus a trailing NULL. |
| */ |
*/ |
| if (bup + 3 + MAXNAMLEN + 1 >= eup) { |
if (bup + 3 + MAXNAMLEN + 1 >= eup) { |
| if (!(up = (char *)realloc(up, upsize *= 2))) |
if ((up = realloc(up, upsize *= 2)) == NULL) |
| goto err; |
goto err; |
| |
bup = up; |
| eup = up + upsize; |
eup = up + upsize; |
| } |
} |
| *bup++ = '.'; |
*bup++ = '.'; |
| Line 196 getcwd(pt, size) |
|
| Line 202 getcwd(pt, size) |
|
| } |
} |
| off = bpt - pt; |
off = bpt - pt; |
| len = ept - bpt; |
len = ept - bpt; |
| if (!(pt = (char *)realloc(pt, ptsize *= 2))) |
if ((pt = realloc(pt, ptsize *= 2)) == NULL) |
| goto err; |
goto err; |
| bpt = pt + off; |
bpt = pt + off; |
| ept = pt + ptsize; |
ept = pt + ptsize; |
|
|
| if (ptsize) |
if (ptsize) |
| free(pt); |
free(pt); |
| free(up); |
free(up); |
| return((char *)NULL); |
return (NULL); |
| } |
} |