File: dest.c

package info (click to toggle)
deliver 2.1.14-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 488 kB
  • ctags: 914
  • sloc: ansic: 6,050; yacc: 405; makefile: 131; sh: 34
file content (283 lines) | stat: -rw-r--r-- 5,574 bytes parent folder | download | duplicates (3)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* $Id: dest.c,v 1.2 1993/10/28 16:49:51 chip Exp $
 *
 * Operations on the list of mail destinations.
 *
 * $Log: dest.c,v $
 * Revision 1.2  1993/10/28 16:49:51  chip
 * Declare function return types, including void.
 *
 * Revision 1.1  1991/05/13  18:36:55  chip
 * Initial revision
 *
 */

#include "deliver.h"

/*
 * Local data.
 */

static DEST deadhead = { &deadhead, &deadhead };
#define HEADPTR	(&deadhead)

/*
 * Local functions.
 */

static int destcmp();
static int ptrcmp();

/*----------------------------------------------------------------------
 * Consider mail to the given user as undeliverable.
 */

DEST *
dest_undel(name)
char *name;
{
    return dest(name, CL_MBOX, MBX_UNDEL);
}

/*----------------------------------------------------------------------
 * Add a new destination to the list (unless it already exists).
 * Return pointer to DEST.
 */

DEST *
dest(name, class, s)
char *name;
DCLASS class;
char *s;
{
    DEST *d;

    /* Make sure that parameter is provided when it's needed. */

    if ((class == CL_MBOX || class == CL_PROG) != (s != NULL))
	return NULL;

    /* Look for an already-existing copy of the given destination. */

    for (d = HEADPTR->d_next; d != HEADPTR; d = d->d_next)
    {
	if (d->d_class != class)
	    continue;

	if (strcmp(d->d_name, name) != 0)
	    continue;

	if (s && strcmp(d->d_param, s) != 0)
	    continue;

	/*
	 * Like, gnarly, dude!  It's already in the chain!
	 */

	return d;
    }

    /*
     * The given dest isn't in the list, so we have to add it.
     */

    d = talloc(DEST, 1);
    d->d_class = class;
    d->d_state = ST_WORKING;
    d->d_name = copystr(name);
    d->d_param = s ? copystr(s) : NULL;

    /*
     * Check address for validity.
     */

    if (!addr_clean(name))
	dest_err(d, E_IVADDR);
    else if (class != CL_UUCP && name_context(name) == NULL)
	dest_err(d, E_NSUSER);

    /*
     * Put new address at the end of of the chain.
     * (This is important!  Other code depends on it.)
     */

    d->d_prev = HEADPTR->d_prev;
    d->d_next = HEADPTR;
    d->d_prev->d_next = d;
    d->d_next->d_prev = d;

    return d;
}

/*----------------------------------------------------------------------
 * Return pointer to first DEST in the list.
 */

DEST *
first_dest()
{
    if (HEADPTR->d_next != HEADPTR)
	return HEADPTR->d_next;

    return NULL;
}

/*----------------------------------------------------------------------
 * Return pointer to next DEST in the list, or NULL.
 */

DEST *
next_dest(d)
DEST *d;
{
    if (d && (d = d->d_next) != HEADPTR)
	return d;

    return NULL;
}

/*----------------------------------------------------------------------
 * Return the number of destinations in the list.
 */

int
dest_count()
{
    DEST *d;
    int count;

    count = 0;
    for (d = HEADPTR->d_next; d && d != HEADPTR; d = d->d_next)
	++count;

    return count;
}

/*----------------------------------------------------------------------
 * Return an allocated array of DEST pointers, or NULL if none.
 * The given integer is set to the array size.
 * Note that the caller must FREE this array when he's done.
 */

DEST **
dest_array(countp)
int *countp;
{
    DEST **dv, *d;
    int i, count;

    if ((count = dest_count()) <= 0)
    {
	*countp = 0;
	return NULL;
    }

    dv = talloc(DEST *, count);

    i = 0;
    for (d = HEADPTR->d_next; d && d != HEADPTR; d = d->d_next)
    {
	if (i < count)
	    dv[i++] = d;
    }

    qsort((char *) dv, (unsigned) count, sizeof(dv[0]), destcmp);

    *countp = count;
    return dv;
}

/*----------------------------------------------------------------------
 * Compare two DEST pointers, for output sorting.
 */

static int
destcmp(p1, p2)
char *p1, *p2;
{
    DEST *d1, *d2;
    int cmp;

    d1 = *(DEST **) p1;
    d2 = *(DEST **) p2;

#if 0
    /* Errors go last. */
    if ((d1->d_state == ST_ERROR) != (d2->d_state == ST_ERROR))
	return (d1->d_state == ST_ERROR) ? 1 : -1;
#endif

#if 0
    /* By class. */
    if ((cmp = (int) d1->d_class - (int) d2->d_class) != 0)
	return (cmp < 0) ? -1 : 1;
#endif

    /* By user name. */
    if ((cmp = ptrcmp(d1->d_name, d2->d_name)) != 0)
	return cmp;

    /* By mailbox/program. */
    if ((cmp = ptrcmp(d1->d_param, d2->d_param)) != 0)
	return cmp;

    return 0;
}

/*----------------------------------------------------------------------
 * Compare two pointers, either of which may be NULL.
 */

static int
ptrcmp(p, q)
char *p, *q;
{
    if (p == q)
	return 0;
    if (p == NULL)
	return -1;
    if (q == NULL)
	return 1;
    return strcmp(p, q);
}

/*----------------------------------------------------------------------
 * Return a destination's error message.
 */

char *
derrmsg(d)
DEST *d;
{
    static char unknown_buf[40];
    static char no_error[] = "no error?!";

    if (!d || d->d_state != ST_ERROR)
	return no_error;

    switch (d->d_error)
    {
    case E_IVADDR:
	return "Invalid address string";
    case E_NSUSER:
	return "No such user";
    case E_NSHOST:
	return "No such UUCP host";
    case E_CTPERM:
	return "No permissions for that context";
    case E_CTLOST:
	return "Context lost (should never happen)";
    case E_MBOX:
	return "Can't write to mailbox";
    case E_PROG:
	return "Subprocess reported failure when exiting";
    case E_PIPE:
	return "Subprocess died while reading its standard input";
    case E_DFONLY:
	return "Address not valid from command line";
    case E_ERRMSG:
	return d->d_errmsg ? d->d_errmsg : no_error;
    }

    (void) sprintf(unknown_buf, "Unknown error %d", d->d_error);
    return unknown_buf;
}