Browse Source

Readme

master
alistair 4 years ago
parent
commit
1b90c999da
  1. 14
      README.md
  2. 20
      lipsum.c

14
README.md

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
# Lipsum.c
Generates a random string of words of given length using UNIX words file.
## Dependencies
- Unix words file at `/usr/share/dict/words` `or /usr/dict/words`.
This should exist in most UNIX-like operating systems.
## Usage
```
lipsum <num words>
```

20
lipsum.c

@ -37,8 +37,8 @@ int readline(FILE* in, char **out) { @@ -37,8 +37,8 @@ int readline(FILE* in, char **out) {
}
}
char **read_word_file(char *file, int *num_ret) {
FILE *words = fopen(file, "r");
char **read_word_file(FILE *words, int *num_ret) {
rewind(words);
int listlen = 10;
char **wordlist = calloc(listlen, sizeof(char *));
@ -67,7 +67,9 @@ int main (int argc, char **argv) { @@ -67,7 +67,9 @@ int main (int argc, char **argv) {
fprintf(stderr, "Usage: lipsum <num words>\n");
return 1;
}
int num;
if (argc == 1) {
num = 50;
} else {
@ -81,12 +83,22 @@ int main (int argc, char **argv) { @@ -81,12 +83,22 @@ int main (int argc, char **argv) {
unsigned int rand_seed = time(NULL);
int len;
char **wordlist = read_word_file("/usr/share/dict/words", &len);
FILE *words = fopen("/usr/share/dict/words", "r");
if (!words) {
words = fopen("/usr/dict/words", "r");
}
if (!words) {
fprintf(stderr, "No words file found\n");
return 1;
}
char **wordlist = read_word_file(words, &len);
fclose(words);
for (int i = 0; i < num; i++) {
int index = rand_r(&rand_seed) % len;
printf("%s ", wordlist[index]);
}
printf("\n");
}

Loading…
Cancel
Save