Tuesday, July 28, 2009

C functions question?

I'm trying to make a function that will allow me to send data to IRC





It has to be an open ended function (like printf)





however, when i try it this way.. the bot has trouble sending the data and connecting. Can someone help me?








The function:





int sendirc(int sockdesc, char *what, ...) {


send(sockdesc, what, strlen(what), 0);


send(sockdesc, "\r\n", strlen("\r\n"), 0);


return 0;


}





the usage of the function:





sendirc(sockdesc, "NICK %s", BOTNICK)


sendirc(sockdesc, "USER %s bot bot :%s", BOTUSER, BOTNAME);





BOTNICK BOTNAME and BOTUSER are all defined.





so the syntax should be sendirc(SOCKETDESCRIPTOR, STRING TO SEND, ...)

C functions question?
You need to use sprintf to translate the "%s" in the string into a passed argument.





for example in the function sendirc could be written as





int sendirc(int sockdesc, char *what, char *a1, char *a2, char *a3, char *a4, char *a5)


{


extern char Buf[2048]; /* defined off of the stack */


sprintf (Buf, what, a1, a2, a3, a4, a5); /* allow up to 5 arguments (just a stack copy regardless if the arguments were really passed or not )*/


strcat(Buf, "\r\n");


send(sockdesc, Buf, strlen(Buf), 0);


return 0;


}





You will have to limit yourself with what you pass to this function to prevent overflowing the Buf[] array


No comments:

Post a Comment