Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcs/csattr.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* Glenn Fowler
23
* AT&T Research
24
*
25
* return the local attr attributes for host name
26
*/
27
28
#include "cslib.h"
29
30
#include <ctype.h>
31
#include <hash.h>
32
33
typedef struct
34
{
35
HASH_HEADER;
36
char data[1];
37
} Info_t;
38
39
/*
40
* load the info from the local info file
41
*/
42
43
static Hash_table_t*
44
load(register Cs_t* state)
45
{
46
register char* s;
47
char* h;
48
register Info_t* ip;
49
register Sfio_t* sp;
50
register Hash_table_t* tp;
51
int c;
52
53
if (!(sp = csinfo(state, NiL, NiL))) return 0;
54
if (tp = hashalloc(NiL, HASH_set, HASH_ALLOCATE, HASH_name, "state->info", 0))
55
while (s = sfgetr(sp, '\n', 1))
56
{
57
while (isspace(*s)) s++;
58
h = s;
59
while (!isspace(*s) && *s) s++;
60
if (*s)
61
{
62
c = *s;
63
*s++ = 0;
64
if (!(ip = (Info_t*)hashlook(tp, h, HASH_CREATE|HASH_SIZE(sizeof(Info_t)+strlen(s)+1), NiL)))
65
{
66
hashfree(tp);
67
tp = 0;
68
break;
69
}
70
*--s = c;
71
strcpy(ip->data, s);
72
}
73
}
74
sfclose(sp);
75
return tp;
76
}
77
78
/*
79
* info cached on first call and return value placed in static buffer ...
80
* if name==0 || name=="-" then all valid records returned by successive
81
* csattr() calls until 0 return
82
* if attr==0 || attr=="-" then all attributes returned
83
*/
84
85
char*
86
csattr(register Cs_t* state, const char* name, const char* attr)
87
{
88
register char* s;
89
register char* b;
90
register char* x;
91
register char* v;
92
register Info_t* ip;
93
int n;
94
unsigned long addr;
95
96
static Hash_table_t* tp;
97
static Hash_position_t* pt;
98
static char buf[256];
99
100
messagef((state->id, NiL, -8, "attr(%s,%s) call", name, attr));
101
if (!tp && !(tp = load(state)))
102
return 0;
103
if (attr && (!*attr || *attr == '-' && !*(attr + 1)))
104
attr = 0;
105
b = buf;
106
x = &buf[sizeof(buf) - 1];
107
if (!name || *name == '-' && !*(name + 1))
108
{
109
name = 0;
110
scan:
111
if (!pt && !(pt = hashscan(tp, 0)))
112
return 0;
113
n = attr && streq(attr, "name");
114
do if (!(ip = (Info_t*)hashnext(pt)))
115
{
116
hashdone(pt);
117
pt = 0;
118
return 0;
119
} while (n && streq(ip->name, CS_HOST_LOCAL));
120
if (n)
121
return ip->name;
122
if (attr && streq(attr, "*"))
123
return buf;
124
b += sfsprintf(b, x - b, "%s", ip->name);
125
}
126
else
127
{
128
if (streq(name, CS_HOST_LOCAL))
129
name = (const char*)csname(state, 0);
130
if (!(ip = (Info_t*)hashlook(tp, name, HASH_LOOKUP, NiL)))
131
return 0;
132
if (attr)
133
{
134
if (streq(attr, "*"))
135
return buf;
136
if (streq(attr, "name"))
137
return csaddr(state, ip->name) ? state->host : ip->name;
138
if (streq(attr, "addr") || streq(attr, "host"))
139
{
140
if (addr = csaddr(state, ip->name))
141
return *attr == 'a' ? csntoa(state, addr) : state->host;
142
return CS_HOST_UNKNOWN;
143
}
144
}
145
}
146
if (!attr)
147
{
148
v = ip->data;
149
if (b == buf) while (isspace(*v)) v++;
150
b += sfsprintf(b, x - b, "%s", v);
151
if (addr = csaddr(state, ip->name))
152
{
153
if (!streq(ip->name, state->host))
154
b += sfsprintf(b, x - b, " host=%s", state->host);
155
b += sfsprintf(b, x - b, " addr=%s", csntoa(state, addr));
156
}
157
}
158
else if (streq(attr, "addr") || streq(attr, "host"))
159
b += sfsprintf(b, x - b, " %s", (addr = csaddr(state, ip->name)) ? (*attr == 'a' ? csntoa(state, addr) : state->host) : CS_HOST_UNKNOWN);
160
else for (v = ip->data;;)
161
{
162
while (isspace(*v)) v++;
163
if (!*v)
164
{
165
if (!name)
166
{
167
b = buf;
168
goto scan;
169
}
170
return 0;
171
}
172
for (s = (char*)attr; *s && *v == *s++; v++);
173
if (!*s && (*v == '=' || !*v || isspace(*v)))
174
{
175
if (*v == '=') v++;
176
else v = "1";
177
if (b > buf && b < x) *b++ = ' ';
178
while (b < x && !isspace(*v) && (*b++ = *v++));
179
break;
180
}
181
while (*v && !isspace(*v)) v++;
182
}
183
*b = 0;
184
return buf;
185
}
186
187
char*
188
_cs_attr(const char* name, const char* attr)
189
{
190
return csattr(&cs, name, attr);
191
}
192
193