root/libutil/env.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. set_env
  2. get_home_directory
  3. env_size

   1 /*
   2  * Copyright (c) 2003, 2005 Tama Communications Corporation
   3  *
   4  * This file is part of GNU GLOBAL.
   5  *
   6  * This program is free software: you can redistribute it and/or modify
   7  * it under the terms of the GNU General Public License as published by
   8  * the Free Software Foundation, either version 3 of the License, or
   9  * (at your option) any later version.
  10  * 
  11  * This program is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU General Public License for more details.
  15  * 
  16  * You should have received a copy of the GNU General Public License
  17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18  */
  19 
  20 #ifdef HAVE_CONFIG_H
  21 #include <config.h>
  22 #endif
  23 #ifdef STDC_HEADERS
  24 #include <stdlib.h>
  25 #endif
  26 #ifdef HAVE_STRING_H
  27 #include <string.h>
  28 #else
  29 #include <strings.h>
  30 #endif
  31 #ifdef HAVE_HOME_ETC_H
  32 #include <home_etc.h>
  33 #endif
  34 
  35 #include "die.h"
  36 #include "env.h"
  37 #include "strbuf.h"
  38 
  39 extern char **environ;
  40 
  41 /**
  42  * set_env: put environment variable.
  43  *
  44  *      @param[in]      var     environment variable
  45  *      @param[in]      val     value
  46  *
  47  * Machine independent version of @XREF{setenv,3}.
  48  */
  49 void
  50 set_env(const char *var, const char *val)
  51 {
  52 /*
  53  * sparc-sun-solaris2.6 doesn't have setenv(3).
  54  */
  55 #ifdef HAVE_PUTENV
  56         STRBUF *sb = strbuf_open(0);
  57 
  58         strbuf_sprintf(sb, "%s=%s", var, val);
  59         putenv(strbuf_value(sb));
  60         /* Don't free memory. putenv(3) require it. */
  61 #else
  62         setenv(var, val, 1);
  63 #endif
  64 }
  65 /**
  66  * get_home_directory: get environment dependent home directory.
  67  *
  68  *      @return home directory
  69  */
  70 char *
  71 get_home_directory(void)
  72 {
  73 #ifdef HAVE_HOME_ETC_H
  74         return _HEdir;
  75 #else
  76         return getenv("HOME");
  77 #endif
  78 }
  79 
  80 /**
  81  * env_size: calculate the size of area used by environment.
  82  */
  83 int
  84 env_size(void)
  85 {
  86         char **e;
  87         int size = 0;
  88 
  89         for (e = environ; *e != NULL; e++)
  90                 size += strlen(*e) + 1;
  91 
  92         return size;
  93 }

/* [previous][next][first][last][top][bottom][index][help] */