ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dce-env.cc
Go to the documentation of this file.
1 #include "dce-stdlib.h"
2 #include "utils.h"
3 #include "process.h"
4 #include "ns3/log.h"
5 #include "ns3/assert.h"
6 #include <string.h>
7 
8 NS_LOG_COMPONENT_DEFINE ("SimuEnv");
9 
10 using namespace ns3;
11 
12 char * dce_getenv (const char *name)
13 {
14  NS_LOG_FUNCTION (Current () << UtilsGetNodeId () << name);
15  NS_ASSERT (Current () != 0);
16  struct Thread *current = Current ();
17  char ***penvp = current->process->penvp;
18  if (!penvp)
19  {
20  return NULL;
21  }
22  return seek_env (name, *penvp);
23 }
24 char * dce__secure_getenv (const char *name)
25 {
26  // XXX
27  return dce_getenv (name);
28 }
29 
30 int dce_putenv (char *string)
31 {
32  NS_LOG_FUNCTION (Current () << UtilsGetNodeId () << string);
33  NS_ASSERT (Current () != 0);
34  struct Thread *current = Current ();
35  char ***penvp = current->process->penvp;
36  // first, locate '=' sign.
37  char *name = strchr (string, '=');
38  if (name == 0)
39  {
40  // no '=' so, this is a request to clear the corresponding
41  // entry in the env array
42  return dce_unsetenv (string);
43  }
44  size_t namelen = ((unsigned long)name) - ((unsigned long)string) + 1;
45  // try to find requested name in env array
46  char **cur;
47  int len = 0;
48  bool found = false;
49  for (cur = *penvp; *cur != 0; cur++)
50  {
51  if (strncmp (string, *cur, namelen) == 0)
52  {
53  // found it, and replace.
54  *cur = string;
55  found = true;
56  }
57  len++;
58  }
59  if (found)
60  {
61  return 0;
62  }
63  // not found: we are going to add it now.
64  char **newEnvp = (char**)malloc (sizeof(char*) * (len + 1 + 1));
65  current->process->allocated.push_back (newEnvp);
66  memcpy (newEnvp, *penvp, sizeof(char*) * (len));
67  newEnvp[len] = string;
68  newEnvp[len + 1] = 0;
69  *penvp = newEnvp;
70  return 0;
71 }
72 int dce_setenv (const char *name, const char *value, int overwrite)
73 {
74  NS_LOG_FUNCTION (Current () << UtilsGetNodeId () << name << value << overwrite);
75  NS_ASSERT (Current () != 0);
76  struct Thread *current = Current ();
77  char ***penvp = current->process->penvp;
78  if (overwrite != 0)
79  {
80  dce_unsetenv (name);
81  }
82  int namelen = strlen (name);
83  char **cur;
84  int len = 0;
85  for (cur = *penvp; *cur != 0; cur++)
86  {
87  if (overwrite == 0 && strncmp (*cur, name, namelen) == 0)
88  {
89  // there is already an entry which matches this name and
90  // we cannot remove it so, we are done.
91  return 0;
92  }
93  len++;
94  }
95  char **newEnvp = (char**)malloc (sizeof(char*) * (len + 1 + 1));
96  current->process->allocated.push_back (newEnvp);
97  memcpy (newEnvp, *penvp, sizeof(char*) * (len));
98  int valuelen = strlen (value);
99  char *str = (char*)malloc (namelen + 1 + valuelen + 1);
100  current->process->allocated.push_back (str);
101  memcpy (str, name, namelen);
102  str[namelen] = '=';
103  memcpy (str + namelen + 1,value, valuelen);
104  str[namelen + 1 + valuelen] = 0;
105  newEnvp[len] = str;
106  newEnvp[len + 1] = 0;
107  *penvp = newEnvp;
108  return 0;
109 }
110 int dce_unsetenv (const char *name)
111 {
112  NS_LOG_FUNCTION (Current () << UtilsGetNodeId () << name);
113  NS_ASSERT (Current () != 0);
114  struct Thread *current = Current ();
115  char ***penvp = current->process->penvp;
116  int namelen = strlen (name);
117  char **cur;
118  for (cur = *penvp; *cur != 0;)
119  {
120  char *equal = strchr (*cur, '=');
121  if (equal == 0)
122  {
123  continue;
124  }
125  if (strncmp (*cur, name, namelen) != 0)
126  {
127  cur++;
128  continue;
129  }
130  char **i, **prev;
131  *cur = *(cur + 1);
132  for (i = cur + 1, prev = cur; *i != 0; i++, prev++)
133  {
134  *prev = *i;
135  }
136  }
137  return 0;
138 }
139 int dce_clearenv (void)
140 {
141  NS_LOG_FUNCTION (Current () << UtilsGetNodeId ());
142  NS_ASSERT (Current () != 0);
143  struct Thread *current = Current ();
144  char ***penvp = current->process->penvp;
145  *penvp = (char**)malloc (sizeof (char *));
146  current->process->allocated.push_back (*penvp);
147  **penvp = 0;
148  return 0;
149 }