forked from crigler/dtach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
122 lines (102 loc) · 2.85 KB
/
util.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
This file is part of dtachez.
Copyright (C) 2023 SudoMaker, Ltd.
Author: Reimu NotMoe <reimu@sudomaker.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "dtachez.hpp"
#include <cstdarg>
void write_all(int fd, const void *buf, size_t count) {
size_t total_written = 0;
while (total_written < count) {
ssize_t bytes_written = write(fd, (uint8_t *)buf + total_written, count - total_written);
if (bytes_written == -1) {
if (errno == EINTR || errno == EAGAIN) {
// Try again
continue;
} else {
// Error occurred, return the error code
THROW_ERROR("failed to write");
}
} else if (bytes_written == 0) {
// End of file
break;
} else {
total_written += bytes_written;
}
}
if (count != total_written)
THROW_ERROR("incomplete write");
}
void read_all(int fd, void *buf, size_t count) {
size_t total_read = 0;
while (total_read < count) {
ssize_t bytes_read = read(fd, (uint8_t *)buf + total_read, count - total_read);
if (bytes_read == -1) {
if (errno == EINTR || errno == EAGAIN) {
// Try again
continue;
} else {
// Error occurred, return the error code
THROW_ERROR("failed to read");
}
} else if (bytes_read == 0) {
// End of file
break;
} else {
total_read += bytes_read;
}
}
if (count != total_read)
THROW_ERROR("incomplete read");
}
int ensure_open(const char *s, int m) {
int fd = open(s, m);
if (fd < 0) {
THROW_ERROR("open");
}
return fd;
}
void ensure_mkfifo(const char *s) {
// Make it 0600 to prevent any surprises
if (mkfifo(s, 0600)) {
if (errno != EEXIST) {
THROW_ERROR("mkfifo");
}
}
}
/* Sets a file descriptor to non-blocking mode. */
int setnonblocking(int fd) {
int flags;
#if defined(O_NONBLOCK)
flags = fcntl(fd, F_GETFL);
if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
return -1;
return 0;
#elif defined(FIONBIO)
flags = 1;
if (ioctl(fd, FIONBIO, &flags) < 0)
return -1;
return 0;
#else
#warning Do not know how to set non-blocking mode.
return 0;
#endif
}
char * __attribute__ ((__format__ (__printf__, 1, 2))) _str_fmt(const char *fmt, ...) {
static char format_buf[PATH_MAX + 128];
va_list ap;
va_start(ap, fmt);
vsnprintf(format_buf, sizeof(format_buf)-1, fmt, ap);
va_end(ap);
return format_buf;
}