1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 | /*
* Name: HTML creation functions
*
* Purpose: Create a nice HTML page with all the statistics
*
* Author: Pedro de Oliveira <falso@rdk.homeip.net>
*
* Copyright (c) 2004-2011 Pedro de Oliveira ( falso@rdk.homeip-net )
*
* This file is part of aMule.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include "html.h"
#include "functions.h"
#include "version.h"
int create_html(char *stats[20], char *lines[6], char template[120], char *path_for_html)<--- Parameter 'stats' can be declared as const array<--- Parameter 'lines' can be declared as const array<--- Parameter 'path_for_html' can be declared as pointer to const
{
/* Strings */
char *path = NULL;
char version[25], upload[25], download[25];
char *search[] = {"#VERSION#", "#CLIENT#", "#NICK#", "#UPLOADRATE#" ,
"#DOWNLOADRATE#" , "#QUEUE#" , "#NUMSHARE#" , "#SESSIONUP#" ,
"#SESSIONDOWN#" , "#TOTALUP#", "#TOTALDOWN#" , "#SERVER#" , "#IP#",
"#PORT#" };
snprintf(version, 25, "cas %s", CAS_VERSION);
snprintf(upload, 25, "%s kB/s", stats[7]);
snprintf(download, 25, "%s kB/s", stats[6]);
char *repl[] = { version , lines[0] , stats[10] , upload , download ,
stats[8] , stats[9] , stats[15] , stats[14] , stats[12] , stats[11] ,
stats[1] , stats[2] , stats[3] };
/* get some memory to read the template into */
int fdTmpl;
if ((fdTmpl = open(template, O_RDONLY)) < 0)
{
printf("\n\n%s\n",template);
perror("Could not open file");
exit (43);
}
struct stat sb;
if (fstat(fdTmpl, &sb) < 0)
{
perror("Could not stat file");
exit(43);
}
close(fdTmpl);
/* 2 times the size of the template should be enough */
/* st_size is defined as off_t, but size_t seems more reasonable */
size_t size = sb.st_size*2;
char *mem = calloc(size, 1);
if (NULL == mem)
{
perror("Could not calloc\n");
exit(44);
}
/* read the template into the memory */
size_t len = 0;
int ler;
FILE *fTmpl = fopen(template,"r");
while ((ler=fgetc(fTmpl)) != EOF && len+1 < size)
{
mem[len++] = ler;
}
fclose(fTmpl);
/* printf ("HTML: %s\n", mem); */
int t;
for (t=0; t<=13; t++)
{
/* replace the special tags */
replace(mem, search[t], repl[t]);
}
/* printf("FINAL: %s\n",mem); */
path = get_amule_path("aMule-online-sign.html", 0, path_for_html);
if (NULL == path)
{
perror("could not get the HTML path\n");
free(mem);
return 0;
}
FILE *fHTML = NULL;
if ((fHTML = fopen(path, "w")) == NULL)
{
perror("Unable to create file\n");
free(path);
free(mem);
exit(44);
}
free(path);
fprintf(fHTML, "%s", mem);
fclose(fHTML);
free(mem);
printf("HTML file created.\n");
return 1;
}
|