1bacd15cpeter/****************************************************************************
27dbeededelphij * Copyright (c) 1998-2012,2013 Free Software Foundation, Inc.              *
3bacd15cpeter *                                                                          *
4bacd15cpeter * Permission is hereby granted, free of charge, to any person obtaining a  *
5bacd15cpeter * copy of this software and associated documentation files (the            *
6bacd15cpeter * "Software"), to deal in the Software without restriction, including      *
7bacd15cpeter * without limitation the rights to use, copy, modify, merge, publish,      *
8bacd15cpeter * distribute, distribute with modifications, sublicense, and/or sell       *
9bacd15cpeter * copies of the Software, and to permit persons to whom the Software is    *
10bacd15cpeter * furnished to do so, subject to the following conditions:                 *
11bacd15cpeter *                                                                          *
12bacd15cpeter * The above copyright notice and this permission notice shall be included  *
13bacd15cpeter * in all copies or substantial portions of the Software.                   *
14bacd15cpeter *                                                                          *
15bacd15cpeter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16bacd15cpeter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17bacd15cpeter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18bacd15cpeter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19bacd15cpeter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20bacd15cpeter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21bacd15cpeter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22bacd15cpeter *                                                                          *
23bacd15cpeter * Except as contained in this notice, the name(s) of the above copyright   *
24bacd15cpeter * holders shall not be used in advertising or otherwise to promote the     *
25bacd15cpeter * sale, use or other dealings in this Software without prior written       *
26bacd15cpeter * authorization.                                                           *
27bacd15cpeter ****************************************************************************/
28bacd15cpeter
29bacd15cpeter/****************************************************************************
30bacd15cpeter *  Author: Thomas E. Dickey <dickey@clark.net> 1998                        *
31bacd15cpeter ****************************************************************************/
32bacd15cpeter
33bacd15cpeter/*
34bacd15cpeter *	getenv_num.c -- obtain a number from the environment
35bacd15cpeter */
36bacd15cpeter
37bacd15cpeter#include <curses.priv.h>
38bacd15cpeter
397dbeededelphijMODULE_ID("$Id: getenv_num.c,v 1.6 2013/09/28 20:25:08 tom Exp $")
40bacd15cpeter
41b7ada7fpeterNCURSES_EXPORT(int)
42bacd15cpeter_nc_getenv_num(const char *name)
43bacd15cpeter{
44b7ada7fpeter    char *dst = 0;
45b7ada7fpeter    char *src = getenv(name);
46b7ada7fpeter    long value;
47bacd15cpeter
48b7ada7fpeter    if ((src == 0)
49b7ada7fpeter	|| (value = strtol(src, &dst, 0)) < 0
50b7ada7fpeter	|| (dst == src)
51b7ada7fpeter	|| (*dst != '\0')
52b7ada7fpeter	|| (int) value < value)
53b7ada7fpeter	value = -1;
54bacd15cpeter
55b7ada7fpeter    return (int) value;
56bacd15cpeter}
577dbeededelphij
587dbeededelphijNCURSES_EXPORT(void)
597dbeededelphij_nc_setenv_num(const char *name, int value)
607dbeededelphij{
617dbeededelphij    if (name != 0 && value >= 0) {
627dbeededelphij	char buffer[128];
637dbeededelphij#if HAVE_SETENV
647dbeededelphij	_nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "%d", value);
657dbeededelphij	setenv(name, buffer, 1);
667dbeededelphij#elif HAVE_PUTENV
677dbeededelphij	char *s;
687dbeededelphij	_nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "%s=%d", name, value);
697dbeededelphij	if ((s = strdup(buffer)) != 0)
707dbeededelphij	    putenv(s);
717dbeededelphij#endif
727dbeededelphij    }
737dbeededelphij}
74