Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void sockputs(char* str, int sock) {
- write(sock, str, strlen(str));
- }
- void sockprintf(int sock, const char *format, ...) {
- //basically GNU asprintf
- const unsigned buf_size = 16;
- char* buf = (char*) malloc(sizeof(char) * buf_size);
- va_list vl;
- //first, try to vsnprintf to a buffer of length 16
- va_start(vl, format);
- unsigned len = vsnprintf(buf, buf_size, format, vl);
- va_end(vl);
- //if that wasn't enough, allocate more memory and try again
- if (len >= buf_size) {
- buf = (char*) realloc(buf, sizeof(char) * (len + 1));
- va_start(vl, format);
- vsnprintf(buf, len + 1, format, vl);
- va_end(vl);
- }
- //output buf to the socket
- sockputs(buf, sock);
- //free the buffer
- free(buf);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement