| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

env_copy

#include "sflenv.h"
char **
env_copy (
    char **environment)

Synopsis

Returns an environment block which is a copy of the supplied environment block, in all new memory. If no environment block is supplied the current process environment is copied. The returned block is an array of strings, terminated by a null pointer. Each string is allocated independently using mem_alloc(). Returns NULL if there was not enough memory to allocate the block. No changes are made to the strings during copying. To free the array, use strtfree().

Source Code - (sflenv.c)

{
    MEMTRN
        *memtrn;                        /*  Memory transation                */
    char
        **env = environment,            /*  Environment to copy              */
        **newenv = NULL;                /*  Copy of environment              */
    int
        size  = 0,
        pos = 0;

    if (env == NULL)
        env = environ;        /*  Default is to copy the process environment */

    /*  Count the size of the environment                                    */
    for (size = 0; env [size] != NULL; env++)
        ;  /* EMPTY BODY */

    memtrn = mem new trans ();
    if (!memtrn)
        return NULL;

    newenv = memt_alloc (memtrn, ((size+1) * sizeof(char *)));
    if (!newenv)
      {
      	mem rollback (memtrn);
        return NULL;
      }

    for (pos = 0; pos < size; pos++)
      {
        newenv [pos] = memt_strdup (memtrn, env [pos]);
        if (newenv [pos] == NULL)
          {
            mem rollback (memtrn);
            return NULL;
          }
      }
    newenv [pos] = NULL;               /*  Terminate the array               */
    mem commit (memtrn);               /*  Commit the memory allocations     */

    return newenv;
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation