39ffbb1e645c21060d99660290af49d0dbf556c1
[platform/upstream/bash.git] / lib / sh / zwrite.c
1 /* Copyright (C) 1999 Free Software Foundation, Inc.
2
3    This file is part of GNU Bash, the Bourne Again SHell.
4
5    Bash is free software; you can redistribute it and/or modify it under
6    the terms of the GNU General Public License as published by the Free
7    Software Foundation; either version 2, or (at your option) any later
8    version.
9
10    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
11    WARRANTY; without even the implied warranty of MERCHANTABILITY or
12    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13    for more details.
14    
15    You should have received a copy of the GNU General Public License along
16    with Bash; see the file COPYING.  If not, write to the Free Software
17    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
18
19 #include <config.h>
20
21 #include <sys/types.h>
22
23 #if defined (HAVE_UNISTD_H)
24 #  include <unistd.h>
25 #endif
26
27 #include <errno.h>
28
29 #if !defined (errno)
30 extern int errno;
31 #endif
32
33 /* Write NB bytes from BUF to file descriptor FD, retrying the write if
34    it is interrupted.  We retry three times if we get a zero-length
35    write.  Any other signal causes this function to return prematurely. */
36 int
37 zwrite (fd, buf, nb)
38      int fd;
39      char *buf;
40      size_t nb;
41 {
42   int n, i, nt;
43
44   for (n = nb, nt = 0;;)
45     {
46       i = write (fd, buf, n);
47       if (i > 0)
48         {
49           n -= i;
50           if (n <= 0)
51             return nb;
52           buf += i;
53         }
54       else if (i == 0)
55         {
56           if (++nt > 3)
57             return (nb - n);
58         }
59       else if (errno != EINTR)
60         return -1;
61     }
62 }