+/* This is used to get/check local shell variables */
+static char *get_local_var(const char *s)
+{
+ struct variables *cur;
+
+ if (!s)
+ return NULL;
+ for (cur = top_vars; cur; cur=cur->next)
+ if(strcmp(cur->name, s)==0)
+ return cur->value;
+ return NULL;
+}
+
+/* This is used to set local shell variables
+ flg_export==0 if only local (not exporting) variable
+ flg_export==1 if "new" exporting environ
+ flg_export>1 if current startup environ (not call putenv()) */
+static int set_local_var(const char *s, int flg_export)
+{
+ char *name, *value;
+ int result=0;
+ struct variables *cur;
+
+ name=strdup(s);
+
+ /* Assume when we enter this function that we are already in
+ * NAME=VALUE format. So the first order of business is to
+ * split 's' on the '=' into 'name' and 'value' */
+ value = strchr(name, '=');
+ if (value==0 && ++value==0) {
+ free(name);
+ return -1;
+ }
+ *value++ = 0;
+
+ for(cur = top_vars; cur; cur = cur->next) {
+ if(strcmp(cur->name, name)==0)
+ break;
+ }
+
+ if(cur) {
+ if(strcmp(cur->value, value)==0) {
+ if(flg_export>0 && cur->flg_export==0)
+ cur->flg_export=flg_export;
+ else
+ result++;
+ } else {
+ if(cur->flg_read_only) {
+ bb_error_msg("%s: readonly variable", name);
+ result = -1;
+ } else {
+ if(flg_export>0 || cur->flg_export>1)
+ cur->flg_export=1;
+ free(cur->value);
+
+ cur->value = strdup(value);
+ }
+ }
+ } else {
+ cur = malloc(sizeof(struct variables));
+ if(!cur) {
+ result = -1;
+ } else {
+ cur->name = strdup(name);
+ if(cur->name == 0) {
+ free(cur);
+ result = -1;
+ } else {
+ struct variables *bottom = top_vars;
+ cur->value = strdup(value);
+ cur->next = 0;
+ cur->flg_export = flg_export;
+ cur->flg_read_only = 0;
+ while(bottom->next) bottom=bottom->next;
+ bottom->next = cur;
+ }
+ }
+ }
+
+ if(result==0 && cur->flg_export==1) {
+ *(value-1) = '=';
+ result = putenv(name);
+ } else {
+ free(name);
+ if(result>0) /* equivalent to previous set */
+ result = 0;
+ }
+ return result;
+}
+
+static void unset_local_var(const char *name)
+{
+ struct variables *cur;
+
+ if (name) {
+ for (cur = top_vars; cur; cur=cur->next) {
+ if(strcmp(cur->name, name)==0)
+ break;
+ }
+ if(cur!=0) {
+ struct variables *next = top_vars;
+ if(cur->flg_read_only) {
+ bb_error_msg("%s: readonly variable", name);
+ return;
+ } else {
+ if(cur->flg_export)
+ unsetenv(cur->name);
+ free(cur->name);
+ free(cur->value);
+ while (next->next != cur)
+ next = next->next;
+ next->next = cur->next;
+ }
+ free(cur);
+ }
+ }
+}
+
+static int is_assignment(const char *s)
+{
+ if (s==NULL || !isalpha(*s)) return 0;
+ ++s;
+ while(isalnum(*s) || *s=='_') ++s;
+ return *s=='=';
+}
+