Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/tappend.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 *f1, *f2;
25
char* s;
26
Sfoff_t p;
27
char buf[1024];
28
int r, w;
29
30
if(!(f1 = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w")) )
31
terror("Can't open f1");
32
if(!(f1 = sfopen(f1, tstfile("sf", 0), "a+")) )
33
terror("Can't open f1");
34
35
if(!(f2 = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "a+")) )
36
terror("Can't open f2");
37
38
if(sfwrite(f1,"012345678\n",10) != 10 || sfsync(f1) < 0)
39
terror("Writing to f1");
40
if((p = sftell(f1)) != 10)
41
terror("Bad sftell1 %ld",p);
42
43
if(sfwrite(f2,"abcdefghi\n",10) != 10 || sfsync(f2) < 0)
44
terror("Writing to f2");
45
if((p = sftell(f2)) != 20)
46
terror("Bad sftell2");
47
48
if((p = sfseek(f1,(Sfoff_t)0,0)) != 0)
49
terror("Bad seek");
50
if(!(s = sfgetr(f1,'\n',1)) )
51
terror("Bad getr1");
52
if(strcmp(s,"012345678") != 0)
53
terror("Bad input1");
54
55
if((p = sftell(f1)) != 10)
56
terror("Bad sftell3");
57
58
if(sfwrite(f1,"012345678\n",10) != 10 || sfsync(f1) < 0)
59
terror("Writing to f1");
60
if((p = sftell(f1)) != 30)
61
terror("Bad sftell4");
62
63
if((p = sfseek(f2,(Sfoff_t)10,0)) != 10)
64
terror("Bad seek");
65
if(!(s = sfgetr(f2,'\n',1)) )
66
terror("Bad getr2");
67
if(strcmp(s,"abcdefghi") != 0)
68
terror("Bad input2");
69
70
if(!(s = sfgetr(f2,'\n',1)) )
71
terror("Bad getr3");
72
if(strcmp(s,"012345678") != 0)
73
terror("Bad input3");
74
75
if(!(f1 = sfopen(f1, tstfile("sf", 0), "w")) )
76
terror("Can't open file to write");
77
for(r = 0; r < 1024; ++r)
78
buf[r] = 'a';
79
if((w = sfwrite(f1,buf,1024)) != 1024)
80
terror("writing w=%d", w);
81
if(!(f1 = sfopen(f1, tstfile("sf", 0), "a")) )
82
terror("Can't open file to append");
83
sfseek(f1,(Sfoff_t)0,0);
84
if((w = sfwrite(f1,buf,64)) != 64)
85
terror("writing w=%d", w);
86
if((r = (int)sftell(f1)) != (1024+64) )
87
terror("seek position wrong s=%d", r);
88
89
texit(0);
90
}
91
92