I have a function irc_sendline
that can be called like printf
can
irc_sendline(s, "A strange game.\nThe only %s is not to play.", "winning move");
It works perfectly, but I'm not happy with its implementation:
int irc_sendline(irc *iobj, char *msg, ...)
{
char tmp_msg[BUFSIZE], fmsg[BUFSIZE];
va_list args;
int len;
va_start(args, msg);
strncpy(tmp_msg, msg, BUFSIZE);
strncat(tmp_msg, "\r\n", BUFSIZE);
len = vsnprintf(fmsg, BUFSIZE, tmp_msg, args);
len = send(iobj->fd, fmsg, len, 0);
return len;
}
You see, I'm using 2 "temporary" buffers here, because I first have to copy the original message from the function arguments to a temporary buffer to append "\r\n" to it, and then copy that temporary buffer to another temporary buffer to do the actual formatting with the arguments supplied from the function call, and only THEN I can send the stuff on its way.
How could I make this cleaner, better?
Thanks for all the input here, I thought my only problem was the mess in there, but it was actually a ticking timebomb! My new function looks like this:
int irc_sendline(irc *iobj, char *msg, ...)
{
char buffer[BUFSIZE];
va_list args;
int res_str_len;
int sent;
va_start(args, msg);
res_str_len = vsnprintf(buffer, BUFSIZE, msg, args);
sent = send(iobj->fd, buffer, res_str_len, 0);
sent += send(iobj->fd, "\r\n", 2, 0);
return sent;
}
If I could, I'd accept multiple answers here, but meh.