[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: So you think you are (or wanna be) a hacker



On Thu, Aug 12, 2004 at 04:38:25PM -0400, Jason Rennie wrote:
> Something a bit safer...
> 
> char *home = getenv("HOME");
> if (home == NULL || cfgfile == NULL) hittheuseronthehead();
> int sz = strlen(home) + strlen(cfgfile) + 2;
> char *configfile = malloc(sizeof(char)*sz);
> sprintf(configfile,"%s/%s",home,cfgfile);
> 
> I'm sure someone can do better (and be more creative :)

Using a glibc extension, how about:

   char *configfile;

   asprintf (&configfile, "%s/%s", getenv ("HOME"), cfgfile);

aprintf auto-mallocs a buffer of the right size.
Be sure to free()!



Reply to: