xref: /titanic_41/usr/src/lib/libbc/libc/gen/common/getcwd.c (revision 82629e3015252bf18319ba3815c773df23e21436)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from S5R2 1.2 */
30 
31 /*LINTLIBRARY*/
32 /*
33  * Library routine to GET the Current Working Directory.
34  * arg1 is a pointer to a character buffer into which the
35  * path name of the current directory is placed by the
36  * subroutine.  arg1 may be zero, in which case the
37  * subroutine will call malloc to get the required space.
38  * arg2 is the length of the buffer space for the path-name.
39  * If the actual path-name is longer than (arg2-2), or if
40  * the value of arg2 is not at least 3, the subroutine will
41  * return a value of zero, with errno set as appropriate.
42  */
43 
44 #include <stdio.h>
45 #include <sys/errno.h>
46 
47 extern FILE *popen();
48 extern char *malloc(), *fgets(), *strchr();
49 extern int errno, pclose();
50 
51 char *
52 getcwd(arg1, arg2)
53 char	*arg1;
54 int	arg2;
55 {
56 	FILE	*pipe;
57 	char	*trm;
58 
59 	if(arg2 <= 0) {
60 		errno = EINVAL;
61 		return(0);
62 	}
63 	if(arg1 == 0)
64 		if((arg1 = malloc((unsigned)arg2)) == 0) {
65 			errno = ENOMEM;
66 			return(0);
67 		}
68 	errno = 0;
69 	if((pipe = popen("pwd", "r")) == 0)
70 		return(0);
71 	(void) fgets(arg1, arg2, pipe);
72 	(void) pclose(pipe);
73 	trm = strchr(arg1, '\0');
74 	if(*(trm-1) != '\n') {
75 		errno = ERANGE;
76 		return(0);
77 	}
78 	*(trm-1) = '\0';
79 	return(arg1);
80 }
81