Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/tests/sfio/terrno.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
/* errnos tested for xopen-compliance */
23
24
tmain()
25
{
26
Sfio_t* fw;
27
Sfio_t* fr;
28
int fds[2];
29
int lseek_errno;
30
int rv;
31
32
if(!(fw = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "w")) )
33
terror("Can't create temp file %s to write", tstfile("sf", 0));
34
if(!(fr = sfopen(NIL(Sfio_t*), tstfile("sf", 0), "r")) )
35
terror("Can't open temp file %s to read", tstfile("sf", 0));
36
37
sfseek(fr, (Sfoff_t)0, SEEK_END);
38
if(sfgetc(fr) >= 0 || !sfeof(fr))
39
terror("Should have seen eof");
40
41
errno = 0;
42
if((rv = sfwrite(fr, "a", 1)) == 1)
43
terror("sfwrite returns %d, expecting 1", rv);
44
if(errno != EBADF)
45
twarn("Wrong errno %d after sfwrite(%d), expecting %d",errno,rv,EBADF);
46
47
/* on some system (eg, apple), lseek does not set errno for this case */
48
errno = 0;
49
lseek(sffileno(fw), (off_t)(-2), SEEK_SET);
50
lseek_errno = errno;
51
lseek(sffileno(fw), (off_t)0, SEEK_SET);
52
errno = 0;
53
54
if(sfseek(fw, (Sfoff_t)(-2), SEEK_SET) != (Sfoff_t)(-1) )
55
terror("sfseek should have failed");
56
if(errno != lseek_errno)
57
twarn("Wrong errno %d after sfseek, expecting %d", errno, lseek_errno);
58
59
errno = 0;
60
if(sfseek(fw, (Sfoff_t)0, SEEK_SET|SEEK_CUR|SEEK_END) >= 0)
61
terror("sfseek should not have succeeded");
62
if(errno != EINVAL)
63
twarn("Wrong errno %d after sfseek, expecting %d", errno, EINVAL);
64
65
if(pipe(fds) < 0)
66
terror("Can't create pipes");
67
68
if(!(fw = sfnew(fw, NIL(Void_t*), (size_t)SF_UNBOUND, fds[1], SF_WRITE)) )
69
terror("Can't create stream for pipe");
70
71
errno = 0;
72
if(sfseek(fw, (Sfoff_t)0, SEEK_SET) >= 0)
73
terror("sfseek should have failed on a pipe");
74
if(errno != ESPIPE)
75
twarn("Wrong errno %d after sfseek, expecting %d", ESPIPE);
76
77
close(sffileno(fw));
78
errno = 0;
79
if(sfseek(fw, (Sfoff_t)0, SEEK_END) >= 0)
80
terror("sfseek should have failed on a closed file descriptor");
81
if(errno != EBADF)
82
twarn("Wrong errno %d after sfseek, expecting %d", EBADF);
83
84
texit(0);
85
}
86
87