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