bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] addition: xsetenv.h, xsetenv.c


From: Bruno Haible
Subject: [Bug-gnulib] addition: xsetenv.h, xsetenv.c
Date: Thu, 23 Jan 2003 19:37:21 +0100 (CET)

Hi,

The xsetenv module provides an error-checking wrapper around setenv().

I learned this lesson the hard way: At first I used only setenv(),
without error checking. On one platform an unportable piece of
setenv.c caused the setenv() function to always fail, and since the
error wasn't detected, the testsuite failed in very strange ways.

This module is needed by the Java related modules (need to set the
CLASSPATH environment variable before running a Java program).

Any objections?

Bruno


================================== xsetenv.h ===========================
/* Setting environment variables, with out-of-memory checking.
   Copyright (C) 2001-2002 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* Get unsetenv().  It can be used without error checking.  */
#include "setenv.h"

#ifdef  __cplusplus
extern "C" {
#endif

/* Set NAME to VALUE in the environment.
   If REPLACE is nonzero, overwrite an existing value.
   With error checking.  */
extern void xsetenv (const char *name, const char *value, int replace);

#ifdef  __cplusplus
}
#endif
================================== xsetenv.c ===========================
/* Setting environment variables, with out-of-memory checking.
   Copyright (C) 2001-2002 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#if HAVE_CONFIG_H
# include <config.h>
#endif

/* Specification.  */
#include "xsetenv.h"

#include "setenv.h"
#include "error.h"
#include "exit.h"
#include "gettext.h"

#define _(str) gettext (str)


/* Set NAME to VALUE in the environment.
   If REPLACE is nonzero, overwrite an existing value.
   With error checking.  */
void
xsetenv (const char *name, const char *value, int replace)
{
  if (setenv (name, value, replace) < 0)
    error (EXIT_FAILURE, 0, _("memory exhausted"));
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]