comparison packages/language/c/libc/stdlib/current/src/getenv.cxx @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children e0c0827131d1
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //========================================================================
2 //
3 // getenv.cxx
4 //
5 // Implementation of the ISO C standard getenv() function
6 //
7 //========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): jlarmour
35 // Contributors:
36 // Date: 2000-04-30
37 // Purpose:
38 // Description:
39 // Usage:
40 //
41 //####DESCRIPTIONEND####
42 //
43 //========================================================================
44
45 // CONFIGURATION
46
47 #include <pkgconf/libc_stdlib.h> // Configuration header
48
49 // INCLUDES
50
51 #include <cyg/infra/cyg_type.h> // Common type definitions and support
52 #include <cyg/infra/cyg_ass.h> // Tracing support
53 #include <cyg/infra/cyg_trac.h> // Tracing support
54 #include <stdlib.h> // Main header for stdlib functions
55 #include <string.h> // strchr(), strlen() and strncmp()
56
57
58 // VARIABLES
59
60 externC char **environ; // standard definition of environ
61
62 // FUNCTIONS
63
64 extern char *
65 getenv( const char *name )
66 {
67 // This function assumes the POSIX 1003.1 layout of the environment
68 // in the environ variable, i.e. extern char **environ is a pointer
69 // to an array of character pointers to the environment strings
70 // (POSIX 3.1.2.2 and 2.6).
71 //
72 // getenv() searches the environment list for a string of the form
73 // "name=value" (POSIX 4.6.1). The strings must have this form
74 // (POSIX 2.6)
75
76 CYG_REPORT_FUNCNAMETYPE( "getenv", "returning %08x" );
77
78 CYG_PRECONDITION( name != NULL, "getenv() called with NULL string!" );
79
80 CYG_REPORT_FUNCARG1( "name=%s", name );
81
82 CYG_PRECONDITION( environ != NULL,
83 "environ not set i.e. environ == NULL" );
84
85 CYG_CHECK_DATA_PTR( environ, "environ isn't a valid pointer!" );
86
87 CYG_CHECK_DATA_PTR( name, "name isn't a valid pointer!" );
88
89 // check name doesn't contain '='
90 CYG_PRECONDITION( strchr(name, '=') == NULL,
91 "name contains '='!" );
92
93 char **env_str_p;
94 cyg_ucount32 len = strlen( name );
95
96 for (env_str_p = environ; *env_str_p != NULL; ++env_str_p ) {
97
98 CYG_CHECK_DATA_PTR( env_str_p,
99 "current environment string isn't valid!" );
100
101 CYG_CHECK_DATA_PTR( *env_str_p,
102 "current environment string isn't valid!" );
103
104 // do we have a match?
105 if ( !strncmp(*env_str_p, name, len) ) {
106 // but it could be just a prefix i.e. we could have
107 // matched MYNAMEFRED when we're looking for just MYNAME, so:
108
109 if ( (*env_str_p)[len] == '=' ) {
110 // we got a match
111 CYG_REPORT_RETVAL( *env_str_p + len + 1 );
112
113 return (*env_str_p + len + 1);
114 } // if
115 } // if
116
117 // check the next pointer isn't NULL, as we're about to dereference
118 // it
119 CYG_ASSERT( env_str_p+1 != NULL,
120 "environment contains a NULL pointer!" );
121 } // for
122
123 CYG_REPORT_RETVAL( NULL );
124 return NULL;
125 } // getenv()
126
127 // EOF getenv.cxx