Created
August 29, 2013 00:46
-
-
Save junjihashimoto/6373106 to your computer and use it in GitHub Desktop.
strf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdarg.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
char* | |
vstrf(char* format,va_list aq){ | |
int n; | |
int buflen=64; | |
char* buf=malloc(sizeof(char)*buflen); | |
RETRY: | |
n=vsnprintf(buf,buflen,format,aq); | |
if(n==buflen){ | |
int newbuflen=buflen*2; | |
char* newbuf =realloc(buf,newbuflen); | |
buf=newbuf; | |
buflen=newbuflen; | |
goto RETRY; | |
} | |
return buf; | |
} | |
char* | |
strf(char* format,...){ | |
va_list aq; | |
va_start(aq,format); | |
char* buf=vstrf(format,aq); | |
va_end(aq); | |
return buf; | |
} | |
int | |
systemf(char* format,...){ | |
va_list aq; | |
va_start(aq,format); | |
char* buf=vstrf(format,aq); | |
va_end(aq); | |
int n=system(buf); | |
free(buf); | |
return n; | |
} | |
int | |
main(){ | |
systemf("ls %s","/tmp"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment