forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / gnu / abc-shell / environ.c
blob800cb65ab6dd837d1080b3f12914d4b27f2df442
1 #include "sh.h"
2 #include <exec/memory.h>
3 #include <dos/dos.h>
4 #include <dos/var.h>
5 #include <utility/hooks.h>
6 #include <proto/exec.h>
7 #include <proto/dos.h>
8 #include <proto/utility.h>
10 char **environ = NULL;
13 #if defined(__amigaos4__) || defined(__AROS__)
15 #define MAX_ENV_SIZE 1024 /* maximum number of environ entries */
17 #ifdef AUTOINIT
18 #ifdef __GNUC__
19 void ___makeenviron() __attribute__((constructor));
20 void ___freeenviron() __attribute__((destructor));
21 #endif
22 #ifdef __VBCC__
23 #define ___makeenviron() _INIT_9_makeenviron()
24 #define ___freeenviron() _EXIT_9_makeenviron()
25 #endif
26 #endif
29 uint32
30 copy_env(struct Hook *hook, APTR userdata, struct ScanVarsMsg *message)
32 static uint32 env_size = 1; /* environ is null terminated */
34 if(strlen(message->sv_GDir) <= 4)
36 if ( env_size == MAX_ENV_SIZE )
38 return 0;
41 char **env = (char **)hook->h_Data;
42 uint32 size = strlen(message->sv_Name) + 1 + message->sv_VarLen + 1 + 1;
43 char *buffer=(char *)malloc(size);
44 if ( buffer == NULL )
46 return 0;
49 ++env_size;
51 snprintf(buffer,size-1,"%s=%.*s", message->sv_Name, (int)message->sv_VarLen, message->sv_Var);
53 *env = buffer;
54 env++;
55 hook->h_Data = env;
58 return 0;
61 void
62 ___makeenviron()
64 char varbuf[8];
65 uint32 flags=0;
66 size_t environ_size=MAX_ENV_SIZE * sizeof(char*);
68 if(GetVar("ABCSH_IMPORT_LOCAL",varbuf,sizeof(varbuf),GVF_LOCAL_ONLY) > 0)
70 flags = GVF_LOCAL_ONLY;
72 else
74 flags = GVF_GLOBAL_ONLY;
77 environ = (char **)malloc(environ_size);
78 if (!environ)
80 return;
83 memset(environ, 0, environ_size);
85 struct Hook hook;
86 memset(&hook, 0, sizeof(struct Hook));
87 hook.h_Entry = copy_env;
88 hook.h_Data = environ;
90 ScanVars(&hook, flags, 0);
93 void
94 ___freeenviron()
96 if ( environ != NULL )
98 char **i;
99 for ( i = environ; *i != NULL; i++ )
101 free(*i);
102 *i = NULL;
105 free(environ);
106 environ = NULL;
109 #endif