PDA

View Full Version : [c]Strutture


Luc@s
09-11-2003, 08:54
#ifndef PAK_H
#define PAK_H

#include "include.h"

typedef char bool;

typedef struct
{
char content[PAK_N]; /* the content of pak */
bool flag[FLAG_N]; /* the pak flag */
char sign[10]; /* the verification signature */

} Pak;

/* prototype */
void setSign(Pak * ptr, const char sign[10]);
bool cntSign(Pak * ptr);
void setContent(Pak * ptr, char content[PAK_N]);
int send(Pak * ptr, int mode);
void rec(Pak * ptr, char buf[PAK_N]);
void log(char * msg);
void controlChecksum(char * ck, bool * control);

/* implementation */
void setSign(Pak * ptr, const char sign[10])
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return;
if((strlen(sign) > 10) || (strlen(sign) < 10))
return;
strcpy(ptr->sign, sign);
}

bool cntSign(Pak * ptr)
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return FALSE;
}


void setContent(Pak * ptr, char content[PAK_N])
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return;
short i;
for(i = 0; i<PAK_N; i++)
{
ptr->content[i] = content[i];
}
}

int send(Pak * ptr, int mode)
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return -1;
if(strlen(ptr->content) < PAK_N)
return -1;
setContent(ptr, ptr->content);
ptr->flag[0] = TRUE; /* the dimension OK flag */
switch(mode)
{
case SERVER:
ptr->flag[1] = SERVER; /* the pak type flag */
break;
case CLIENT:
ptr->flag[1] = CLIENT;
break;
case REQUEST:
ptr->flag[1] = REQUEST;
break;
default:
ptr->flag[1] = ERROR;
break;
}
if((ptr->flag[0] == TRUE) && (ptr->flag[1] != ERROR))
{
setSign((Pak *) ptr->sign, CHECK);
}else{
return -1;
}
/*
send to serial port
*/
return 0;

}

void rec(Pak * ptr, char buf[PAK_N])
{
/*
receive to serial port
*/
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return;
strcpy(buf, ptr->content);
}
/*
* Log Format:
* # DATE #
* # FLAG #
* # CONTENT #
*/
void log(char * msg)
{
FILE * log = fopen("app.log", "a+");
if(log == NULL)
return;
fprintf(log, "#%s#\n", msg);
fclose(log);
}

void controlChecksum(char * ck, bool * control)
{
if(strcmp(ck, CHECK) == 0)
{
control = (bool *) 0;
}else{
control = (bool *) 1;
}
}

#endif



Se provo ad usare la funz send() mi restituisce -123343
Help me :muro: :muro:

Luc@s
09-11-2003, 13:45
ap:(

Darkman
10-11-2003, 11:40
A me il tuo programma compilato da una lista di errori interminabili....

ilsensine
10-11-2003, 12:19
Innanzitutto non usare gli include guard (#ifndef...#define...#endif) dentro codice c (il tuo listato mi sembra codice c, non un header). Poi, non usare nomi di funzioni di sistema per le tue funzioni (tipo send e log). Infine, la tua send restituisce solo 0 oppure -1, è impossibile che ritorni -123343.

Luc@s
10-11-2003, 14:05
Originariamente inviato da ilsensine
Innanzitutto non usare gli include guard (#ifndef...#define...#endif) dentro codice c (il tuo listato mi sembra codice c, non un header). Poi, non usare nomi di funzioni di sistema per le tue funzioni (tipo send e log). Infine, la tua send restituisce solo 0 oppure -1, è impossibile che ritorni -123343.

eppire lo fa:eek:

ilsensine
10-11-2003, 14:42
Originariamente inviato da Luc@s
eppire lo fa:eek:
Due possibili cause:
1) Buffer overflow da qualche parte
2) Chi chiama la send chiama effettivamente la send di sistema, non quella che hai scritto tu. Cambiagli nome per evitare conflitti.

Kleidemos
10-11-2003, 14:44
Originariamente inviato da ilsensine
Due possibili cause:
1) Buffer overflow da qualche parte
2) Chi chiama la send chiama effettivamente la send di sistema, non quella che hai scritto tu. Cambiagli nome per evitare conflitti.

direi la 2 perche sulla prima nn penso sia possibile(ho fatto molti controlli:D )

