--- libaitio/src/Attic/tools.c 2012/07/03 08:51:05 1.15 +++ libaitio/src/Attic/tools.c 2012/12/19 11:03:04 1.20 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tools.c,v 1.15 2012/07/03 08:51:05 misho Exp $ +* $Id: tools.c,v 1.20 2012/12/19 11:03:04 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -303,6 +303,113 @@ io_FreeNullTerm(char *** __restrict arr) } /* + * io_argsNum() Parse and calculate number of arguments + * + * @csArgs = Input arguments line + * @csDelim = Delimiter(s) for separate + * return: 0 error format; -1 error:: can`t read; >0 ok, number of items + */ +inline int +io_argsNum(const char *csArgs, const char *csDelim) +{ + register int res; + char *pos; + + assert(csArgs); + assert(csDelim); + if (!csArgs || !csDelim) + return -1; + + for (res = 1, pos = (char*) csArgs; (pos = strpbrk(pos, csDelim)); res++, pos++); + return res; +} + +/* + * io_MakeAV() Parse and make attribute/value pair + * + * @csArgs = Input argument line + * @csDelim = Delimiter for separate + * @psAttr = Output Attribute + * @attrLen = Size of attribute array + * @psValue = Output Value, if ==NULL this element not present value or not wanted for return + * @valLen = Size of value array + * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items +*/ +int +io_MakeAV(const char * __restrict csArgs, const char *csDelim, + char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen) +{ + register int ret = 0; + char *pos, *psBuf; + + if (!csArgs || !csDelim || !psAttr || !attrLen) + return -1; + if (psValue && !valLen) + return -1; + else + memset(psValue, 0, valLen); + psBuf = io_strdup(csArgs); + if (!psBuf) { + LOGERR; + return -1; + } + + pos = strpbrk(psBuf, csDelim); + if (pos) + *pos++ = 0; + ret++; + strlcpy(psAttr, psBuf, attrLen); + + if (pos && *pos) { + ret++; + if (psValue) + strlcpy(psValue, pos, valLen); + } + + io_free(psBuf); + return ret; +} + +/* + * io_MakeAV2() Parse and make attribute/value pair over input string + * + * @csArgs = Input argument line, will be modified! + * @csDelim = Delimiter for separate + * @psAttr = Output Attribute + * @psValue = Output Value, if ==NULL this element not present value or not wanted for return + * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items +*/ +int +io_MakeAV2(char * __restrict psArgs, const char *csDelim, + char ** __restrict psAttr, char ** __restrict psValue) +{ + register int ret = 0; + char *pos; + + if (!psArgs || !csDelim) + return -1; + + pos = strpbrk(psArgs, csDelim); + if (pos) { + *pos++ = 0; + ret++; + if (psAttr) + *psAttr = psArgs; + } else + return 0; + + if (psValue) { + if (pos && *pos) { + ret++; + *psValue = pos; + } else + *psValue = NULL; + } + + return ret; +} + +/* * io_Path2File() - Parse and make path/filename pair * * @csArgs = Input argument line @@ -486,8 +593,6 @@ io_gethostbyname(const char *psHost, u_short port, io_ if (!psHost || !addr) return NULL; - else - memset(addr, 0, sizeof(io_sockaddr_t)); if (*psHost != '/') { /* resolver */ @@ -498,10 +603,15 @@ io_gethostbyname(const char *psHost, u_short port, io_ if (!host) { io_SetErr(EINVAL, "Resolver #%d - %s", h_errno, hstrerror(h_errno)); return NULL; - } else + } else { + memset(addr, 0, sizeof(io_sockaddr_t)); addr->sa.sa_family = host->h_addrtype; - } else + } + } else { + memset(addr, 0, sizeof(io_sockaddr_t)); addr->sa.sa_family = AF_LOCAL; + } + switch (addr->sa.sa_family) { case AF_INET: @@ -528,4 +638,144 @@ io_gethostbyname(const char *psHost, u_short port, io_ } return NULL; +} + +/* + * io_addrcmp() - Compare network addresses + * + * @a = 1st address + * @b = 2nd address + * @p = compare and ports, if family is AF_INET or AF_INET6 + * return: 0 is equal or !=0 is different + */ +int +io_addrcmp(io_sockaddr_t * __restrict a, io_sockaddr_t * __restrict b, int p) +{ + if (a && b && a->sa.sa_family == b->sa.sa_family) + switch (a->sa.sa_family) { + case AF_LOCAL: + return strcmp(a->sun.sun_path, b->sun.sun_path); + case AF_INET: + if (p && (a->sin.sin_port - b->sin.sin_port)) + return (int) !!(a->sin.sin_port - b->sin.sin_port); + else + return memcmp(&a->sin.sin_addr, &b->sin.sin_addr, + sizeof a->sin.sin_addr); + case AF_INET6: + if (p && (a->sin6.sin6_port - b->sin6.sin6_port)) + return (int) !!(a->sin6.sin6_port - b->sin6.sin6_port); + else + return memcmp(&a->sin6.sin6_addr, &b->sin6.sin6_addr, + sizeof a->sin6.sin6_addr); + case AF_LINK: + return memcmp(&a->sdl.sdl_data, &b->sdl.sdl_data, + sizeof a->sdl.sdl_data); + } + + return (int) !!(a - b); +} + +/* + * io_usleep() - usleep() replacement for ELWIX + * + * @usec = microseconds for sleep + * return: -1 interrupted by signal or 0 ok + */ +inline int +io_usleep(u_int usec) +{ + struct timeval tv = { (time_t) (usec / 1000000), (long) (usec % 1000000) }; + + return select(0, NULL, NULL, NULL, &tv); +} + +/* + * io_AV2Path() - Attribute/Value pair store to file + * + * @csPath = Directory + * @csAttr = Attribute + * @csVal = Value + * @update = Update if a/v exists + * @perm = File permissions, if =0 set default perm=0600 + * return: -1 error or >-1 written bytes + */ +int +io_AV2Path(const char *csPath, const char *csAttr, const char *csVal, + int update, int perm) +{ + int fd, wlen = 0; + char szFile[MAXPATHLEN]; + + if (!csAttr) + return -1; + else + memset(szFile, 0, sizeof szFile); + snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr); + + wlen = O_CREAT | O_WRONLY; + if (!update) + wlen |= O_EXCL; + fd = open(szFile, wlen, perm ? perm : 0600); + if (fd == -1) { + LOGERR; + return -1; + } else + wlen ^= wlen; + + if (csVal) + if ((wlen = write(fd, csVal, strlen(csVal))) == -1) { + LOGERR; + close(fd); + unlink(szFile); + return -1; + } + + close(fd); + return wlen; +} + +/* + * io_Path2AV() - Get stored Attribute/Value + * + * @csPath = Directory + * @csAttr = Attribute + * @psVal = Value + * @valLen = Value length + * @del = Delete a/v pair after read + * return: -1 error or >-1 readed bytes + */ +int +io_Path2AV(const char *csPath, const char *csAttr, char *psVal, int valLen, int del) +{ + int fd, rlen = 0; + char szFile[MAXPATHLEN]; + + if (!csAttr) + return -1; + else + memset(szFile, 0, sizeof szFile); + snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr); + + if (psVal && valLen) { + fd = open(szFile, O_RDONLY); + if (fd == -1) { + LOGERR; + return -1; + } else + rlen ^= rlen; + + memset(psVal, 0, valLen); + rlen = read(fd, psVal, valLen - 1); + if (rlen == -1) { + LOGERR; + close(fd); + return -1; + } + + close(fd); + } + + if (del) + unlink(szFile); + return rlen; }