Altre correzioni, con esempi sul server fortunes e relativa "demonizzazzione"
[gapil.git] / sources / FortuneParse.c
1 /* FortuneParse.c
2  * 
3  * Copyright (C) 2002 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Routine FortuneParse
22  * parse fortunes from a fortune file
23  *
24  * Author: Simone Piccardi
25  * Aug. 2002
26  *
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
30  *
31  * $Id: FortuneParse.c,v 1.3 2003/01/12 16:10:07 piccardi Exp $
32  *
33  ****************************************************************/
34 /* 
35  * Include needed headers
36  */
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>       /*  */
45
46 #include "macros.h"
47
48 /* Subroutines declaration */
49 extern void usage(void);
50
51 int FortuneParse(char *file, char **fortune, int n)
52 {
53 /* Variables definition */
54     FILE *fortunefile;
55     char line[80];
56     int i, len;
57     /* 
58      *  fortune file scanning, read string in memory 
59      */
60     fortunefile = fopen(file,"r");
61     if (fortunefile == NULL) {                       /* on open error exit */
62         printf("On file %s\n", file);
63         perror("Cannot open file");
64         exit(-1);
65     }
66     i = 0;
67     do {
68         if (!fgets(line, 80, fortunefile)) {
69             if (feof(fortunefile)) break;
70             perror("Read error");
71             exit(-1);
72         }
73         debug("i=%d, line=%s", i, line);
74         if (line[0]=='%') {
75             if (fortune[i]!=NULL) i++;
76             continue;
77         }
78         len = strlen(line) + 1;
79         if (fortune[i]==NULL) {
80             fortune[i] = malloc(len);
81             strncpy(fortune[i], line, len);
82         } else {
83             fortune[i] = realloc(fortune[i], strlen(fortune[i])+len+1);
84             strncat(fortune[i], line, len);
85         }
86     } while (i<n);
87     return i;
88 }