scclib
Stable Cloud Computing C++ Library
safe_clib.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_SYS_SAFE_CLIB_H
32 #define _SCC_SYS_SAFE_CLIB_H
33 
34 #include <cstdio>
35 #include <sys/types.h>
36 
37 namespace scc::util
38 {
39 
63 int safe_close(int fd);
65 int safe_close_throw(int fd);
67 ssize_t safe_read(int fd, void *buf, size_t count);
69 ssize_t safe_read_throw(int fd, void *buf, size_t count);
71 ssize_t safe_write(int fd, const void *buf, size_t count);
73 ssize_t safe_write_throw(int fd, const void *buf, size_t count);
75 int safe_dup(int oldfd);
77 int safe_dup_throw(int oldfd);
79 int safe_dup2(int oldfd, int newfd);
81 int safe_dup2_throw(int oldfd, int newfd);
82 
84 int safe_open(const char *pathname, int flags);
86 int safe_open(const char *pathname, int flags, mode_t mode);
88 int safe_open_throw(const char *pathname, int flags);
90 int safe_open_throw(const char *pathname, int flags, mode_t mode);
91 
93 FILE* safe_fopen(const char *pathname, const char *mode);
95 FILE* safe_fopen_throw(const char *pathname, const char *mode);
97 int safe_fclose(FILE* stream);
99 int safe_fclose_throw(FILE* stream);
101 int safe_fscanf(FILE *stream, const char *format, ...);
103 int safe_fscanf_throw(FILE *stream, const char *format, ...);
105 ssize_t safe_getline(char **lineptr, size_t *n, FILE *stream);
107 ssize_t safe_getline_throw(char **lineptr, size_t *n, FILE *stream);
109 int safe_truncate(const char *path, off_t length);
111 int safe_truncate_throw(const char *path, off_t length);
113 int safe_ftruncate(int fd, off_t length);
115 int safe_ftruncate_throw(int fd, off_t length);
116 
118 int safe_wait(int *wstatus);
120 int safe_waitpid(pid_t pid, int *wstatus, int options);
122 int safe_wait_throw(int *wstatus);
124 int safe_waitpid_throw(pid_t pid, int *wstatus, int options);
125 
128 }
129 
130 #endif
int safe_open(const char *pathname, int flags, mode_t mode)
Signal safe open.
Definition: safe_clib.cc:233
int safe_close(int fd)
Signal safe close.
Definition: safe_clib.cc:95
int safe_dup2_throw(int oldfd, int newfd)
Signal safe dup2, throws system_error on error.
Definition: safe_clib.cc:198
ssize_t safe_read_throw(int fd, void *buf, size_t count)
Signal safe read, throws system_error on error.
Definition: safe_clib.cc:129
int safe_wait_throw(int *wstatus)
Signal safe wait, throws system_error on error.
Definition: safe_clib.cc:380
int safe_fclose_throw(FILE *stream)
Signal safe fclose, throws system_error on error.
Definition: safe_clib.cc:290
ssize_t safe_write_throw(int fd, const void *buf, size_t count)
Signal safe write, throws system_error on error.
Definition: safe_clib.cc:152
FILE * safe_fopen(const char *pathname, const char *mode)
Signal safe fopen.
Definition: safe_clib.cc:256
ssize_t safe_read(int fd, void *buf, size_t count)
Signal safe read.
Definition: safe_clib.cc:118
int safe_wait(int *wstatus)
Signal safe wait.
Definition: safe_clib.cc:364
ssize_t safe_write(int fd, const void *buf, size_t count)
Signal safe write.
Definition: safe_clib.cc:141
int safe_dup_throw(int oldfd)
Signal safe dup, throws system_error on error.
Definition: safe_clib.cc:175
ssize_t safe_getline_throw(char **lineptr, size_t *n, FILE *stream)
Signal safe getline, throw on error.
Definition: safe_clib.cc:352
ssize_t safe_getline(char **lineptr, size_t *n, FILE *stream)
Signal safe getline.
Definition: safe_clib.cc:341
int safe_fscanf(FILE *stream, const char *format,...)
Signal safe fscanf.
Definition: safe_clib.cc:317
int safe_close_throw(int fd)
Signal safe close, throws system_error on error.
Definition: safe_clib.cc:106
int safe_waitpid_throw(pid_t pid, int *wstatus, int options)
Signal safe waitpid, throws system_error on error.
Definition: safe_clib.cc:385
int safe_ftruncate_throw(int fd, off_t length)
Signal safe ftruncate, throw on error.
Definition: safe_clib.cc:83
int safe_fclose(FILE *stream)
Signal safe fclose.
Definition: safe_clib.cc:279
int safe_truncate(const char *path, off_t length)
Signal safe truncate.
Definition: safe_clib.cc:49
int safe_waitpid(pid_t pid, int *wstatus, int options)
Signal safe waitpid.
Definition: safe_clib.cc:369
int safe_dup2(int oldfd, int newfd)
Signal safe dup2.
Definition: safe_clib.cc:187
int safe_open_throw(const char *pathname, int flags, mode_t mode)
Signal safe open, throws system_error on error.
Definition: safe_clib.cc:244
int safe_fscanf_throw(FILE *stream, const char *format,...)
Signal safe fscanf, throw on error.
Definition: safe_clib.cc:326
int safe_truncate_throw(const char *path, off_t length)
Signal safe truncate, throw on error.
Definition: safe_clib.cc:60
int safe_ftruncate(int fd, off_t length)
Signal safe ftruncate.
Definition: safe_clib.cc:72
int safe_dup(int oldfd)
Signal safe dup.
Definition: safe_clib.cc:164
FILE * safe_fopen_throw(const char *pathname, const char *mode)
Signal safe fopen, throws system_error on error.
Definition: safe_clib.cc:267