-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,39 @@ | ||
#define _POSIX_C_SOURCE 200809L | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <assert.h> | ||
#include <unistd.h> | ||
|
||
void flipline(void) { | ||
char* line = NULL; | ||
size_t n; | ||
ssize_t m; | ||
while ((m = getline(&line, &n, stdin)) > 0) { | ||
int i = rand() % m; | ||
int j = rand() % 8; | ||
line[i] ^= i<<j; | ||
fputs(line, stdout); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
assert(argc==2); | ||
FILE* f = fopen(argv[1], "r+"); | ||
srand(getpid()); | ||
assert(f != NULL); | ||
assert(fseek(f, 0, SEEK_END) == 0); | ||
int size = ftell(f); | ||
if (size == 0) return 0; | ||
int i = rand() % size; | ||
int j = rand() % 8; | ||
assert(fseek(f, i, SEEK_SET) == 0); | ||
unsigned char c; | ||
assert(fread(&c, 1, 1, f) == 1); | ||
c ^= (1<<j); | ||
assert(fseek(f, i, SEEK_SET) == 0); | ||
assert(fwrite(&c, 1, 1, f) == 1); | ||
if (argc == 1) { | ||
flipline(); | ||
} else { | ||
assert(argc==2); | ||
FILE* f = fopen(argv[1], "r+"); | ||
assert(f != NULL); | ||
assert(fseek(f, 0, SEEK_END) == 0); | ||
int size = ftell(f); | ||
if (size == 0) return 0; | ||
int i = rand() % size; | ||
int j = rand() % 8; | ||
assert(fseek(f, i, SEEK_SET) == 0); | ||
unsigned char c; | ||
assert(fread(&c, 1, 1, f) == 1); | ||
c ^= 1<<j; | ||
assert(fseek(f, i, SEEK_SET) == 0); | ||
assert(fwrite(&c, 1, 1, f) == 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/sh | ||
|
||
exec flipbit |