Annotation of libelwix/src/av.c, revision 1.2.20.1

1.1       misho       1: /*************************************************************************
                      2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
                      3: *  by Michael Pounov <misho@elwix.org>
                      4: *
                      5: * $Author: misho $
1.2.20.1! misho       6: * $Id: av.c,v 1.2 2014/01/29 14:16:54 misho Exp $
1.1       misho       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: 
1.2.20.1! misho      15: Copyright 2004 - 2015
1.1       misho      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:                strlcpy(psFile, psBuf, fileLen);
                    160: 
                    161:                e_free(psBuf);
                    162:                return 1;
                    163:        } else
                    164:                *pos++ = 0;
                    165: 
                    166:        strlcpy(psFile, pos, fileLen);
                    167:        if (psPath)
                    168:                strlcpy(psPath, psBuf, pathLen);
                    169: 
                    170:        e_free(psBuf);
                    171:        return 2;
                    172: }
                    173: 
                    174: 
                    175: /*
                    176:  * av_Save() - Attribute/Value pair store to file
                    177:  *
                    178:  * @csPath = Directory
                    179:  * @csAttr = Attribute
                    180:  * @csVal = Value
                    181:  * @update = Update if a/v exists
                    182:  * @perm = File permissions, if =0 set default perm=0600
                    183:  * return: -1 error or >-1 written bytes
                    184:  */
                    185: int
                    186: av_Save(const char *csPath, const char *csAttr, const char *csVal, int update, int perm)
                    187: {
                    188:        int fd, wlen = 0;
                    189:        char szFile[MAXPATHLEN];
                    190: 
                    191:        if (!csAttr)
                    192:                return -1;
                    193:        else
                    194:                memset(szFile, 0, sizeof szFile);
                    195:        snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr);
                    196: 
                    197:        wlen = O_CREAT | O_WRONLY;
                    198:        if (!update)
                    199:                wlen |= O_EXCL;
                    200:        fd = open(szFile, wlen, perm ? perm : 0600);
                    201:        if (fd == -1) {
                    202:                LOGERR;
                    203:                return -1;
                    204:        } else
                    205:                wlen ^= wlen;
                    206: 
                    207:        if (csVal)
                    208:                if ((wlen = write(fd, csVal, strlen(csVal))) == -1) {
                    209:                        LOGERR;
                    210:                        close(fd);
                    211:                        unlink(szFile);
                    212:                        return -1;
                    213:                }
                    214: 
                    215:        close(fd);
                    216:        return wlen;
                    217: }
                    218: 
                    219: /*
                    220:  * av_Load() - Get stored Attribute/Value
                    221:  *
                    222:  * @csPath = Directory
                    223:  * @csAttr = Attribute
                    224:  * @psVal = Value
                    225:  * @valLen = Value length
                    226:  * @del = Delete a/v pair after read
                    227:  * return: -1 error or >-1 readed bytes
                    228:  */
                    229: int
                    230: av_Load(const char *csPath, const char *csAttr, char *psVal, int valLen, int del)
                    231: {
                    232:        int fd, rlen = 0;
                    233:        char szFile[MAXPATHLEN];
                    234: 
                    235:        if (!csAttr)
                    236:                return -1;
                    237:        else
                    238:                memset(szFile, 0, sizeof szFile);
                    239:        snprintf(szFile, sizeof szFile, "%s/%s.av", csPath ? csPath : ".", csAttr);
                    240: 
                    241:        if (psVal && valLen) {
                    242:                fd = open(szFile, O_RDONLY);
                    243:                if (fd == -1) {
                    244:                        LOGERR;
                    245:                        return -1;
                    246:                } else
                    247:                        rlen ^= rlen;
                    248: 
                    249:                memset(psVal, 0, valLen);
                    250:                rlen = read(fd, psVal, valLen - 1);
                    251:                if (rlen == -1) {
                    252:                        LOGERR;
                    253:                        close(fd);
                    254:                        return -1;
                    255:                }
                    256: 
                    257:                close(fd);
                    258:        }
                    259: 
                    260:        if (del)
                    261:                unlink(szFile);
                    262:        return rlen;
                    263: }

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