Octopus
recipes.c
Go to the documentation of this file.
1/*
2 Copyright (C) 2002 M. Marques, A. Castro, A. Rubio, G. Bertsch
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18
19*/
20
21#include <config.h>
22
23#include <gsl/gsl_rng.h>
24#include <locale.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#if __has_include(<unistd.h>)
30#include <unistd.h>
31#endif
32
33#ifdef _POSIX_VERSION
34#include <dirent.h>
35#include <sys/time.h>
36#endif
37
38unsigned long int random_seed() {
39 unsigned long int seed;
40 FILE *devrandom;
41
42 if ((devrandom = fopen("/dev/urandom", "r")) == NULL) {
43#ifdef _POSIX_VERSION
44 struct timeval tv;
45 gettimeofday(&tv, 0);
46 seed = tv.tv_sec + tv.tv_usec;
47#else
48 seed = 0;
49#endif
50 } else {
51 fread(&seed, sizeof(seed), 1, devrandom);
52 fclose(devrandom);
53 }
55 return seed;
57
58void oct_print_recipe(char * dir_, char * filename) {
59
60#ifdef _POSIX_VERSION
61 char *lang, dir[512];
62 struct dirent **namelist;
63 int ii, nn;
64 gsl_rng *rng;
65
66 /* get language */
67 lang = getenv("LANG");
68 if (lang == NULL)
69 lang = "en";
70
71 strcpy(dir, dir_);
72
73 strcat(dir, "/recipes");
74
75 /* check out if lang dir exists */
76 nn = scandir(dir, &namelist, 0, alphasort);
77 if (nn < 0) {
78 printf("Directory does not exist: %s", dir);
79 return;
80 }
81
82 for (ii = 0; ii < nn; ii++)
83 if (strncmp(lang, namelist[ii]->d_name, 2) == 0) {
84 strcat(dir, "/");
85 strcat(dir, namelist[ii]->d_name);
86 break;
87 }
88
89 if (ii == nn)
90 strcat(dir, "/en"); /* default */
91
92 /* clean up */
93 for (ii = 0; ii < nn; ii++)
94 free(namelist[ii]);
95 free(namelist);
96
97 /* now we read the recipes */
98 nn = scandir(dir, &namelist, 0, alphasort);
99
100 /* initialize random numbers */
101 gsl_rng_env_setup();
102 rng = gsl_rng_alloc(gsl_rng_default);
103 gsl_rng_set(rng, random_seed());
104 ii = gsl_rng_uniform_int(rng, nn - 2);
105 gsl_rng_free(rng);
106
107 strcat(dir, "/");
108 strcat(dir, namelist[ii + 2]->d_name); /* skip ./ and ../ */
109
110 /* clean up again */
111 for (ii = 0; ii < nn; ii++)
112 free(namelist[ii]);
113 free(namelist);
114
115 strcpy(filename, dir);
116
117#else
118 printf("Sorry, recipes cannot be printed unless scandir and alphasort are "
119 "available with your C compiler.\n");
120#endif
121}
int scandir(const char *__restrict __dir, struct dirent ***__restrict __namelist, int(*__selector)(const struct dirent *), int(*__cmp)(const struct dirent **, const struct dirent **)) __attribute__((__nonnull__(1
int gettimeofday(struct timeval *__restrict __tv, void *__restrict __tz) __attribute__((__nothrow__
unsigned long int random_seed()
Definition: recipes.c:3938
int fclose(FILE *__stream)
void oct_print_recipe(char *dir_, char *filename)
Definition: recipes.c:3962
int int alphasort(const struct dirent **__e1, const struct dirent **__e2) __attribute__((__nothrow__