2 * Copyright (c) 2011-2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <sys/types.h>
34 #include <jeffpc/io.h>
35 #include <jeffpc/error.h>
37 static inline ssize_t
rw(int fd
, void *buf
, size_t nbyte
, off_t off
,
38 const bool useoff
, const bool readfxns
)
49 ret
= pread(fd
, ptr
, nbyte
, off
);
51 ret
= read(fd
, ptr
, nbyte
);
54 ret
= pwrite(fd
, ptr
, nbyte
, off
);
56 ret
= write(fd
, ptr
, nbyte
);
74 int xread(int fd
, void *buf
, size_t nbyte
)
76 return rw(fd
, buf
, nbyte
, 0, false, true);
79 int xpread(int fd
, void *buf
, size_t nbyte
, off_t off
)
81 return rw(fd
, buf
, nbyte
, off
, true, true);
84 int xwrite(int fd
, const void *buf
, size_t nbyte
)
86 return rw(fd
, (void *) buf
, nbyte
, 0, false, false);
89 int xpwrite(int fd
, const void *buf
, size_t nbyte
, off_t off
)
91 return rw(fd
, (void *) buf
, nbyte
, off
, true, false);
94 char *read_file_common(int dirfd
, const char *fname
, struct stat
*sb
)
101 fd
= xopenat(dirfd
, fname
, O_RDONLY
, 0);
107 ret
= fstat(fd
, &statbuf
);
109 out
= ERR_PTR(-errno
);
113 out
= malloc(statbuf
.st_size
+ 1);
115 out
= ERR_PTR(-ENOMEM
);
119 ret
= xread(fd
, out
, statbuf
.st_size
);
124 out
[statbuf
.st_size
] = '\0';
137 int write_file(const char *fname
, const char *data
, size_t len
)
142 fd
= xopen(fname
, O_WRONLY
| O_CREAT
| O_EXCL
,
143 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
147 ret
= xwrite(fd
, data
, len
);