summaryrefslogtreecommitdiffstats
path: root/libpam/pam_modutil_ioloop.c
blob: 54ab0e55264e63a29679bd06a57892a5e944dc39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 * $Id$
 *
 * These functions provides common methods for ensure a complete read or
 * write occurs. It handles EINTR and partial read/write returns.
 */

#include "pam_modutil_private.h"

#include <unistd.h>
#include <errno.h>

int
pam_modutil_read(int fd, char *buffer, int count)
{
       int block, offset = 0;

       while (count > 0) {
               block = read(fd, &buffer[offset], count);

               if (block < 0) {
                       if (errno == EINTR) continue;
                       return block;
               }
               if (block == 0) return offset;

               offset += block;
               count -= block;
       }

       return offset;
}

int
pam_modutil_write(int fd, const char *buffer, int count)
{
       int block, offset = 0;

       while (count > 0) {
               block = write(fd, &buffer[offset], count);

               if (block < 0) {
                       if (errno == EINTR) continue;
                       return block;
               }
               if (block == 0) return offset;

               offset += block;
               count -= block;
       }

       return offset;
}