summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/src/bf_portable.c
blob: 4de0af3cd3f36dd06b4e087a840e083cab034d19 (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright (c) 1999-2000 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 * Contributed by Exactis.com, Inc.
 *
 */

#ifndef lint
static char id[] = "@(#)$Id: bf_portable.c,v 8.25.4.3 2000/06/29 21:21:58 gshapiro Exp $";
#endif /* ! lint */

#if SFIO
# include <sfio/stdio.h>
#endif /* SFIO */

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <sys/uio.h>
#include <errno.h>
#if !SFIO
# include <stdio.h>
#endif /* !SFIO */
#ifndef BF_STANDALONE
# include "sendmail.h"
#endif /* ! BF_STANDALONE */
#include "bf_portable.h"
#include "bf.h"

/*
**  BFOPEN -- create a new buffered file
**
**	Parameters:
**		filename -- the file's name
**		fmode -- what mode the file should be created as
**		bsize -- amount of buffer space to allocate (may be 0)
**		flags -- if running under sendmail, passed directly to safeopen
**
**	Returns:
**		a FILE * which may then be used with stdio functions, or NULL
**		on failure. FILE * is opened for writing (mode "w+").
**
**	Side Effects:
**		none.
**
**	Sets errno:
**		ENOMEM -- out of memory
**		ENOENT -- illegal empty filename specified
**		any value of errno specified by open()
**		any value of errno specified by fdopen()
**		any value of errno specified by funopen()
*/

#ifdef BF_STANDALONE
# define OPEN(fn, omode, cmode, sff) open(fn, omode, cmode)
#else /* BF_STANDALONE */
# define OPEN(fn, omode, cmode, sff) safeopen(fn, omode, cmode, sff)
#endif /* BF_STANDALONE */

/* List of currently-open buffered files */
struct bf *bflist = NULL;

FILE *
bfopen(filename, fmode, bsize, flags)
	char *filename;
	int fmode;
	size_t bsize;
	long flags;
{
	struct bf *bfp;
	FILE *retval;
	int fd, l;

	fd = OPEN(filename, O_RDWR | O_CREAT | O_TRUNC, fmode, flags);
	if (fd == -1)
	{
		/* errno is set implicitly by open */
		return NULL;
	}

	retval = fdopen(fd, "w+");

	/* If failure, return immediately */
	if (retval == NULL)
	{
		/* errno is set implicitly by fdopen */
		return NULL;
	}

	/* Allocate memory */
	bfp = (struct bf *)malloc(sizeof(struct bf));
	if (bfp == NULL)
	{
		(void) fclose(retval);

		/* don't care about errors */
		(void) unlink(filename);
		errno = ENOMEM;
		return NULL;
	}
	if (tTd(58, 8))
		dprintf("bfopen(%s): malloced %ld\n",
			filename, (long) sizeof(struct bf));

	l = strlen(filename) + 1;
	bfp->bf_filename = (char *)malloc(l);
	if (bfp->bf_filename == NULL)
	{
		free(bfp);
		(void) fclose(retval);

		/* don't care about errors */
		(void) unlink(filename);
		errno = ENOMEM;
		return NULL;
	}
	(void) strlcpy(bfp->bf_filename, filename, l);

	/* Fill in the other fields, then add it to the list */
	bfp->bf_key = retval;
	bfp->bf_committed = FALSE;
	bfp->bf_refcount = 1;

	bfinsert(bfp);

	/* Whew. Nothing bad happened. We're okay. */
	return retval;
}
/*
**  BFDUP -- increase refcount on buffered file
**
**	Parameters:
**		fp -- FILE * to "duplicate"
**
**	Returns:
**		fp with increased refcount
*/

FILE *
bfdup(fp)
	FILE *fp;
{
	struct bf *bfp;

	/* Get associated bf structure */
	bfp = bflookup(fp);

	if (bfp == NULL)
		return NULL;

	/* Increase the refcount */
	bfp->bf_refcount++;

	return fp;
}

/*
**  BFCOMMIT -- "commits" the buffered file
**
**	Parameters:
**		fp -- FILE * to commit to disk
**
**	Returns:
**		0 on success, -1 on error
**
**	Side Effects:
**		Forces the given FILE * to be written to disk if it is not
**		already, and ensures that it will be kept after closing. If
**		fp is not a buffered file, this is a no-op.
**
**	Sets errno:
**		any value of errno specified by open()
**		any value of errno specified by write()
**		any value of errno specified by lseek()
*/

int
bfcommit(fp)
	FILE *fp;
{
	struct bf *bfp;

	/* Get associated bf structure */
	bfp = bflookup(fp);

	/* If called on a normal FILE *, noop */
	if (bfp != NULL)
		bfp->bf_committed = TRUE;

	return 0;
}

/*
**  BFREWIND -- rewinds the FILE *
**
**	Parameters:
**		fp -- FILE * to rewind
**
**	Returns:
**		0 on success, -1 on error
**
**	Side Effects:
**		rewinds the FILE * and puts it into read mode. Normally one
**		would bfopen() a file, write to it, then bfrewind() and
**		fread(). If fp is not a buffered file, this is equivalent to
**		rewind().
**
**	Sets errno:
**		any value of errno specified by fseek()
*/

int
bfrewind(fp)
	FILE *fp;
{
	int err;

	/* check to see if there is an error on the stream */
	err = ferror(fp);

	(void) fflush(fp);

	/*
	**  Clear error if tried to fflush()
	**  a read-only file pointer and
	**  there wasn't a previous error.
	*/

	if (err == 0)
		clearerr(fp);

	/* errno is set implicitly by fseek() before return */
	return fseek(fp, 0, SEEK_SET);
}

/*
**  BFTRUNCATE -- rewinds and truncates the FILE *
**
**	Parameters:
**		fp -- FILE * to truncate
**
**	Returns:
**		0 on success, -1 on error
**
**	Side Effects:
**		rewinds the FILE *, truncates it to zero length, and puts it
**		into write mode. If fp is not a buffered file, this is
**		equivalent to a rewind() and then an ftruncate(fileno(fp), 0).
**
**	Sets errno:
**		any value of errno specified by fseek()
**		any value of errno specified by ftruncate()
*/

int
bftruncate(fp)
	FILE *fp;
{
	int ret;

	if (bfrewind(fp) == -1)
	{
		/* errno is set implicitly by bfrewind() */
		return -1;
	}

#if NOFTRUNCATE
	/* XXX */
	errno = EINVAL;
	ret = -1;
#else /* NOFTRUNCATE */
	/* errno is set implicitly by ftruncate() before return */
	ret = ftruncate(fileno(fp), 0);
#endif /* NOFTRUNCATE */
	return ret;
}

/*
**  BFCLOSE -- close a buffered file
**
**	Parameters:
**		fp -- FILE * to close
**
**	Returns:
**		0 on success, EOF on failure
**
**	Side Effects:
**		Closes fp. If fp is a buffered file, unlink it if it has not
**		already been committed. If fp is not a buffered file, this is
**		equivalent to fclose().
**
**	Sets errno:
**		any value of errno specified by fclose()
*/

int
bfclose(fp)
	FILE *fp;
{
	int retval;
	struct bf *bfp = NULL;

	/* Get associated bf structure */
	bfp = bflookup(fp);

	/* Decrement and check refcount */
	if (bfp != NULL && --bfp->bf_refcount > 0)
		return 0;

	/* If bf, get bf structure and remove from list */
	if (bfp != NULL)
		bfp = bfdelete(fp);

	if (fclose(fp) == EOF)
	{
		if (tTd(58, 8))
			dprintf("bfclose: fclose failed\n");
		/* errno is set implicitly by fclose() */
		return -1;
	}

	if (bfp == NULL)
		return 0;

	/* Success unless we determine otherwise in next block */
	retval = 0;

	if (bfp != NULL)
	{
		/* Might have to unlink; certainly will have to deallocate */
		if (!bfp->bf_committed)
			retval = unlink(bfp->bf_filename);

		free(bfp->bf_filename);
		free(bfp);
		if (tTd(58, 8))
			dprintf("bfclose: freed %ld\n",
				(long) sizeof(struct bf));
	}
	else
	{
		if (tTd(58, 8))
			dprintf("bfclose: bfp was NULL\n");
	}

	return retval;
}

/*
**  BFTEST -- test if a FILE * is a buffered file
**
**	Parameters:
**		fp -- FILE * to test
**
**	Returns:
**		TRUE if fp is a buffered file, FALSE otherwise.
**
**	Side Effects:
**		none.
**
**	Sets errno:
**		never.
*/

bool
bftest(fp)
	FILE *fp;
{
	return (bflookup(fp) != NULL);
}

/*
**  BFINSERT -- insert item in linking list
**
**	Parameters:
**		datum -- item to insert
**
**	Returns:
**		none.
**
**	Side Effects:
**		none.
**
**	Sets errno:
**		never.
*/

void
bfinsert(datum)
	struct bf *datum;
{
	datum->bf_cdr = bflist;
	bflist = datum;
}

/*
**  BFLOOKUP -- lookup FILE * in list
**
**	Parameters:
**		fp -- FILE * to lookup
**
**	Returns:
**		bf struct for the FILE *, NULL if not found
**
**	Side Effects:
**		none.
**
**	Sets errno:
**		never.
*/

struct bf *
bflookup(key)
	FILE *key;
{
	struct bf *t;

	for (t = bflist; t != NULL; t = t->bf_cdr)
	{
		if (t->bf_key == key)
		{
			return t;
		}
	}

	/* If we got this far, we didn't find it */
	return NULL;
}

/*
**  BFDELETE -- delete a FILE * in list
**
**	Parameters:
**		fp -- FILE * to delete
**
**	Returns:
**		bf struct for deleted FILE *, NULL if not found,
**
**	Side Effects:
**		none.
**
**	Sets errno:
**		never.
*/

struct bf *
bfdelete(key)
	FILE *key;
{
	struct bf *t, *u;

	if (bflist == NULL)
		return NULL;

	/* if first element, special case */
	if (bflist->bf_key == key)
	{
		u = bflist;
		bflist = bflist->bf_cdr;
		return u;
	}

	for (t = bflist; t->bf_cdr != NULL; t = t->bf_cdr)
	{
		if (t->bf_cdr->bf_key == key)
		{
			u = t->bf_cdr;
			t->bf_cdr = u->bf_cdr;
			return u;
		}
	}

	/* If we got this far, we didn't find it */
	return NULL;
}
OpenPOWER on IntegriCloud