Skip to content

Instantly share code, notes, and snippets.

@pcarrier
Last active August 29, 2015 14:08
Show Gist options
  • Save pcarrier/a9d9da3fe9ea0c22a6d8 to your computer and use it in GitHub Desktop.
Save pcarrier/a9d9da3fe9ea0c22a6d8 to your computer and use it in GitHub Desktop.
#define _FILE_OFFSET_BITS 64
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#define N 16384
int main() {
ssize_t r, counts[256] = {0};
unsigned char buf[N];
while ((r = read(0, (void*) buf, N)) > 0)
for (ssize_t j = 0; j < r; j++)
counts[buf[j]]++;
if (r != 0) return 1;
for (int i = 0; i < 256; i++) {
ssize_t count = counts[i];
memset(buf, i, (count < N) ? count : N);
for (; count >= N; count -= N)
if (write(1, buf, N) < 0)
return 1;
if (write(1, buf, count) < 0)
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment