File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / src / av.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Mon Oct 28 09:58:51 2024 UTC (3 weeks, 6 days ago) by misho
Branches: MAIN
CVS tags: elwix6_6, elwix6_5, HEAD, ELWIX6_5, ELWIX6_4
Version 6.4

/*************************************************************************
* (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
*  by Michael Pounov <misho@elwix.org>
*
* $Author: misho $
* $Id: av.c,v 1.5 2024/10/28 09:58:51 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2024
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"


/*
 * av_Make() 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
av_Make(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 if (psValue)
		memset(psValue, 0, valLen);
	psBuf = e_strdup(csArgs);
	if (!psBuf)
		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);
	}

	e_free(psBuf);
	return ret;
}

/*
 * av_MakeExt() 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
av_MakeExt(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;
}

/*
 * av_Path2File() - Parse and make path/filename pair
 *
 * @csArgs = Input argument line
 * @psPath = Output Path, if ==NULL path not returned
 * @pathLen = Size of path array
 * @psFile = Output File
 * @fileLen = Size of file array
 * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
 */
int
av_Path2File(const char * __restrict csArgs, char * __restrict psPath, 
		int pathLen, char * __restrict psFile, int fileLen)
{
	char *pos, *psBuf;

	if (!csArgs || !psFile || !fileLen)
		return -1;
	if (psPath && !pathLen)
		return -1;

	psBuf = e_strdup(csArgs);
	if (!psBuf)
		return -1;

	pos = strrchr(psBuf, '/');
	if (!pos) {
		if (psPath)
			strlcpy(psPath, ".", pathLen);

		strlcpy(psFile, psBuf, fileLen);

		e_free(psBuf);
		return 1;
	} else
		*pos++ = 0;

	strlcpy(psFile, pos, fileLen);
	if (psPath)
		strlcpy(psPath, *psBuf ? psBuf : "/", pathLen);

	e_free(psBuf);
	return 2;
}


/*
 * av_Save() - 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
av_Save(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;
}

/*
 * av_Load() - 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
av_Load(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;
}

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