X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=daemon%2Futils.c;h=5f2c03a4f2e5eb99d4b9f87e4dab9f298d9d2789;hb=01663fd9f926b17092dba5beb571488aaf11edf9;hp=216ae771af770b268d1e600961997bb5d80e9ab6;hpb=d3372da97568d5e1f35fa19787c8ec8af93a0435;p=doldaconnect.git diff --git a/daemon/utils.c b/daemon/utils.c index 216ae77..5f2c03a 100644 --- a/daemon/utils.c +++ b/daemon/utils.c @@ -126,6 +126,19 @@ wchar_t *swprintf2(wchar_t *format, ...) return(buf); } +int havecharset(char *charset) +{ + iconv_t cd; + + if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1) + return(0); + iconv_close(cd); + if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1) + return(0); + iconv_close(cd); + return(1); +} + wchar_t *icmbstowcs(char *mbs, char *charset) { int ret; @@ -190,7 +203,7 @@ wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def) free(buf); if((buf = icmbstowcs(mbs, charset)) == NULL) { - if(*def == '~') + if((def != NULL) && (*def == L'~')) { flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno)); def++; @@ -264,7 +277,7 @@ char *icswcstombs(wchar_t *wcs, char *charset, char *def) free(buf); if((buf = icwcstombs(wcs, charset)) == NULL) { - if(*def == '~') + if((def != NULL) && (*def == '~')) { flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno)); def++; @@ -533,6 +546,8 @@ char *base64decode(char *data, size_t *datalen) c = (int)(unsigned char)*data; if(c == '=') break; + if(c == '\n') + continue; if(base64rev[c] == -1) { if(buf != NULL) @@ -630,6 +645,8 @@ char *base32decode(char *data, size_t *datalen) c = (int)(unsigned char)*data; if(c == '=') break; + if(c == '\n') + continue; if(base32rev[c] == -1) { if(buf != NULL) @@ -676,13 +693,19 @@ int _parrlen(void **arr) char *getetcpath(char *binpath) { + int f; char *etcpath, *p; size_t etcpathsize, etcpathdata; etcpath = NULL; etcpathsize = etcpathdata = 0; + f = 1; do { + if(f) + f = 0; + else + binpath++; for(p = binpath; *p && (*p != ':'); p++); for(; (p >= binpath) && (*p != '/'); p--); if(p >= binpath) @@ -697,21 +720,30 @@ char *getetcpath(char *binpath) return(etcpath); } -char *findfile(char *gname, char *uname, char *homedir) +char *findfile(char *gname, char *uname, char *homedir, int filldef) { char *path, *binpath, *etcpath, *p; + struct passwd *pw; + int mode; - if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, uname)) != NULL)) - { - if(!access(path, F_OK)) - return(path); - free(path); + mode = R_OK | (filldef ? W_OK : 0); + if(uname != NULL) { + 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(!access(path, mode)) + return(path); + free(path); + } } if(gname != NULL) { if(strchr(gname, '/') != NULL) { - if(!access(gname, F_OK)) + if(!access(gname, mode)) return(sstrdup(gname)); } else { if((binpath = getenv("PATH")) == NULL) @@ -722,7 +754,7 @@ char *findfile(char *gname, char *uname, char *homedir) { if((path = sprintf2("%s/%s", p, gname)) != NULL) { - if(!access(path, F_OK)) + if(!access(path, mode)) { free(etcpath); return(path); @@ -733,5 +765,58 @@ char *findfile(char *gname, char *uname, char *homedir) free(etcpath); } } + if(filldef) { + if(uname && homedir) + return(sprintf2("%s/.%s", homedir, uname)); + return(sprintf2("/etc/%s", gname)); + } else { + return(NULL); + } +} + +struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list) +{ + struct wcspair *pair; + + pair = smalloc(sizeof(*pair)); + memset(pair, 0, sizeof(*pair)); + if(key != NULL) + pair->key = swcsdup(key); + if(val != NULL) + pair->val = swcsdup(val); + if(list == NULL) + { + pair->next = NULL; + } else { + pair->next = *list; + *list = pair; + } + return(pair); +} + +void freewcspair(struct wcspair *pair, struct wcspair **list) +{ + struct wcspair *cur; + + for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next) + { + if(cur == pair) + { + *list = cur->next; + break; + } + } + free(pair->key); + free(pair->val); + free(pair); +} + +wchar_t *wpfind(struct wcspair *list, wchar_t *key) +{ + for(; list != NULL; list = list->next) + { + if(!wcscmp(list->key, key)) + return(list->val); + } return(NULL); }