27 |
/* Turn on nonblocking I/O on a fd */ |
/* Turn on nonblocking I/O on a fd */ |
28 |
int FAST_FUNC ndelay_on(int fd) |
int FAST_FUNC ndelay_on(int fd) |
29 |
{ |
{ |
30 |
return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) | O_NONBLOCK); |
return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
31 |
} |
} |
32 |
|
|
33 |
int FAST_FUNC ndelay_off(int fd) |
int FAST_FUNC ndelay_off(int fd) |
34 |
{ |
{ |
35 |
return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL) & ~O_NONBLOCK); |
return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK); |
36 |
} |
} |
37 |
|
|
38 |
int FAST_FUNC close_on_exec_on(int fd) |
int FAST_FUNC close_on_exec_on(int fd) |
40 |
return fcntl(fd, F_SETFD, FD_CLOEXEC); |
return fcntl(fd, F_SETFD, FD_CLOEXEC); |
41 |
} |
} |
42 |
|
|
43 |
/* Convert unsigned long long value into compact 4-char |
char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src) |
44 |
* representation. Examples: "1234", "1.2k", " 27M", "123T" |
{ |
45 |
* String is not terminated (buf[4] is untouched) */ |
#ifndef IFNAMSIZ |
46 |
void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale) |
enum { IFNAMSIZ = 16 }; |
47 |
{ |
#endif |
48 |
const char *fmt; |
return strncpy(dst, src, IFNAMSIZ); |
|
char c; |
|
|
unsigned v, u, idx = 0; |
|
|
|
|
|
if (ul > 9999) { // do not scale if 9999 or less |
|
|
ul *= 10; |
|
|
do { |
|
|
ul /= 1024; |
|
|
idx++; |
|
|
} while (ul >= 10000); |
|
|
} |
|
|
v = ul; // ullong divisions are expensive, avoid them |
|
|
|
|
|
fmt = " 123456789"; |
|
|
u = v / 10; |
|
|
v = v % 10; |
|
|
if (!idx) { |
|
|
// 9999 or less: use "1234" format |
|
|
// u is value/10, v is last digit |
|
|
c = buf[0] = " 123456789"[u/100]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
c = buf[1] = fmt[u/10%10]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
buf[2] = fmt[u%10]; |
|
|
buf[3] = "0123456789"[v]; |
|
|
} else { |
|
|
// u is value, v is 1/10ths (allows for 9.2M format) |
|
|
if (u >= 10) { |
|
|
// value is >= 10: use "123M', " 12M" formats |
|
|
c = buf[0] = " 123456789"[u/100]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
v = u % 10; |
|
|
u = u / 10; |
|
|
buf[1] = fmt[u%10]; |
|
|
} else { |
|
|
// value is < 10: use "9.2M" format |
|
|
buf[0] = "0123456789"[u]; |
|
|
buf[1] = '.'; |
|
|
} |
|
|
buf[2] = "0123456789"[v]; |
|
|
buf[3] = scale[idx]; /* typically scale = " kmgt..." */ |
|
|
} |
|
|
} |
|
|
|
|
|
/* Convert unsigned long long value into compact 5-char representation. |
|
|
* String is not terminated (buf[5] is untouched) */ |
|
|
void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[6], const char *scale) |
|
|
{ |
|
|
const char *fmt; |
|
|
char c; |
|
|
unsigned v, u, idx = 0; |
|
|
|
|
|
if (ul > 99999) { // do not scale if 99999 or less |
|
|
ul *= 10; |
|
|
do { |
|
|
ul /= 1024; |
|
|
idx++; |
|
|
} while (ul >= 100000); |
|
|
} |
|
|
v = ul; // ullong divisions are expensive, avoid them |
|
|
|
|
|
fmt = " 123456789"; |
|
|
u = v / 10; |
|
|
v = v % 10; |
|
|
if (!idx) { |
|
|
// 99999 or less: use "12345" format |
|
|
// u is value/10, v is last digit |
|
|
c = buf[0] = " 123456789"[u/1000]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
c = buf[1] = fmt[u/100%10]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
c = buf[2] = fmt[u/10%10]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
buf[3] = fmt[u%10]; |
|
|
buf[4] = "0123456789"[v]; |
|
|
} else { |
|
|
// value has been scaled into 0..9999.9 range |
|
|
// u is value, v is 1/10ths (allows for 92.1M format) |
|
|
if (u >= 100) { |
|
|
// value is >= 100: use "1234M', " 123M" formats |
|
|
c = buf[0] = " 123456789"[u/1000]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
c = buf[1] = fmt[u/100%10]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
v = u % 10; |
|
|
u = u / 10; |
|
|
buf[2] = fmt[u%10]; |
|
|
} else { |
|
|
// value is < 100: use "92.1M" format |
|
|
c = buf[0] = " 123456789"[u/10]; |
|
|
if (c != ' ') fmt = "0123456789"; |
|
|
buf[1] = fmt[u%10]; |
|
|
buf[2] = '.'; |
|
|
} |
|
|
buf[3] = "0123456789"[v]; |
|
|
buf[4] = scale[idx]; /* typically scale = " kmgt..." */ |
|
|
} |
|
49 |
} |
} |
50 |
|
|
51 |
|
|
164 |
} |
} |
165 |
#endif |
#endif |
166 |
|
|
167 |
|
char* FAST_FUNC xmalloc_ttyname(int fd) |
168 |
|
{ |
169 |
|
char *buf = xzalloc(128); |
170 |
|
int r = ttyname_r(fd, buf, 127); |
171 |
|
if (r) { |
172 |
|
free(buf); |
173 |
|
buf = NULL; |
174 |
|
} |
175 |
|
return buf; |
176 |
|
} |
177 |
|
|
178 |
/* It is perfectly ok to pass in a NULL for either width or for |
/* It is perfectly ok to pass in a NULL for either width or for |
179 |
* height, in which case that value will not be set. */ |
* height, in which case that value will not be set. */ |
180 |
int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height) |
int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height) |
209 |
{ |
{ |
210 |
return tcsetattr(STDIN_FILENO, TCSANOW, tp); |
return tcsetattr(STDIN_FILENO, TCSANOW, tp); |
211 |
} |
} |
212 |
|
|
213 |
|
void FAST_FUNC generate_uuid(uint8_t *buf) |
214 |
|
{ |
215 |
|
/* http://www.ietf.org/rfc/rfc4122.txt |
216 |
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
217 |
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
218 |
|
* | time_low | |
219 |
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
220 |
|
* | time_mid | time_hi_and_version | |
221 |
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
222 |
|
* |clk_seq_and_variant | node (0-1) | |
223 |
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
224 |
|
* | node (2-5) | |
225 |
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
226 |
|
* IOW, uuid has this layout: |
227 |
|
* uint32_t time_low (big endian) |
228 |
|
* uint16_t time_mid (big endian) |
229 |
|
* uint16_t time_hi_and_version (big endian) |
230 |
|
* version is a 4-bit field: |
231 |
|
* 1 Time-based |
232 |
|
* 2 DCE Security, with embedded POSIX UIDs |
233 |
|
* 3 Name-based (MD5) |
234 |
|
* 4 Randomly generated |
235 |
|
* 5 Name-based (SHA-1) |
236 |
|
* uint16_t clk_seq_and_variant (big endian) |
237 |
|
* variant is a 3-bit field: |
238 |
|
* 0xx Reserved, NCS backward compatibility |
239 |
|
* 10x The variant specified in rfc4122 |
240 |
|
* 110 Reserved, Microsoft backward compatibility |
241 |
|
* 111 Reserved for future definition |
242 |
|
* uint8_t node[6] |
243 |
|
* |
244 |
|
* For version 4, these bits are set/cleared: |
245 |
|
* time_hi_and_version & 0x0fff | 0x4000 |
246 |
|
* clk_seq_and_variant & 0x3fff | 0x8000 |
247 |
|
*/ |
248 |
|
pid_t pid; |
249 |
|
int i; |
250 |
|
|
251 |
|
i = open("/dev/urandom", O_RDONLY); |
252 |
|
if (i >= 0) { |
253 |
|
read(i, buf, 16); |
254 |
|
close(i); |
255 |
|
} |
256 |
|
/* Paranoia. /dev/urandom may be missing. |
257 |
|
* rand() is guaranteed to generate at least [0, 2^15) range, |
258 |
|
* but lowest bits in some libc are not so "random". */ |
259 |
|
srand(monotonic_us()); |
260 |
|
pid = getpid(); |
261 |
|
while (1) { |
262 |
|
for (i = 0; i < 16; i++) |
263 |
|
buf[i] ^= rand() >> 5; |
264 |
|
if (pid == 0) |
265 |
|
break; |
266 |
|
srand(pid); |
267 |
|
pid = 0; |
268 |
|
} |
269 |
|
|
270 |
|
/* version = 4 */ |
271 |
|
buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; |
272 |
|
/* variant = 10x */ |
273 |
|
buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; |
274 |
|
} |