X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=common%2Futils.c;h=bb297818a036e9fccc73df8dc3ef8512b820eea0;hb=e7e964564de02c1e2e73eb30fd72bf92a8526fd4;hp=cdf2bc6de0bf3405a2925e6dbdb562e7fdbfbea0;hpb=8b17e919cee63400e6de2c5f699c0a88d226b7e6;p=doldaconnect.git diff --git a/common/utils.c b/common/utils.c index cdf2bc6..bb29781 100644 --- a/common/utils.c +++ b/common/utils.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include +#include #include #include #include @@ -79,14 +79,19 @@ char *vsprintf2(char *format, va_list al) { int ret; char *buf; + va_list al2; - ret = vsnprintf(NULL, 0, format, al); + va_copy(al2, al); + ret = vsnprintf(NULL, 0, format, al2); + va_end(al2); if((buf = malloc(ret + 1)) == NULL) { LOGOOM(ret + 1); return(NULL); } - vsnprintf(buf, ret + 1, format, al); + va_copy(al2, al); + vsnprintf(buf, ret + 1, format, al2); + va_end(al2); return(buf); } @@ -106,10 +111,18 @@ wchar_t *vswprintf2(wchar_t *format, va_list al) int ret; wchar_t *buf; size_t bufsize; + va_list al2; buf = smalloc(sizeof(wchar_t) * (bufsize = 1024)); - while((ret = vswprintf(buf, bufsize, format, al)) < 0) + while(1) + { + va_copy(al2, al); + ret = vswprintf(buf, bufsize, format, al2); + va_end(al2); + if(ret >= 0) + break; buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2)); + } if(bufsize > ret + 1) buf = srealloc(buf, sizeof(wchar_t) * (ret + 1)); return(buf); @@ -732,31 +745,38 @@ char *getetcpath(char *binpath) return(etcpath); } -char *findfile(char *gname, char *uname, char *homedir, int filldef) +char *findfile(char *name, char *homedir, int filldef) { char *path, *binpath, *etcpath, *p; struct passwd *pw; - int mode; + int mode, homeonly; + if(name == NULL) + return(NULL); + mode = R_OK | (filldef ? W_OK : 0); - if(uname != NULL) { + homeonly = homedir != NULL; + + if(!strchr(name, '/')) + { if(homedir == NULL) homedir = getenv("HOME"); if((homedir == NULL) && ((pw = getpwuid(getuid())) != NULL)) homedir = pw->pw_dir; - if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, uname)) != NULL)) + if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, name)) != NULL)) { if(!access(path, mode)) return(path); free(path); } } - if(gname != NULL) + + if(!homeonly) { - if(strchr(gname, '/') != NULL) + if(strchr(name, '/') != NULL) { - if(!access(gname, mode)) - return(sstrdup(gname)); + if(!access(name, mode)) + return(sstrdup(name)); } else { if((binpath = getenv("PATH")) == NULL) etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc"); @@ -764,7 +784,7 @@ char *findfile(char *gname, char *uname, char *homedir, int filldef) etcpath = getetcpath(binpath); for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":")) { - if((path = sprintf2("%s/%s", p, gname)) != NULL) + if((path = sprintf2("%s/%s", p, name)) != NULL) { if(!access(path, mode)) { @@ -777,10 +797,11 @@ char *findfile(char *gname, char *uname, char *homedir, int filldef) free(etcpath); } } + if(filldef) { - if(uname && homedir) - return(sprintf2("%s/.%s", homedir, uname)); - return(sprintf2("/etc/%s", gname)); + if(homedir) + return(sprintf2("%s/.%s", homedir, name)); + return(sprintf2("/etc/%s", name)); } else { return(NULL); }