comparison atomicio.c @ 1324:871698dd3669

Merge pull request #26 from fperrad/kill_kr remove K&R old style code
author Matt Johnston <matt@ucc.asn.au>
date Fri, 18 Nov 2016 22:48:50 +0800
parents 83d85b28b353
children
comparison
equal deleted inserted replaced
1321:2535ea9d0a6f 1324:871698dd3669
1 /* $OpenBSD: atomicio.c,v 1.17 2006/04/01 05:51:34 djm Exp $ */
1 /* 2 /*
2 * Copied from OpenSSH 3.6.1p2. 3 * Copied from OpenSSH/OpenBSD.
3 * 4 *
5 * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
4 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. 6 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
5 * All rights reserved. 7 * All rights reserved.
6 * 8 *
7 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 28 */
27 29
28 /* RCSID("OpenBSD: atomicio.c,v 1.10 2001/05/08 22:48:07 markus Exp "); */ 30 #include "includes.h"
29 31
30 #include "atomicio.h" 32 #include "atomicio.h"
31 33
32 /* 34 /*
33 * ensure all of data on socket comes through. f==read || f==write 35 * ensure all of data on socket comes through. f==read || f==vwrite
34 */ 36 */
35 ssize_t 37 size_t
36 atomicio(f, fd, _s, n) 38 atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
37 ssize_t (*f) ();
38 int fd;
39 void *_s;
40 size_t n;
41 { 39 {
42 char *s = _s; 40 char *s = _s;
41 size_t pos = 0;
43 ssize_t res; 42 ssize_t res;
44 size_t pos = 0;
45 43
46 while (n > pos) { 44 while (n > pos) {
47 res = (f) (fd, s + pos, n - pos); 45 res = (f) (fd, s + pos, n - pos);
48 switch (res) { 46 switch (res) {
49 case -1: 47 case -1:
50 #ifdef EWOULDBLOCK
51 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
52 #else
53 if (errno == EINTR || errno == EAGAIN) 48 if (errno == EINTR || errno == EAGAIN)
54 #endif
55 continue; 49 continue;
56 /* FALLTHROUGH */ 50 return 0;
57 case 0: 51 case 0:
58 return (res); 52 errno = EPIPE;
53 return pos;
59 default: 54 default:
60 pos += res; 55 pos += (size_t)res;
61 } 56 }
62 } 57 }
63 return (pos); 58 return (pos);
64 } 59 }