buffer.c revision 55682
1149050Spjd/*
2149050Spjd * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska H�gskolan
3149050Spjd * (Royal Institute of Technology, Stockholm, Sweden).
4149050Spjd * All rights reserved.
5149050Spjd *
6149050Spjd * Redistribution and use in source and binary forms, with or without
7149050Spjd * modification, are permitted provided that the following conditions
8149050Spjd * are met:
9149050Spjd *
10149050Spjd * 1. Redistributions of source code must retain the above copyright
11149050Spjd *    notice, this list of conditions and the following disclaimer.
12149050Spjd *
13149050Spjd * 2. Redistributions in binary form must reproduce the above copyright
14208060Sdougb *    notice, this list of conditions and the following disclaimer in the
15149050Spjd *    documentation and/or other materials provided with the distribution.
16149050Spjd *
17149050Spjd * 3. Neither the name of the Institute nor the names of its contributors
18149050Spjd *    may be used to endorse or promote products derived from this software
19149050Spjd *    without specific prior written permission.
20149050Spjd *
21149050Spjd * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22149050Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23149050Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24149050Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25149050Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26149050Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27149050Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28149050Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29149050Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30149050Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31168283Sdes * SUCH DAMAGE.
32149050Spjd */
33149050Spjd
34149050Spjd#include "common.h"
35149050Spjd#include <stdio.h>
36149050Spjd#include <err.h>
37298550Slme#include "roken.h"
38149050Spjd
39149050SpjdRCSID("$Id: buffer.c,v 1.3 1999/12/02 16:58:29 joda Exp $");
40149050Spjd
41149050Spjd/*
42149050Spjd * Allocate a buffer enough to handle st->st_blksize, if
43149050Spjd * there is such a field, otherwise BUFSIZ.
44149050Spjd */
45149050Spjd
46239595Sdelphijvoid *
47149050Spjdalloc_buffer (void *oldbuf, size_t *sz, struct stat *st)
48149050Spjd{
49149050Spjd    size_t new_sz;
50149050Spjd
51149050Spjd    new_sz = BUFSIZ;
52149050Spjd#ifdef HAVE_ST_BLKSIZE
53149050Spjd    if (st)
54149050Spjd	new_sz = max(BUFSIZ, st->st_blksize);
55149050Spjd#endif
56149050Spjd    if(new_sz > *sz) {
57149050Spjd	if (oldbuf)
58149050Spjd	    free (oldbuf);
59149050Spjd	oldbuf = malloc (new_sz);
60	if (oldbuf == NULL) {
61	    warn ("malloc");
62	    *sz = 0;
63	    return NULL;
64	}
65	*sz = new_sz;
66    }
67    return oldbuf;
68}
69
70