I have a irc_sendline function that can be called as printf can
irc_sendline(s, "A strange game.\nThe only %s is not to play.", "winning move");
It works fine, 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 use 2 "temporary" buffers here, because first I need to copy the original message from the function arguments to a temporary buffer to add "\ r \ n" to it, and then copy this temporary buffer to another temporary buffer for actual formatting with the arguments provided from the function call, and only THEN can I send stuff in my path.
How can I make it cleaner, better?
Thanks for all the input here, I thought my only problem was the mess there, but it was actually a ticking bomb! My new feature 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 would accept a few answers here, but meh.