xref: /plan9/sys/src/cmd/gs/src/gp_getnv.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1997 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gp_getnv.c,v 1.5 2002/06/16 05:48:55 lpd Exp $ */
18 /* Standard implementation of gp_getenv */
19 #include "stdio_.h"
20 #include "string_.h"
21 #include "gsmemory.h"
22 #include "gstypes.h"
23 #include "gp.h"
24 
25 /* Import the C getenv function. */
26 extern char *getenv(const char *);
27 
28 /* Get the value of an environment variable.  See gp.h for details. */
29 int
gp_getenv(const char * key,char * ptr,int * plen)30 gp_getenv(const char *key, char *ptr, int *plen)
31 {
32     const char *str = getenv(key);
33 
34     if (str) {
35 	int len = strlen(str);
36 
37 	if (len < *plen) {
38 	    /* string fits */
39 	    strcpy(ptr, str);
40 	    *plen = len + 1;
41 	    return 0;
42 	}
43 	/* string doesn't fit */
44 	*plen = len + 1;
45 	return -1;
46     }
47     /* missing key */
48     if (*plen > 0)
49 	*ptr = 0;
50     *plen = 1;
51     return 1;
52 }
53