1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #include "sfhdr.h"
23
24 /* Resize a stream.
25 Written by Kiem-Phong Vo.
26 */
27
28 #if __STD_C
sfresize(Sfio_t * f,Sfoff_t size)29 int sfresize(Sfio_t* f, Sfoff_t size)
30 #else
31 int sfresize(f, size)
32 Sfio_t* f;
33 Sfoff_t size;
34 #endif
35 {
36 SFMTXDECL(f);
37
38 SFMTXENTER(f, -1);
39
40 if(size < 0 || f->extent < 0 ||
41 (f->mode != SF_WRITE && _sfmode(f,SF_WRITE,0) < 0) )
42 SFMTXRETURN(f, -1);
43
44 SFLOCK(f,0);
45
46 if(f->flags&SF_STRING)
47 { SFSTRSIZE(f);
48
49 if(f->extent >= size)
50 { if((f->flags&SF_MALLOC) && (f->next - f->data) <= size)
51 { size_t s = (((size_t)size + 1023)/1024)*1024;
52 Void_t* d;
53 if(s < f->size && (d = realloc(f->data, s)) )
54 { f->data = d;
55 f->size = s;
56 f->extent = s;
57 }
58 }
59 memclear((char*)(f->data+size), (int)(f->extent-size));
60 }
61 else
62 { if(SFSK(f, size, SEEK_SET, f->disc) != size)
63 SFMTXRETURN(f, -1);
64 memclear((char*)(f->data+f->extent), (int)(size-f->extent));
65 }
66 }
67 else
68 { if(f->next > f->data)
69 SFSYNC(f);
70 #if _lib_ftruncate
71 if(ftruncate(f->file, (sfoff_t)size) < 0)
72 SFMTXRETURN(f, -1);
73 #else
74 SFMTXRETURN(f, -1);
75 #endif
76 }
77
78 f->extent = size;
79
80 SFOPEN(f, 0);
81
82 SFMTXRETURN(f, 0);
83 }
84