- FILE *f;
- char line[1024];
- char cmd[1024], path[256];
- double est;
- char *cp;
- int linenum;
- entropy_source_t *entcmd;
- int num_cmds = 64;
- int cur_cmd = 0;
-
- f = fopen(cmdfilename, "r");
- if (!f) {
- fatal("couldn't read entropy commands file %.100s: %.100s",
- cmdfilename, strerror(errno));
- }
-
- linenum = 0;
-
- entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
- memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
-
- while (fgets(line, sizeof(line), f)) {
- linenum++;
-
- /* skip leading whitespace, test for blank line or comment */
- cp = line + strspn(line, WHITESPACE);
- if ((*cp == 0) || (*cp == '#'))
- continue; /* done with this line */
-
- switch (*cp) {
- int arg;
- char *argv;
-
- case '"':
- /* first token, command args (incl. argv[0]) in double quotes */
- cp = strtok(cp, "\"");
- if (cp==NULL) {
- error("missing or bad command string, %.100s line %d -- ignored",
- cmdfilename, linenum);
- continue;
- }
- strncpy(cmd, cp, sizeof(cmd));
- /* second token, full command path */
- if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
- error("missing command path, %.100s line %d -- ignored",
- cmdfilename, linenum);
- continue;
- }
- if (strncmp("undef", cp, 5)==0) /* did configure mark this as dead? */
- continue;
-
- strncpy(path, cp, sizeof(path));
- /* third token, entropy rate estimate for this command */
- if ( (cp = strtok(NULL, WHITESPACE)) == NULL) {
- error("missing entropy estimate, %.100s line %d -- ignored",
- cmdfilename, linenum);
- continue;
- }
- est = strtod(cp, &argv);/* FIXME: (ATL) no error checking here */
-
- /* end of line */
- if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
- error("garbage at end of line %d in %.100s -- ignored",
- linenum, cmdfilename);
- continue;
- }
-
- /* split the command args */
- cp = strtok(cmd, WHITESPACE);
- arg = 0; argv = NULL;
- do {
- char *s = (char*)xmalloc(strlen(cp)+1);
- strncpy(s, cp, strlen(cp)+1);
- entcmd[cur_cmd].args[arg] = s;
- arg++;
- } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
- if (strtok(NULL, WHITESPACE))
- error("ignored extra command elements (max 5), %.100s line %d",
- cmdfilename, linenum);
-
- /* copy the command path and rate estimate */
- entcmd[cur_cmd].path = (char *)xmalloc(strlen(path)+1);
- strncpy(entcmd[cur_cmd].path, path, strlen(path)+1);
- entcmd[cur_cmd].rate = est;
- /* initialise other values */
- entcmd[cur_cmd].sticky_badness = 1;
-
- cur_cmd++;
-
- /* If we've filled the array, reallocate it twice the size */
- /* Do this now because even if this we're on the last command,
- we need another slot to mark the last entry */
- if (cur_cmd == num_cmds) {
- num_cmds *= 2;
- entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
- }
- break;
-
- default:
- error("bad entropy command, %.100s line %d", cmdfilename,
- linenum);
- continue;
- }
- }
-
- /* zero the last entry */
- memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
- /* trim to size */
- entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
-
- debug("loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
-
- return (cur_cmd >= MIN_ENTROPY_SOURCES);
-}
-
-
-#endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
-
-#if defined(EGD_SOCKET) || defined(RANDOM_POOL)