#include #include #include #include #include #include struct state { const char **cmd; int fd; const char *path; }; static void do_one_command(const char *cmd, struct state *state) { if (!strcmp(cmd, "unlink")) { fprintf(stderr, "dangle: unlink\n"); unlink(state->path); } else if (!strncmp(cmd, "write", 5)) { const char *data; data = cmd+5; if (!*data) data = "X"; fprintf(stderr, "dangle: write(\"%s\")\n", data); write(state->fd, data, strlen(data)); } else if (!strcmp(cmd, "fsync")) { fprintf(stderr, "dangle: fsync\n"); fsync(state->fd); } else if (!strcmp(cmd, "fdatasync")) { fprintf(stderr, "dangle: fdatasync\n"); fdatasync(state->fd); } else if (!strcmp(cmd, "sigpause")) { fprintf(stderr, "dangle: sigpause\n"); sigpause(0); } else if (!strncmp(cmd, "loop", 4)) { int i, N; N = atoi(cmd+4); fprintf(stderr, "dangle: looping, N=%d\n", N); state->cmd++; if (N == 0) { for (;;) do_one_command(*state->cmd, state); } else { for (i = 0 ; i < N ; i++) do_one_command(*state->cmd, state); } } else { fprintf(stderr, "dangle: unknown command \"%s\"\n", cmd); exit(1); } } static void do_commands(struct state *state) { for ( ; *state->cmd != NULL ; state->cmd++) { do_one_command(*state->cmd, state); } } int main(int argc, char **argv) { struct state state; if (argc < 2) { fprintf(stderr, "Usage: dangle filename [command...]\n"); exit(1); } state.path = argv[1]; state.cmd = (const char **)argv+2; fprintf(stderr, "dangle: open(\"%s\")\n", state.path); state.fd = open(state.path, O_RDWR|O_CREAT, 0); if (state.fd < 0) { perror(state.path); exit(1); } do_commands(&state); fprintf(stderr, "dangle: exiting\n"); return 0; }