home *** CD-ROM | disk | FTP | other *** search
- /* e_getenv.c */
- /* 11Sep95 e */
-
- #include <stdlib.h>
- #include <Resources.h>
- #include "os_mac_str.h"
-
- /* the temporary env shouldn't be big (~0..10 entries?)
- so we'll use a linked list
- */
-
- typedef struct enventry
- {
- struct enventry *link;
- Str255 name;
- // the name is followed immediately by the c string value
- } enventry;
-
- static enventry *mem_env_root = NULL;
-
- static enventry *e_getenv_mem_guts(char *name)
- {
- enventry *ep = mem_env_root;
- Str255 pname;
- c_to_p( name, pname );
- while( ep != NULL )
- {
- if( compare_p_to_p( pname, ep->name ) == 0 )
- return ep;
- ep = ep->link;
- }
- return NULL;
- }
-
- static char *e_getenv_mem(char *name)
- {
- enventry *ep = e_getenv_mem_guts( name );
- if( ep != NULL )
- return (char *)&ep->name[ep->name[0]+1];
- return NULL;
- }
-
- static char *e_getenv_res(char *name)
- {
- Handle h;
- Str255 buf;
-
- c_to_p( name, buf );
- h = GetNamedResource( 'ENV ', buf );
- if( h == NULL ) return NULL;
- MoveHHi(h);
- HLock(h);
- return (char *)*h;
- }
-
- char *e_getenv(const char *name)
- {
- char *rslt = e_getenv_mem( (char *)name );
- if( rslt == NULL ) rslt = e_getenv_res( (char *)name );
- return( rslt );
- }
-
- int e_setenv(const char *name, const char *text, int rewrite)
- {
- enventry *ep = e_getenv_mem_guts( (char *)name );
- if( ep != NULL )
- {
- if( rewrite )
- return -1; // don't handle yet (doubly linked list?)
- else
- return 0;
- }
- else
- {
- int size = strlen(name) + strlen(text) + 6;
- ep = (enventry *)NewPtr(size);
- if( ep == NULL )
- return -1;
- ep->link = mem_env_root;
- mem_env_root = ep;
- c_to_p( (char *)name, ep->name );
- strcpy( (char *)&ep->name[ep->name[0]+1], (char *)text );
- }
- return 0;
- }
-
- /* end */
-