ilsensine
10-11-2003, 14:45
Originariamente inviato da Kleidemos
direi la 2 perche sulla prima nn penso sia possibile(ho fatto molti controlli:D )
Luc@s o Kleidemos che dir si voglia, che ne diresti di usare un solo nick?

Luc@s
10-11-2003, 14:47
hai ragione!
Skusa:cry:

ilsensine
10-11-2003, 14:55
Originariamente inviato da Luc@s
hai ragione!
Skusa:cry:
Quale dei due vuoi mantenere? :huh:

Luc@s
10-11-2003, 15:03
Originariamente inviato da ilsensine
Quale dei due vuoi mantenere? :huh:

Luc@s!
Cancellami l'altro:D

ilsensine
10-11-2003, 15:09
Ok fuori uno :D
Hai altri nick o me li devo cercare da solo? ;)

Luc@s
10-11-2003, 15:12
Originariamente inviato da ilsensine
Ok fuori uno :D
Hai altri nick o me li devo cercare da solo? ;)

Nessun altro, giuro
:)

ilsensine
10-11-2003, 15:16
Ok tornando al tuo problema: innanzitutto rimuovi i nomi di funzioni riservate (send e log) e guarda se cambia qualche cosa...

Luc@s
10-11-2003, 16:47
mi da lo stesso numero :O


#include "include.h"

typedef char bool;

typedef struct
{
char content[PAK_N]; /* the content of pak */
bool flag[FLAG_N]; /* the pak flag */
char sign[10]; /* the verification signature */

} Pak;

/* prototype */
void setSign(Pak * ptr, const char sign[10]);
bool cntSign(Pak * ptr);
void setContent(Pak * ptr, char content[PAK_N]);
int send_pak(Pak * ptr, short mode);
void rec_pak(Pak * ptr, char buf[PAK_N]);
void aplog(char * msg);
void controlChecksum(char * ck, bool * control);

/* implementation */
void setSign(Pak * ptr, const char sign[10])
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return;
if((strlen(sign) > 10) || (strlen(sign) < 10))
return;
strcpy(ptr->sign, sign);
}

bool cntSign(Pak * ptr)
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return FALSE;
}


void setContent(Pak * ptr, char content[PAK_N])
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return;
short i;
for(i = 0; i<PAK_N; i++)
{
ptr->content[i] = content[i];
}
}

int send_pak(Pak * ptr, short mode)
{
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return -1;
if(strlen(ptr->content)+1 < PAK_N)
return -1;
ptr->flag[0] = TRUE; /* the dimension OK flag */
switch(mode)
{
case SERVER:
ptr->flag[1] = SERVER; /* the pak type flag */
break;
case CLIENT:
ptr->flag[1] = CLIENT;
break;
case REQUEST:
ptr->flag[1] = REQUEST;
break;
default:
ptr->flag[1] = ERROR;
break;
}
if((ptr->flag[0] == TRUE) && (ptr->flag[1] != ERROR))
{
setSign((Pak *) ptr->sign, CHECK);
}else{
return -1;
}
/*
send to serial port
*/
return 0;

}

void rec_pak(Pak * ptr, char buf[PAK_N])
{
/*
receive to serial port
*/
#ifdef DEBUG
assert(ptr != NULL);
#endif
if(ptr == NULL)
return;
strcpy(buf, ptr->content);
}
/*
* Log Format:
* # DATE #
* # FLAG #
* # CONTENT #
*/
void aplog(char * msg)
{
FILE * log = fopen("app.log", "a+");
if(log == NULL)
return;
fprintf(log, "#%s#\n", msg);
fclose(log);
}

void controlChecksum(char * ck, bool * control)
{
if(strcmp(ck, CHECK) == 0)
{
control = (bool *) 0;
}else{
control = (bool *) 1;
}
}




#include "include.h"
#include "pak.h"

extern soc_S;

int main()
{
#ifdef DEBUG
printf("Debug Mode CLIENT\n");
#endif
char sc;
time_t ltime;
do
{
Pak * p;
strcpy(p->content, "c");
int err = send_pak(p, CLIENT);
puts("\nInsert 0 to exit\n");
printf("Err: %d \n", &err);
scanf("%d", &sc);
}while(sc != 0);
return 0;
}

ilsensine
10-11-2003, 19:13
printf("Err: %d \n", &err);

Non "&err", ma semplicemente "err"