* Now checks if /etc/fortunes is a directory, and picks a random file out of
that. This fixes bug #1475. * Now accepts file arguments (will pick a random one out of them) as fortune sources. * Minor cleanup and simplifications. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@23788 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+132
-47
@@ -1,81 +1,166 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright 2002-2004, The OpenBeOS Team. All rights reserved.
|
* Copyright 2002-2008, Haiku. All rights reserved.
|
||||||
** Distributed under the terms of the OpenBeOS License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <errno.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <OS.h>
|
#include <OS.h>
|
||||||
|
|
||||||
|
|
||||||
#define FORTUNES "/etc/fortunes"
|
#define FORTUNES "/etc/fortunes"
|
||||||
|
|
||||||
int
|
|
||||||
main(void)
|
static int
|
||||||
|
choose_file(const char *path)
|
||||||
{
|
{
|
||||||
|
struct dirent *dirent;
|
||||||
|
int count = 0;
|
||||||
|
int chosen;
|
||||||
|
|
||||||
|
DIR *dir = opendir(path);
|
||||||
|
if (dir == NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// count directory entries
|
||||||
|
|
||||||
|
while ((dirent = readdir(dir)) != NULL) {
|
||||||
|
if (dirent->d_name[0] == '.')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// choose and open entry
|
||||||
|
|
||||||
|
chosen = rand() % count;
|
||||||
|
count = 0;
|
||||||
|
rewinddir(dir);
|
||||||
|
|
||||||
|
while ((dirent = readdir(dir)) != NULL) {
|
||||||
|
if (dirent->d_name[0] == '.')
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (chosen <= count) {
|
||||||
|
char name[PATH_MAX];
|
||||||
int fd;
|
int fd;
|
||||||
int rc;
|
|
||||||
char *buf;
|
// build full path
|
||||||
unsigned i;
|
strlcpy(name, path, sizeof(name));
|
||||||
unsigned found;
|
strlcat(name, "/", sizeof(name));
|
||||||
|
strlcat(name, dirent->d_name, sizeof(name));
|
||||||
|
|
||||||
|
fd = open(name, O_RDONLY);
|
||||||
|
if (fd >= 0) {
|
||||||
|
closedir(dir);
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
char *file = FORTUNES;
|
||||||
|
int fd;
|
||||||
|
char *buffer;
|
||||||
|
unsigned start, i;
|
||||||
|
unsigned count;
|
||||||
struct stat stat;
|
struct stat stat;
|
||||||
|
|
||||||
fd = open(FORTUNES, O_RDONLY, 0);
|
srand(system_time() % INT_MAX);
|
||||||
|
|
||||||
|
if (argc > 1) {
|
||||||
|
// if there are arguments, choose one randomly
|
||||||
|
file = argv[1 + (rand() % (argc - 1))];
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = open(file, O_RDONLY, 0);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
printf("Couldn't open %s: %s\n", FORTUNES, strerror(errno));
|
fprintf(stderr, "Couldn't open %s: %s\n", file, strerror(errno));
|
||||||
return -1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = fstat(fd, &stat);
|
if (fstat(fd, &stat) < 0) {
|
||||||
if (rc < 0) {
|
fprintf(stderr, "stat() failed: %s\n", strerror(errno));
|
||||||
printf("stat() failed: %s\n", strerror(errno));
|
return 1;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = malloc(stat.st_size + 1);
|
if (S_ISDIR(stat.st_mode)) {
|
||||||
rc = read(fd, buf, stat.st_size);
|
|
||||||
if (rc < 0) {
|
|
||||||
printf("Could not read from fortune file: %s\n", strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf[stat.st_size] = 0;
|
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
found = 0;
|
fd = choose_file(file);
|
||||||
for (i = 0; i < stat.st_size; i++) {
|
if (fd < 0) {
|
||||||
if (!strncmp(buf + i, "#@#", 3))
|
fprintf(stderr, "Could not find any fortune file.\n");
|
||||||
found += 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found > 0)
|
if (fstat(fd, &stat) < 0) {
|
||||||
found = 1 + ((system_time() + 3) % found);
|
fprintf(stderr, "stat() failed: %s\n", strerror(errno));
|
||||||
else {
|
return 1;
|
||||||
printf("Out of cookies...\n");
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = malloc(stat.st_size + 1);
|
||||||
|
if (buffer == NULL) {
|
||||||
|
fprintf(stderr, "Not enough memory.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (read(fd, buffer, stat.st_size) < 0) {
|
||||||
|
fprintf(stderr, "Could not read from fortune file: %s\n",
|
||||||
|
strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < stat.st_size; i++) {
|
buffer[stat.st_size] = '\0';
|
||||||
if (!strncmp(buf + i, "#@#", 3))
|
close(fd);
|
||||||
found -= 1;
|
|
||||||
|
|
||||||
if (found == 0) {
|
// count fortunes
|
||||||
unsigned j;
|
|
||||||
|
|
||||||
for (j = i + 1; j < stat.st_size; j++) {
|
count = 0;
|
||||||
if (!strncmp(buf + j, "#@#", 3))
|
for (i = 0; i < stat.st_size - 2; i++) {
|
||||||
buf[j] = 0;
|
if (!strncmp(buffer + i, "\n%\n", 3)) {
|
||||||
}
|
count++;
|
||||||
|
i += 3;
|
||||||
printf("%s\n", buf + i + 3);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!count) {
|
||||||
|
printf("Out of cookies...\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
count = rand() % count;
|
||||||
|
start = 0;
|
||||||
|
|
||||||
|
// find beginning & end
|
||||||
|
for (i = 0; i < stat.st_size - 2; i++) {
|
||||||
|
if (!strncmp(buffer + i, "\n%\n", 3)) {
|
||||||
|
if (count-- <= 0) {
|
||||||
|
buffer[i] = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
start = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
puts(buffer + start);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user