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