Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active July 28, 2017 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/0acf51c256fd2fd4fd904ada7f1079e6 to your computer and use it in GitHub Desktop.
Save mattn/0acf51c256fd2fd4fd904ada7f1079e6 to your computer and use it in GitHub Desktop.
5%の確率で Vim になる Emacs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <time.h>
int
main(int argc, char* argv[]) {
const char* home = getenv("HOME");
int lhs = 5, rhs = 95;
char s[PATH_MAX], ns[PATH_MAX];
FILE *fp;
char line[2048];
double ratio;
int golhs = 0;
strcpy(s, home);
strcat(s, "/.boronrc");
fp = fopen(s, "r");
if (fp) {
line[0] = 0;
fgets(line, sizeof(s), fp);
fclose(fp);
sscanf(line, "%d,%d", &lhs, &rhs);
}
ratio = (double)lhs / (double)(lhs + rhs);
if (ratio > 0.05)
rhs++;
else if (ratio < 0.05) {
lhs++;
golhs = 1;
} else {
srand(time(NULL));
if (rand() % 100 > 5)
rhs++;
else {
lhs++;
golhs = 1;
}
}
strcat(ns, s);
strcat(ns, "~");
fp = fopen(ns, "w");
if (fp) {
fprintf(fp, "%d,%d", lhs, rhs);
fclose(fp);
rename(ns, s);
}
if (golhs)
argv[0] = "/usr/bin/vim";
else
argv[0] = "/usr/bin/emacs";
execv(argv[0], argv);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment