Skip to content

Instantly share code, notes, and snippets.

@junjihashimoto
Created August 29, 2013 00:46
Show Gist options
  • Save junjihashimoto/6373106 to your computer and use it in GitHub Desktop.
Save junjihashimoto/6373106 to your computer and use it in GitHub Desktop.
strf
#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