|
0
|
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 Cygnus eCos Public License |
|
|
12 // Version 1.0 (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://sourceware.cygnus.com/ecos |
|
|
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 Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //======================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour |
|
|
33 // Contributors: jlarmour@cygnus.co.uk |
|
|
34 // Date: 1998-08-31 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //======================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libc.h> // Configuration header |
|
|
46 |
|
|
47 // Include the C library? |
|
|
48 #if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_ENVIRONMENT) |
|
|
49 |
|
|
50 // INCLUDES |
|
|
51 |
|
|
52 #include <cyg/infra/cyg_type.h> // Common type definitions and support |
|
|
53 #include <cyg/infra/cyg_ass.h> // Tracing support |
|
|
54 #include <cyg/infra/cyg_trac.h> // Tracing support |
|
|
55 #include <stdlib.h> // Main header for stdlib functions |
|
|
56 #include <string.h> // strchr(), strlen() and strncmp() |
|
|
57 #include "clibincl/stdlibsupp.hxx" // Support for stdlib functions |
|
|
58 |
|
|
59 |
|
|
60 // VARIABLES |
|
|
61 |
|
|
62 externC char **environ; // standard definition of environ |
|
|
63 |
|
|
64 // EXPORTED SYMBOLS |
|
|
65 |
|
|
66 externC char * |
|
|
67 getenv( const char *name ) CYGPRI_LIBC_WEAK_ALIAS("_getenv"); |
|
|
68 |
|
|
69 |
|
|
70 // FUNCTIONS |
|
|
71 |
|
|
72 extern char * |
|
|
73 _getenv( const char *name ) |
|
|
74 { |
|
|
75 // This function assumes the POSIX 1003.1 layout of the environment |
|
|
76 // in the environ variable, i.e. extern char **environ is a pointer |
|
|
77 // to an array of character pointers to the environment strings |
|
|
78 // (POSIX 3.1.2.2 and 2.6). |
|
|
79 // |
|
|
80 // getenv() searches the environment list for a string of the form |
|
|
81 // "name=value" (POSIX 4.6.1). The strings must have this form |
|
|
82 // (POSIX 2.6) |
|
|
83 |
|
|
84 CYG_REPORT_FUNCNAMETYPE( "_getenv", "returning %08x" ); |
|
|
85 |
|
|
86 CYG_PRECONDITION( name != NULL, "getenv() called with NULL string!" ); |
|
|
87 |
|
|
88 CYG_REPORT_FUNCARG1( "name=%s", name ); |
|
|
89 |
|
|
90 CYG_PRECONDITION( environ != NULL, |
|
|
91 "environ not set i.e. environ == NULL" ); |
|
|
92 |
|
|
93 CYG_CHECK_DATA_PTR( environ, "environ isn't a valid pointer!" ); |
|
|
94 |
|
|
95 CYG_CHECK_DATA_PTR( name, "name isn't a valid pointer!" ); |
|
|
96 |
|
|
97 // check name doesn't contain '=' |
|
|
98 CYG_PRECONDITION( strchr(name, '=') == NULL, |
|
|
99 "name contains '='!" ); |
|
|
100 |
|
|
101 char **env_str_p; |
|
|
102 cyg_ucount32 len = strlen( name ); |
|
|
103 |
|
|
104 for (env_str_p = environ; *env_str_p != NULL; ++env_str_p ) { |
|
|
105 |
|
|
106 CYG_CHECK_DATA_PTR( env_str_p, |
|
|
107 "current environment string isn't valid!" ); |
|
|
108 |
|
|
109 CYG_CHECK_DATA_PTR( *env_str_p, |
|
|
110 "current environment string isn't valid!" ); |
|
|
111 |
|
|
112 // do we have a match? |
|
|
113 if ( !strncmp(*env_str_p, name, len) ) { |
|
|
114 // but it could be just a prefix i.e. we could have |
|
|
115 // matched MYNAMEFRED when we're looking for just MYNAME, so: |
|
|
116 |
|
|
117 if ( (*env_str_p)[len] == '=' ) { |
|
|
118 // we got a match |
|
|
119 CYG_REPORT_RETVAL( *env_str_p + len + 1 ); |
|
|
120 |
|
|
121 return (*env_str_p + len + 1); |
|
|
122 } // if |
|
|
123 } // if |
|
|
124 |
|
|
125 // check the next pointer isn't NULL, as we're about to dereference |
|
|
126 // it |
|
|
127 CYG_ASSERT( env_str_p+1 != NULL, |
|
|
128 "environment contains a NULL pointer!" ); |
|
|
129 } // for |
|
|
130 |
|
|
131 CYG_REPORT_RETVAL( NULL ); |
|
|
132 return NULL; |
|
|
133 } // getenv() |
|
|
134 |
|
|
135 |
|
|
136 #endif // if defined(CYGPKG_LIBC) && defined(CYGPKG_LIBC_ENVIRONMENT) |
|
|
137 |
|
|
138 // EOF getenv.cxx |