Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tputgetr.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
/* to test handling of writing whole lines */
23
static ssize_t writef(Sfio_t* f, const void* data, size_t n, Sfdisc_t* disc)
24
{
25
char *dt;
26
ssize_t k;
27
28
for(dt = (char*)data + n-1; dt >= (char*)data; --dt)
29
if(*dt == '\n')
30
break;
31
32
if((k = (dt - (char*)data) + 1) == 0 )
33
tinfo("Processing a partial line, ok");
34
35
return k;
36
}
37
38
Sfdisc_t Disc = { 0, writef, 0, 0, 0 };
39
40
tmain()
41
{
42
char buf[100];
43
Sfio_t *fp;
44
int i;
45
char *s;
46
47
if(!(fp = sftmp(8)))
48
terror("Can't open temp file");
49
50
sfset(fp,SF_LINE,1);
51
for(i = 0; i < 1000; ++i)
52
{ sfsprintf(buf,sizeof(buf),"Number: %d",i);
53
if(sfputr(fp,buf,'\n') <= 0)
54
terror("Writing %s",buf);
55
}
56
57
sfseek(fp,(Sfoff_t)0,0);
58
59
for(i = 0; i < 1000; ++i)
60
{ sfsprintf(buf,sizeof(buf),"Number: %d",i);
61
if(!(s = sfgetr(fp,'\n',1)))
62
terror("Reading %s",buf);
63
if(strcmp(s,buf) != 0)
64
terror("Input=%s, Expect=%s",s,buf);
65
}
66
67
sfseek(fp,(Sfoff_t)0,0);
68
s = sfgetr(fp,'\0',1);
69
if(s)
70
terror("Expecting a null string");
71
s = sfgetr(fp,'\0',-1);
72
if(!s)
73
terror("Expecting a non-null string");
74
if(sfvalue(fp) != sfsize(fp))
75
terror("Wrong size");
76
77
sfclose(fp);
78
if(!(fp = sfnew(0, buf, 12, 1, SF_WRITE)) )
79
terror("Opening a test stream");
80
sfsetbuf(fp, buf, 12);
81
sfset(fp, SF_LINE, 0);
82
sfdisc(fp, &Disc);
83
if(sfputr(fp, "0123456789", '\n') != 11)
84
terror("Sfputr failed1");
85
if(sfputr(fp, "0", -1) != 1)
86
terror("Sfputr failed2");
87
if(sfputr(fp, "1", -1) != 1)
88
terror("Sfputr failed3");
89
90
texit(0);
91
}
92
93