-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathname.c
174 lines (156 loc) · 3.35 KB
/
name.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Eagles Bulletin Board System
Copyright (C) 1995, Ray Rocker, [email protected]
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.
*/
#include "common.h"
#if LACKS_MALLOC_H
# include <stdlib.h>
#else
# include <malloc.h>
#endif
void
free_namelist(list)
NAMELIST *list;
{
NAMENODE *trav = *list, *next;
while (trav) {
free(trav->word);
next = trav->next;
free(trav);
trav = next;
}
}
void
create_namelist(list)
NAMELIST *list;
{
free_namelist(list);
*list = NULL;
}
add_namelist(list, name, name_before)
NAMELIST *list;
char *name;
char *name_before;
{
NAMENODE *node;
NAMENODE *trav = *list;
node = (NAMENODE *)malloc(sizeof(NAMENODE));
if (!node) return S_SYSERR;
node->word = (char *)malloc(strlen(name)+1);
node->next = NULL;
if (!node->word) return S_SYSERR;
strcpy(node->word, name);
if (*list == NULL ||
(name_before && !strcasecmp(name_before, trav->word))) {
node->next = *list;
*list = node;
}
else {
while (trav->next) {
if (name_before && !strcasecmp(name_before, trav->next->word)) {
node->next = trav->next;
break;
}
trav = trav->next;
}
trav->next = node;
}
return 0;
}
remove_namelist(list, name)
NAMELIST *list;
char *name;
{
NAMENODE *prev = NULL;
NAMENODE *curr = *list;
while (curr) {
if (!strcasecmp(curr->word, name)) {
if (prev == NULL) *list = curr->next;
else prev->next = curr->next;
curr->next = NULL;
free(curr->word);
free(curr);
return S_OK;
}
prev = curr;
curr = curr->next;
}
return S_NOSUCHREC;
}
is_in_namelist(list, name)
NAMELIST list;
char *name;
{
while (list) {
if (!strcasecmp(list->word, name))
return 1;
list = list->next;
}
return 0;
}
apply_namelist(list, fptr, arg)
NAMELIST list;
int (*fptr)();
void *arg;
{
int indx = 0;
for(; list!=NULL; list=list->next)
(*fptr)(indx++, list->word, arg);
return indx;
}
/*ARGSUSED*/
_write_list_element(indx, name, fp)
int indx;
char *name;
FILE *fp;
{
fprintf(fp, "%s\n", name);
return S_OK;
}
write_namelist(fname, list)
char *fname;
NAMELIST list;
{
FILE *fp;
if (list == NULL) unlink(fname);
else {
if ((fp = fopen(fname, "w")) == NULL) {
return S_SYSERR;
}
apply_namelist(list, _write_list_element, fp);
fclose(fp);
}
return S_OK;
}
/*ARGSUSED*/
_read_list_element(indx, rec, list)
int indx;
char *rec;
NAMELIST *list;
{
strip_trailing_space(rec);
rec[NAMELEN] = '\0';
if (!isspace(*rec) && !is_in_namelist(*list, rec)) {
add_namelist(list, rec, NULL);
}
return S_OK;
}
read_namelist(fname, list)
char *fname;
NAMELIST *list;
{
create_namelist(list);
_record_enumerate(fname, 0, _read_list_element, list);
return S_OK;
}