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
| #include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/inotify.h>
#include <time.h>
//mysql
#include <mariadb/mysql.h>
int main()
{
char chemin[100] = "/tmp/";
int i=0;
int fd = inotify_init();
if (fd < 0)
perror("inotify_init() \n");
int wd = inotify_add_watch(fd, "/tmp/seb/une/", IN_CLOSE_WRITE);
if (wd < 0)
perror("inotify_add_watch \n");
FILE* fichier = NULL;
while (1)
{
char chemin_et_fichier[300] ="";
char nom_fichier[200]="";
char tampon[200];
if(read(fd, tampon, 200)==-1) {
printf("erreur %s\n", strerror(errno));
}
;
strcpy(nom_fichier,((struct inotify_event*)tampon)->name);//copie du chemin dans la variable chemin
strcat(chemin_et_fichier, chemin);
strcat(chemin_et_fichier, nom_fichier);
if(i==250)
{
//connexion mysql
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "***";
char *password = "***";
char *database = "BD";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
char insertion[300];
sprintf(insertion, "SELECT * FROM BD WHERE debut < %d ORDER BY debut DESC LIMIT 1", (int)time(NULL));
if (mysql_query(conn, insertion)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("%s %s %s %s \n", row[0],row[1],row[2],row[3]);
}
mysql_free_result(res);
mysql_close(conn);
i=0;
}else{
i=i+1;
}
remove(chemin_et_fichier);
}
return (0);
} |
Partager