ragazzi vorrei migliorare un mio programma e accetterò tutti i commenti e segnalazioni per un miglio funzionamento...
codice:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#define NOME_FILE "uc.bin"
#define ERRORE_USCITA "C\'è un solo utente connesso"
int verificaIp(char* IPTmp);
struct strDati {
char ipUtente[80];
int oraAccesso;
};
typedef struct strContDati {
struct strDati impoUtente;
struct strContDati* pStesso;
} impoUtenti;
void liberaMemoria(impoUtenti*);
FILE* fp=NULL;
int contaUtenti=0;
int verificaUtente=0;
int main(int argc, char *argv[]) {
char* ipUtenteTmp=getenv("REMOTE_ADDR"); // "127.0.0.1";
int oraAccessoTmp=time('\0');
impoUtenti* Lista=(impoUtenti*)malloc(sizeof(impoUtenti));
impoUtenti* cLista=Lista;
printf("content-type:text/html\n\n");
if(!ipUtenteTmp) {
printf("%s", ERRORE_USCITA);
free(Lista);
exit(1);
}
fp=fopen(NOME_FILE, "rb");
if(fp==NULL) {
printf("%s", ERRORE_USCITA);
free(Lista);
exit(1);
}
while(!feof(fp)) {
fread(Lista, sizeof(impoUtenti), 1, fp);
if(verificaIp(Lista->impoUtente.ipUtente)) {
strcpy(Lista->impoUtente.ipUtente, Lista->impoUtente.ipUtente);
if(!strcmp(Lista->impoUtente.ipUtente, ipUtenteTmp)) {
Lista->impoUtente.oraAccesso=oraAccessoTmp;
verificaUtente=1;
}
else {
Lista->impoUtente.oraAccesso=Lista->impoUtente.oraAccesso;
}
if(!feof(fp)) {
Lista->pStesso=(impoUtenti*)malloc(sizeof(impoUtenti));
Lista=Lista->pStesso;
}
else {
Lista->pStesso=NULL;
}
}
else {
Lista->pStesso=NULL;
}
}
fclose(fp);
if(!verificaUtente) {
strcpy(Lista->impoUtente.ipUtente, ipUtenteTmp);
Lista->impoUtente.oraAccesso=oraAccessoTmp;
Lista->pStesso=NULL;
}
Lista=cLista;
fp=NULL;
fp=fopen(NOME_FILE, "wb");
while(Lista!=NULL) {
if(Lista->impoUtente.oraAccesso>(oraAccessoTmp-600) && verificaIp(Lista->impoUtente.ipUtente)) {
fwrite(Lista, sizeof(impoUtenti), 1, fp);
contaUtenti++;
}
Lista=Lista->pStesso;
}
fclose(fp);
if(contaUtenti>1) {
printf("Utenti connessi %d", contaUtenti);
}
else {
printf("C\'è un solo utente connesso");
}
Lista=cLista;
liberaMemoria(Lista);
// system("pause");
return 0;
}
// verifica ip
int verificaIp(char* IPTmp) {
int contaPunti=0;
int x=0;
for(x=0; IPTmp[x]; x++) {
if(IPTmp[x]=='.') {
contaPunti++;
}
}
if(contaPunti==3) {
return 1;
}
else {
return 0;
}
}
// libera memoria
void liberaMemoria(impoUtenti* pTmp) {
impoUtenti* tLista;
while(pTmp!=NULL) {
tLista=pTmp->pStesso;
free(pTmp);
pTmp=tLista;
}
}