forked from albertw/flash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.c
126 lines (91 loc) · 2.68 KB
/
set.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Functions to set/search/unset variables.
Copyright (C) Stephen Fegan 1995, 1996
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.
please send patches or advice on flash to: `flash@netsoc.ucd.ie'
*/
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<termios.h>
#include<unistd.h>
#include<pwd.h>
#include"set.h"
#include"tree.h"
#include"misc.h"
struct dict *SetVariables;
static int Set_name_compare(void *a,void *b)
{
return strcmp(((struct set_node *)a)->variable,((struct set_node *)b)->variable);
}
int find_variable(char *key, char **data)
{
struct set_node k,*x;
k.variable=key;
k.value=NULL;
if(data!=NULL)*data=NULL;
if(SetVariables==NULL)return 0;
x=(struct set_node *)find_node(SetVariables,&k);
if((x==NULL)||((x->flags & SF_SET) == 0))return 0;
else
{
if(data!=NULL)*data=x->value;
return 1;
}
}
void set_variable(char *key,char *data,int resettable)
{
struct set_node *k,*x;
k=(struct set_node *)xmalloc(sizeof(*k));
k->variable=xmalloc(strlen(key)+1);
strcpy(k->variable,key);
if(data!=NULL)
{
k->value=xmalloc(strlen(data)+1);
strcpy(k->value,data);
}
else k->value=NULL;
k->flags = SF_SET;
if(!resettable)k->flags |= SF_NORESET;
if(SetVariables==NULL)SetVariables=new_dict(Set_name_compare);
if(!add_unique_node(SetVariables,(void *)k))
{
x=find_node(SetVariables,(void *)k);
if((x->flags & SF_NORESET)==0)
x=change_node(SetVariables,(void *)k);
else x=k;
free(x);
}
}
void unset_variable(char *key)
{
struct set_node k,*x;
k.variable=key;
k.value=NULL;
if(SetVariables==NULL)return;
x=find_node(SetVariables,&k);
if((x!=NULL)&&((x->flags & SF_NORESET)==0))
x->flags &= !(SF_SET | SF_NORESET);
return;
}
void global_variables(void)
{
struct passwd *pw;
char temp[40];
pw=getpwuid(getuid());
sprintf(temp,"%d",pw->pw_uid);
set_variable("uid",temp,0);
set_variable("user",pw->pw_name,0);
set_variable("home",pw->pw_dir,0);
set_variable("tty",ttyname(0),0);
}