1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1996 BBN, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 /* from: static char sccsid[] = "@(#)util.c	8.2 (Berkeley) 4/2/94"; */
37 static const char rcsid[] = "@(#) $Id: util.c,v 1.2 2003-04-12 16:08:52 heas Exp $";
38 #endif /* not lint */
39 
40 #include "config.h"
41 
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #if HAVE_SYS_WAIT_H
45 # include <sys/wait.h>
46 #endif
47 
48 #include <ctype.h>
49 #include <errno.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #if HAVE_STRING_H
54 # include <string.h>
55 #endif
56 #if HAVE_SYS_UNISTD_H
57 # include <unistd.h>
58 #endif
59 
60 #include "rcpd.h"
61 
62 #define RCPROUNDUP(x, y)   ((((x)+((y)-1))/(y))*(y))
63 
64 void
verifydir(cp)65 verifydir(cp)
66 	char *cp;
67 {
68 	struct stat stb;
69 
70 	if (!stat(cp, &stb)) {
71 		if (S_ISDIR(stb.st_mode))
72 			return;
73 		errno = ENOTDIR;
74 	}
75 	run_err("%s: %s", cp, strerror(errno));
76 	exit(1);
77 }
78 
79 BUF *
allocbuf(bp,fd,blksize)80 allocbuf(bp, fd, blksize)
81 	BUF *bp;
82 	int fd, blksize;
83 {
84 	struct stat stb;
85 	size_t size;
86 
87 	if (fstat(fd, &stb) < 0) {
88 		run_err("fstat: %s", strerror(errno));
89 		return (0);
90 	}
91 	size = RCPROUNDUP(stb.st_blksize, blksize);
92 	if (size == 0)
93 		size = blksize;
94 	if (bp->cnt >= size)
95 		return (bp);
96 	if ((bp->buf = bp->buf?realloc(bp->buf, size):malloc(size)) == NULL) {
97 		bp->cnt = 0;
98 		run_err("%s", strerror(errno));
99 		return (0);
100 	}
101 	bp->cnt = size;
102 	return (bp);
103 }
104 
105 void
lostconn(signo)106 lostconn(signo)
107 	int signo;
108 {
109 	error("lost connection");
110 	exit(1);
111 }
112