/[path-sandbox]/trunk/libsbutil/sb_close.c
Gentoo

Diff of /trunk/libsbutil/sb_close.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/libsbutil/sbio.c Revision 326 trunk/libsbutil/sb_close.c Revision 340
1/* 1/*
2 * sbio.c 2 * sb_close.c
3 * 3 *
4 * IO functions. 4 * IO functions.
5 * 5 *
6 * Copyright 1999-2006 Gentoo Foundation 6 * Copyright 1999-2006 Gentoo Foundation
7 * 7 *
30#include <fcntl.h> 30#include <fcntl.h>
31 31
32#include "sbutil.h" 32#include "sbutil.h"
33 33
34 34
35static int (*sbio_open)(const char *, int, mode_t) = (void *)open;
36
37
38void sb_set_open(void *new_open)
39{
40 if (!check_ptr(new_open))
41 return;
42
43 sbio_open = new_open;
44}
45
46/* General purpose function to _reliably_ open a file
47 *
48 * Returns the file descriptor or -1 on error (and errno set)
49 */
50
51int sb_open(const char *path, int flags, mode_t mode)
52{
53 int fd;
54
55 do {
56 fd = sbio_open(path, flags, mode);
57 } while ((-1 == fd) && (EINTR == errno));
58
59 if ((-1 != fd) && (0 != errno))
60 /* Do not trigger debugging */
61 errno = 0;
62
63 if (-1 == fd)
64 DBG_MSG("Failed to open file '%s'!\n", path);
65
66 return fd;
67}
68
69/* General purpose function to _reliably_ read from a file.
70 *
71 * Returns total read bytes or -1 on error.
72 */
73
74size_t sb_read(int fd, void *buf, size_t count)
75{
76 ssize_t n;
77 size_t accum = 0;
78
79 do {
80 n = read(fd, buf + accum, count - accum);
81
82 if (n > 0) {
83 accum += n;
84 continue;
85 }
86
87 if (n < 0) {
88 if (EINTR == errno) {
89 /* Reset errno to not trigger DBG_MSG */
90 errno = 0;
91 continue;
92 }
93
94 DBG_MSG("Failed to read from fd=%i!\n", fd);
95 return -1;
96 }
97
98 /* Found EOF */
99 break;
100 } while (accum < count);
101
102 return accum;
103}
104
105/* General purpose function to _reliably_ write to a file
106 *
107 * If returned value is less than count, there was a fatal
108 * error and value tells how many bytes were actually written
109 */
110
111size_t sb_write(int fd, const void *buf, size_t count)
112{
113 ssize_t n;
114 size_t accum = 0;
115
116 do {
117 n = write(fd, buf + accum, count - accum);
118 if (n < 0) {
119 if (EINTR == errno) {
120 /* Reset errno to not trigger DBG_MSG */
121 errno = 0;
122 continue;
123 }
124
125 DBG_MSG("Failed to write to fd=%i!\n", fd);
126 break;
127 }
128
129 accum += n;
130 } while (accum < count);
131
132 return accum;
133}
134
135/* General purpose function to _reliably_ close a file 35/* General purpose function to _reliably_ close a file
136 * 36 *
137 * Returns 0 if successful or negative number on error (and errno set) 37 * Returns 0 if successful or negative number on error (and errno set)
138 */ 38 */
139 39

Legend:
Removed from v.326  
changed lines
  Added in v.340

  ViewVC Help
Powered by ViewVC 1.1.20