Compare commits

...

5 Commits
main ... main

3 changed files with 71 additions and 74 deletions

Binary file not shown.

31
compte rendutp2.txt Normal file
View File

@ -0,0 +1,31 @@
Compte rendu
daemon.c :
create_daemon() :
La complexité est de 4 car il y a les 2 boucles if et la boucle for et +1 s'il se passe rien
ping_request() : La complexite est de 2 ca il y'a une boucle if + 1 s'il ne se passe rien
send_check() : La complexite est de 3 car il y'a 2 boucles if + 1 s'il se passe rien.
check_keep_working() : La complexite est de 4 car il y a le premier if, le 2eme if et dans le 3eme if on compte les 2 else.
daemon_work() : Le copmlexité est de 3 si if(db_connect()) fonctionne et fonctionne pas et +1 s'il ne se passe rien.
db-sqlite.c :
db_connect() : La complexité est de 1 car il y'a juste le return.
db_disconnect() : La complexité est de 1 également juste le return.
insert_hourly_report : La complexité est de 1 car il n'y a aucune boucle
ping-report.c :
La complexité est de 5 car il y a 5 chemins possible dans celui-ci.
stats.c :
get_ping-from_temp_log : La complexité est de 13 car 13 chemins. (Voir l'arbre)

View File

@ -22,104 +22,70 @@
Return value :
Ping value as a string or NULL if an error occured
*/
/*@null@*/char* get_ping_from_temp_log(){
/* Variables */
/*@null@*/char* get_ping_from_temp_log() {
FILE* fd = NULL;
char* read_line = NULL;
size_t n = 0;
size_t nmatch = 2;
regex_t *p_reg;
regmatch_t* pmatch;
regex_t* p_reg = malloc(sizeof(*p_reg));
regmatch_t* pmatch = NULL;
char* ping = NULL;
int start;
int end;
size_t size_ping;
/* regex struct memory allocation */
p_reg = (regex_t *) malloc(sizeof(*p_reg));
if(p_reg == NULL){
return ping; /* NULL */
}
if (!p_reg) return NULL;
/* Open ping log file */
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
fd = fopen("/var/log/ping-report/last-ping.log", "r");
if (!fd) {
free(p_reg);
return ping; /* NULL */
return NULL;
}
/* Construct regex to get ping from log file */
if(regcomp(p_reg,"time=(.*) ms",REG_EXTENDED) != 0){
if(p_reg != NULL){
free(p_reg);
}
(void) fclose(fd);
return ping; /* NULL */
if (regcomp(p_reg, "time=(.*) ms", REG_EXTENDED) != 0) {
cleanup(fd, p_reg, NULL, NULL);
return NULL;
}
/* match info memory allocation */
pmatch = malloc(sizeof(*pmatch) * nmatch);
if(pmatch == NULL){
(void) fclose(fd);
regfree(p_reg);
free(p_reg);
return ping; /* NULL */
if (!pmatch) {
cleanup(fd, p_reg, NULL, NULL);
return NULL;
}
/* Read file */
while(getline(&read_line,&n,fd) != -1){
if(read_line == NULL){
break;
}
/* Exec regex to find ping */
if(regexec(p_reg,read_line,nmatch,pmatch,0) == 0){
/* Extract ping position from read line */
start = (int) pmatch[1].rm_so;
end = (int) pmatch[1].rm_eo;
size_ping = (size_t) (end - start);
/* ping string memory allocation */
ping = malloc(sizeof(char) * (size_ping+2));
if(ping == NULL){
free(read_line);
read_line = NULL;
n = 0;
break;
}
/* Create ping string */
(void) strncpy(ping, &read_line[start], size_ping);
ping[size_ping]='\n';
ping[size_ping+1]='\0';
/* Free memory */
while (getline(&read_line, &n, fd) != -1) {
if (regexec(p_reg, read_line, nmatch, pmatch, 0) == 0) {
ping = extract_ping(read_line, pmatch);
free(read_line);
read_line = NULL;
n = 0;
break;
}
free(read_line);
read_line = NULL;
n = 0;
}
/* free allocated memory */
regfree(p_reg);
free(p_reg);
free(pmatch);
if(read_line != NULL){
free(read_line);
cleanup(fd, p_reg, pmatch, read_line);
return ping;
}
void cleanup(FILE* fd, regex_t* p_reg, regmatch_t* pmatch, char* read_line) {
if (fd) fclose(fd);
if (p_reg) {
regfree(p_reg);
free(p_reg);
}
if (pmatch) free(pmatch);
if (read_line) free(read_line);
}
char* extract_ping(char* read_line, regmatch_t* pmatch) {
int start = (int)pmatch[1].rm_so;
int end = (int)pmatch[1].rm_eo;
size_t size_ping = (size_t)(end - start);
char* ping = malloc(sizeof(char) * (size_ping + 2));
if (ping) {
strncpy(ping, &read_line[start], size_ping);
ping[size_ping] = '\n';
ping[size_ping + 1] = '\0';
}
(void) fclose(fd);
/* ping may be null, then it must mean that the ping request was lost */
return ping;
}