1 |
27 |
unneback |
//========================================================================
|
2 |
|
|
//
|
3 |
|
|
// getenv.cxx
|
4 |
|
|
//
|
5 |
|
|
// Implementation of the ISO C standard getenv() function
|
6 |
|
|
//
|
7 |
|
|
//========================================================================
|
8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
16 |
|
|
//
|
17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
22 |
|
|
// You should have received a copy of the GNU General Public License along
|
23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
25 |
|
|
//
|
26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
28 |
|
|
// with other works to produce a work based on this file, this file does not
|
29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
30 |
|
|
// License. However the source code for this file must still be made available
|
31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
32 |
|
|
//
|
33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
34 |
|
|
// this file might be covered by the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
37 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
38 |
|
|
// -------------------------------------------
|
39 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
40 |
|
|
//========================================================================
|
41 |
|
|
//#####DESCRIPTIONBEGIN####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): jlarmour
|
44 |
|
|
// Contributors:
|
45 |
|
|
// Date: 2000-04-30
|
46 |
|
|
// Purpose:
|
47 |
|
|
// Description:
|
48 |
|
|
// Usage:
|
49 |
|
|
//
|
50 |
|
|
//####DESCRIPTIONEND####
|
51 |
|
|
//
|
52 |
|
|
//========================================================================
|
53 |
|
|
|
54 |
|
|
// CONFIGURATION
|
55 |
|
|
|
56 |
|
|
#include <pkgconf/libc_stdlib.h> // Configuration header
|
57 |
|
|
|
58 |
|
|
// INCLUDES
|
59 |
|
|
|
60 |
|
|
#include <cyg/infra/cyg_type.h> // Common type definitions and support
|
61 |
|
|
#include <cyg/infra/cyg_ass.h> // Tracing support
|
62 |
|
|
#include <cyg/infra/cyg_trac.h> // Tracing support
|
63 |
|
|
#include <stdlib.h> // Main header for stdlib functions
|
64 |
|
|
#include <string.h> // strchr(), strlen() and strncmp()
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
// VARIABLES
|
68 |
|
|
|
69 |
|
|
externC char **environ; // standard definition of environ
|
70 |
|
|
|
71 |
|
|
// FUNCTIONS
|
72 |
|
|
|
73 |
|
|
extern char *
|
74 |
|
|
getenv( const char *name )
|
75 |
|
|
{
|
76 |
|
|
// This function assumes the POSIX 1003.1 layout of the environment
|
77 |
|
|
// in the environ variable, i.e. extern char **environ is a pointer
|
78 |
|
|
// to an array of character pointers to the environment strings
|
79 |
|
|
// (POSIX 3.1.2.2 and 2.6).
|
80 |
|
|
//
|
81 |
|
|
// getenv() searches the environment list for a string of the form
|
82 |
|
|
// "name=value" (POSIX 4.6.1). The strings must have this form
|
83 |
|
|
// (POSIX 2.6)
|
84 |
|
|
|
85 |
|
|
CYG_REPORT_FUNCNAMETYPE( "getenv", "returning %08x" );
|
86 |
|
|
|
87 |
|
|
CYG_PRECONDITION( name != NULL, "getenv() called with NULL string!" );
|
88 |
|
|
|
89 |
|
|
CYG_REPORT_FUNCARG1( "name=%s", name );
|
90 |
|
|
|
91 |
|
|
CYG_PRECONDITION( environ != NULL,
|
92 |
|
|
"environ not set i.e. environ == NULL" );
|
93 |
|
|
|
94 |
|
|
CYG_CHECK_DATA_PTR( environ, "environ isn't a valid pointer!" );
|
95 |
|
|
|
96 |
|
|
CYG_CHECK_DATA_PTR( name, "name isn't a valid pointer!" );
|
97 |
|
|
|
98 |
|
|
// check name doesn't contain '='
|
99 |
|
|
CYG_PRECONDITION( strchr(name, '=') == NULL,
|
100 |
|
|
"name contains '='!" );
|
101 |
|
|
|
102 |
|
|
char **env_str_p;
|
103 |
|
|
cyg_ucount32 len = strlen( name );
|
104 |
|
|
|
105 |
|
|
for (env_str_p = environ; *env_str_p != NULL; ++env_str_p ) {
|
106 |
|
|
|
107 |
|
|
CYG_CHECK_DATA_PTR( env_str_p,
|
108 |
|
|
"current environment string isn't valid!" );
|
109 |
|
|
|
110 |
|
|
CYG_CHECK_DATA_PTR( *env_str_p,
|
111 |
|
|
"current environment string isn't valid!" );
|
112 |
|
|
|
113 |
|
|
// do we have a match?
|
114 |
|
|
if ( !strncmp(*env_str_p, name, len) ) {
|
115 |
|
|
// but it could be just a prefix i.e. we could have
|
116 |
|
|
// matched MYNAMEFRED when we're looking for just MYNAME, so:
|
117 |
|
|
|
118 |
|
|
if ( (*env_str_p)[len] == '=' ) {
|
119 |
|
|
// we got a match
|
120 |
|
|
CYG_REPORT_RETVAL( *env_str_p + len + 1 );
|
121 |
|
|
|
122 |
|
|
return (*env_str_p + len + 1);
|
123 |
|
|
} // if
|
124 |
|
|
} // if
|
125 |
|
|
|
126 |
|
|
// check the next pointer isn't NULL, as we're about to dereference
|
127 |
|
|
// it
|
128 |
|
|
CYG_ASSERT( env_str_p+1 != NULL,
|
129 |
|
|
"environment contains a NULL pointer!" );
|
130 |
|
|
} // for
|
131 |
|
|
|
132 |
|
|
CYG_REPORT_RETVAL( NULL );
|
133 |
|
|
return NULL;
|
134 |
|
|
} // getenv()
|
135 |
|
|
|
136 |
|
|
// EOF getenv.cxx
|