]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * Network library. | |
3 | * Copyright (C) 1997 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | ||
25 | /* Read nbytes from fd and store into ptr. */ | |
26 | int | |
02ff83c5 | 27 | readn (int fd, u_char *ptr, int nbytes) |
718e3744 | 28 | { |
29 | int nleft; | |
30 | int nread; | |
31 | ||
32 | nleft = nbytes; | |
33 | ||
34 | while (nleft > 0) | |
35 | { | |
36 | nread = read (fd, ptr, nleft); | |
37 | ||
38 | if (nread < 0) | |
39 | return (nread); | |
40 | else | |
41 | if (nread == 0) | |
42 | break; | |
43 | ||
44 | nleft -= nread; | |
45 | ptr += nread; | |
46 | } | |
47 | ||
48 | return nbytes - nleft; | |
49 | } | |
50 | ||
51 | /* Write nbytes from ptr to fd. */ | |
52 | int | |
b04c699e | 53 | writen(int fd, const u_char *ptr, int nbytes) |
718e3744 | 54 | { |
55 | int nleft; | |
56 | int nwritten; | |
57 | ||
58 | nleft = nbytes; | |
59 | ||
60 | while (nleft > 0) | |
61 | { | |
62 | nwritten = write(fd, ptr, nleft); | |
63 | ||
64 | if (nwritten <= 0) | |
65 | return (nwritten); | |
66 | ||
67 | nleft -= nwritten; | |
68 | ptr += nwritten; | |
69 | } | |
70 | return nbytes - nleft; | |
71 | } |