Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tgetr.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1999-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "sftest.h"
21
22
tmain()
23
{
24
Sfio_t *f;
25
int n, i;
26
char buf[50], *s, *string = "111\n222\n333";
27
28
f = sfopen(NIL(Sfio_t*),string,"s");
29
if(!(s = sfgetr(f,'\n',SF_STRING|SF_LOCKR)) || strcmp(s,"111") != 0)
30
terror("sfgetr failed1");
31
32
if(sfgetr(f,'\n',0) != NIL(char*))
33
terror("sfgetr should have failed because of locking");
34
sfread(f,s,1);
35
36
if(!(s = sfgetr(f,'\n',SF_STRING)) || strcmp(s,"222") != 0)
37
terror("sfgetr failed2");
38
39
if((s = sfgetr(f,'\n',0)) != NIL(char*))
40
terror("sfgetr should have failed because of partial record");
41
42
if(!(s = sfgetr(f,0,SF_LASTR)) )
43
terror("sfgetr should have succeeded getting partial record");
44
45
/* test type == -1 and type == 1 modes */
46
sfseek(f,(Sfoff_t)0,0);
47
if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"111") != 0)
48
terror("sfgetr failed in compatible mode");
49
50
if(!(s = sfgetr(f,'\n',SF_STRING|SF_LOCKR)) || strcmp(s,"222") != 0)
51
terror("sfgetr failed3");
52
if(sfgetr(f,'\n',1) )
53
terror("sfgetr should have failed due to locking");
54
sfread(f,s,0);
55
56
if(sfgetr(f,'\n',1) )
57
terror("sfgetr should have failed because record is incomplete");
58
59
if(!(s = sfgetr(f,0,-1)) || strcmp(s,"333") != 0)
60
terror("sfgetr failed in getting last partial record");
61
62
if(!(f = sftmp(0)) )
63
terror("Can't open temporary stream");
64
for(n = 0; n < 10; ++n) /* each record is 100 bytes */
65
{ for(i = 0; i < 100; ++i)
66
sfputc(f, 'a');
67
sfputc(f,'\n');
68
}
69
sfseek(f,(Sfoff_t)0,0);
70
sfsetbuf(f, buf, 50);
71
if(!(s = sfgetr(f, '\n', 1)))
72
terror("Can't get a record");
73
74
n = 80;
75
sfmaxr(n, 1); /* set maximum record size */
76
if((i = sfmaxr(0, 0)) != n)
77
terror("maxr is %d, expected %d", i, n);
78
if((s = sfgetr(f, '\n', 1)) != 0)
79
terror("Shouldn't have gotten a record");
80
81
n = 0;
82
sfmaxr(n, 1); /* no record size limit */
83
if((i = sfmaxr(0, 0)) != n)
84
terror("maxr is %d, expected %d", i, n);
85
if(!(s = sfgetr(f, '\n', 1)))
86
terror("Can't get a record");
87
if(!(s = sfgetr(f, '\n', 1)))
88
terror("Can't get a record");
89
90
texit(0);
91
}
92
93