File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / av.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu May 31 15:35:16 2018 UTC (5 years, 11 months ago) by misho
Branches: MAIN
CVS tags: elwix5_9, elwix5_8, elwix5_7, elwix5_6, elwix5_5, elwix5_4, elwix5_3, elwix5_2, elwix5_12, elwix5_11, elwix5_10, elwix5_1, elwix5_0, elwix4_26, elwix4_25, elwix4_24, elwix4_23, elwix4_22, elwix4_21, elwix4_20, elwix4_19, elwix4_18, elwix4_17, elwix4_16, elwix4_15, HEAD, ELWIX5_9, ELWIX5_8, ELWIX5_7, ELWIX5_6, ELWIX5_5, ELWIX5_4, ELWIX5_3, ELWIX5_2, ELWIX5_11, ELWIX5_10, ELWIX5_1, ELWIX5_0, ELWIX4_26, ELWIX4_25, ELWIX4_24, ELWIX4_23, ELWIX4_22, ELWIX4_21, ELWIX4_20, ELWIX4_19, ELWIX4_18, ELWIX4_17, ELWIX4_16, ELWIX4_15, ELWIX4_14
version 4.14

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: av.c,v 1.4 2018/05/31 15:35:16 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004 - 2018
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * av_Make() Parse and make attribute/value pair
   51:  *
   52:  * @csArgs = Input argument line
   53:  * @csDelim = Delimiter for separate
   54:  * @psAttr = Output Attribute
   55:  * @attrLen = Size of attribute array
   56:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
   57:  * @valLen = Size of value array
   58:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
   59: */
   60: int
   61: av_Make(const char * __restrict csArgs, const char *csDelim, 
   62: 		char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen)
   63: {
   64: 	register int ret = 0;
   65: 	char *pos, *psBuf;
   66: 
   67: 	if (!csArgs || !csDelim || !psAttr || !attrLen)
   68: 		return -1;
   69: 	if (psValue && !valLen)
   70: 		return -1;
   71: 	else
   72: 		memset(psValue, 0, valLen);
   73: 	psBuf = e_strdup(csArgs);
   74: 	if (!psBuf)
   75: 		return -1;
   76: 
   77: 	pos = strpbrk(psBuf, csDelim);
   78: 	if (pos)
   79: 		*pos++ = 0;
   80: 	ret++;
   81: 	strlcpy(psAttr, psBuf, attrLen);
   82: 
   83: 	if (pos && *pos) {
   84: 		ret++;
   85: 		if (psValue)
   86: 			strlcpy(psValue, pos, valLen);
   87: 	}
   88: 
   89: 	e_free(psBuf);
   90: 	return ret;
   91: }
   92: 
   93: /*
   94:  * av_MakeExt() Parse and make attribute/value pair over input string
   95:  *
   96:  * @csArgs = Input argument line, will be modified!
   97:  * @csDelim = Delimiter for separate
   98:  * @psAttr = Output Attribute
   99:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
  100:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  101: */
  102: int
  103: av_MakeExt(char * __restrict psArgs, const char *csDelim, 
  104: 		char ** __restrict psAttr, char ** __restrict psValue)
  105: {
  106: 	register int ret = 0;
  107: 	char *pos;
  108: 
  109: 	if (!psArgs || !csDelim)
  110: 		return -1;
  111: 
  112: 	pos = strpbrk(psArgs, csDelim);
  113: 	if (pos) {
  114: 		*pos++ = 0;
  115: 		ret++;
  116: 		if (psAttr)
  117: 			*psAttr = psArgs;
  118: 	} else
  119: 		return 0;
  120: 
  121: 	if (psValue) {
  122: 	       	if (pos && *pos) {
  123: 			ret++;
  124: 			*psValue = pos;
  125: 		} else
  126: 			*psValue = NULL;
  127: 	}
  128: 
  129: 	return ret;
  130: }
  131: 
  132: /*
  133:  * av_Path2File() - Parse and make path/filename pair
  134:  *
  135:  * @csArgs = Input argument line
  136:  * @psPath = Output Path, if ==NULL path not returned
  137:  * @pathLen = Size of path array
  138:  * @psFile = Output File
  139:  * @fileLen = Size of file array
  140:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  141:  */
  142: int
  143: av_Path2File(const char * __restrict csArgs, char * __restrict psPath, 
  144: 		int pathLen, char * __restrict psFile, int fileLen)
  145: {
  146: 	char *pos, *psBuf;
  147: 
  148: 	if (!csArgs || !psFile || !fileLen)
  149: 		return -1;
  150: 	if (psPath && !pathLen)
  151: 		return -1;
  152: 
  153: 	psBuf = e_strdup(csArgs);
  154: 	if (!psBuf)
  155: 		return -1;
  156: 
  157: 	pos = strrchr(psBuf, '/');
  158: 	if (!pos) {
  159: 		if (psPath)
  160: 			strlcpy(psPath, ".", pathLen);
  161: 
  162: 		strlcpy(psFile, psBuf, fileLen);
  163: 
  164: 		e_free(psBuf);
  165: 		return 1;
  166: 	} else
  167: 		*pos++ = 0;
  168: 
  169: 	strlcpy(psFile, pos, fileLen);
  170: 	if (psPath)
  171: 		strlcpy(psPath, *psBuf ? psBuf : "/", pathLen);
  172: 
  173: 	e_free(psBuf);
  174: 	return 2;
  175: }
  176: 
  177: 
  178: /*
  179:  * av_Save() - Attribute/Value pair store to file
  180:  *
  181:  * @csPath = Directory
  182:  * @csAttr = Attribute
  183:  * @csVal = Value
  184:  * @update = Update if a/v exists
  185:  * @perm = File permissions, if =0 set default perm=0600
  186:  * return: -1 error or >-1 written bytes
  187:  */
  188: int
  189: av_Save(const char *csPath, const char *csAttr, const char *csVal, int update, int perm)
  190: {
  191: 	int fd, wlen = 0;
  192: 	char szFile[MAXPATHLEN];
  193: 
  194: 	if (!csAttr)
  195: 		return -1;
  196: 	else
  197: 		memset(szFile, 0, sizeof szFile);
  198: 	snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr);
  199: 
  200: 	wlen = O_CREAT | O_WRONLY;
  201: 	if (!update)
  202: 		wlen |= O_EXCL;
  203: 	fd = open(szFile, wlen, perm ? perm : 0600);
  204: 	if (fd == -1) {
  205: 		LOGERR;
  206: 		return -1;
  207: 	} else
  208: 		wlen ^= wlen;
  209: 
  210: 	if (csVal)
  211: 		if ((wlen = write(fd, csVal, strlen(csVal))) == -1) {
  212: 			LOGERR;
  213: 			close(fd);
  214: 			unlink(szFile);
  215: 			return -1;
  216: 		}
  217: 
  218: 	close(fd);
  219: 	return wlen;
  220: }
  221: 
  222: /*
  223:  * av_Load() - Get stored Attribute/Value
  224:  *
  225:  * @csPath = Directory
  226:  * @csAttr = Attribute
  227:  * @psVal = Value
  228:  * @valLen = Value length
  229:  * @del = Delete a/v pair after read
  230:  * return: -1 error or >-1 readed bytes
  231:  */
  232: int
  233: av_Load(const char *csPath, const char *csAttr, char *psVal, int valLen, int del)
  234: {
  235: 	int fd, rlen = 0;
  236: 	char szFile[MAXPATHLEN];
  237: 
  238: 	if (!csAttr)
  239: 		return -1;
  240: 	else
  241: 		memset(szFile, 0, sizeof szFile);
  242: 	snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr);
  243: 
  244: 	if (psVal && valLen) {
  245: 		fd = open(szFile, O_RDONLY);
  246: 		if (fd == -1) {
  247: 			LOGERR;
  248: 			return -1;
  249: 		} else
  250: 			rlen ^= rlen;
  251: 
  252: 		memset(psVal, 0, valLen);
  253: 		rlen = read(fd, psVal, valLen - 1);
  254: 		if (rlen == -1) {
  255: 			LOGERR;
  256: 			close(fd);
  257: 			return -1;
  258: 		}
  259: 
  260: 		close(fd);
  261: 	}
  262: 
  263: 	if (del)
  264: 		unlink(szFile);
  265: 	return rlen;
  266: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>