TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Convert hex to binary faster than xxd - part 1/2

2 pointsby textmodeabout 4 years ago
<p><pre><code> #include &lt;unistd.h&gt; #include &lt;poll.h&gt; #include &lt;errno.h&gt; #include &lt;sys&#x2F;types.h&gt; #include &lt;sys&#x2F;stat.h&gt; #ifndef EINTR #define EINTR (-5004) #endif #ifndef EAGAIN #define EAGAIN (-5011) #endif #ifndef EWOULDBLOCK #define EWOULDBLOCK (-7011) #endif int writeall(int fd,const void *xv,long long xlen) { const unsigned char *x = xv; long long w; while (xlen &gt; 0) { w = xlen; if (w &gt; 1048576) w = 1048576; w = write(fd,x,w); if (w &lt; 0) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { struct pollfd p; p.fd = fd; p.events = POLLOUT | POLLERR; poll(&amp;p,1,-1); continue; } return -1; } x += w; xlen -= w; } return 0; } long long readblock(int fd, void *x, long long xlen) { long long r; char *buf = x; while (xlen &gt; 0) { r = xlen; if (r &gt; 1048576) r = 1048576; r = read(fd, buf, r); if (r == 0) break; if (r == -1) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { struct pollfd p; p.fd = fd; p.events = POLLIN; poll(&amp;p, 1, -1); continue; } return -1; } buf += r; xlen -= r; } return (buf - (char *)x); } int fsyncfd(int fd) { struct stat st; if (fstat(fd, &amp;st) == 0 &amp;&amp; S_ISREG(st.st_mode)) { if (fsync(fd) == -1) return -1; } return 0; }</code></pre>

no comments

no comments