3 * Copyright (C) 2002 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
21 * Routine FortuneParse
22 * parse fortunes from a fortune file
24 * Author: Simone Piccardi
27 * Usage: int FortuneParse(char *file, char ** fortune, int n);
28 * Read n fortunes from fortune file file, and put it into the
29 * string array fortune
31 * $Id: FortuneParse.c,v 1.1 2002/08/18 10:34:10 piccardi Exp $
33 ****************************************************************/
35 * Include needed headers
37 #include <sys/types.h> /* predefined types */
38 #include <sys/stat.h> /* */
39 #include <unistd.h> /* include unix standard library */
40 #include <stdio.h> /* include standard I/O library */
41 #include <stdlib.h> /* standard library */
42 #include <string.h> /* ANSI C standard string */
43 #include <errno.h> /* error definitions */
44 #include <fcntl.h> /* */
46 /* Subroutines declaration */
47 extern void usage(void);
49 int FortuneParse(char *file, char **fortune, int n)
51 /* Variables definition */
56 * fortune file scanning, read string in memory
58 fortunefile = fopen(file,"r");
59 if (fortunefile == NULL) { /* on open error exit */
60 perror("Opening fortune file");
65 if (!fgets(line, 80, fortunefile)) {
66 if (feof(fortunefile)) break;
71 if (fortune[i]!=NULL) i++;
74 if (fortune[i]==NULL) {
75 fortune[i] = (char *) malloc(strlen(line)+1);
76 strncpy(fortune[i], line, strlen(line)+1);
78 fortune[i] = (char *) realloc(fortune[i], strlen(line)+1);
79 strncat(fortune[i], line, strlen(line)+1);