Skip to content

Instantly share code, notes, and snippets.

@dwest
Created June 13, 2017 14:58
Show Gist options
  • Save dwest/04648b26954713565e74c7c86c87ac5f to your computer and use it in GitHub Desktop.
Save dwest/04648b26954713565e74c7c86c87ac5f to your computer and use it in GitHub Desktop.
yes.c mucking about
/* See https://www.reddit.com/r/unix/comments/6gxduc/how_is_gnu_yes_so_fast/ for what this is all about, all credit to the original authors. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
char yes[2] = "y\n";
int pagesize = getpagesize() * 8;
char *buff = malloc(pagesize);
int buffused = 0;
while(buffused < pagesize) {
memcpy(buff+buffused, yes, 2);
buffused += 2;
}
/* Very minor improvement by setting buffering off */
setvbuf(stdout, NULL, _IONBF, 0);
while(fwrite(buff, pagesize, 1, stdout));
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment