Annotation of libaitio/src/tools.c, revision 1.5
1.2 misho 1: /*************************************************************************
1.4 misho 2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
1.2 misho 4: *
5: * $Author: misho $
1.5 ! misho 6: * $Id: tools.c,v 1.4.4.3 2011/05/17 20:25:20 misho Exp $
1.2 misho 7: *
1.4 misho 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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: */
1.2 misho 46: #include "global.h"
47: #include "aitio.h"
48:
49:
50: /*
51: * io_LTrimStr() Remove left whitespaces from text string
52: * @psLine = Text string
53: * return: 0 nothing to do; !=0 Removed bytes
54: */
1.5 ! misho 55: inline int
! 56: io_LTrimStr(u_char * __restrict psLine)
1.2 misho 57: {
58: int pos = 0;
59: u_char *s;
60:
61: if (!psLine || !*psLine)
62: return 0;
63:
64: for (s = psLine; isspace(*s); s++);
65: pos = s - psLine;
66:
67: memmove(psLine, s, (strlen((char*) psLine) - pos) + 1);
68: return pos;
69: }
70:
71: /*
72: * io_RTrimStr() Remove right whitespaces from text string
73: * @psLine = Text string
74: * return: 0 nothing to do; !=0 Removed bytes
75: */
1.5 ! misho 76: inline int
! 77: io_RTrimStr(u_char * __restrict psLine)
1.2 misho 78: {
79: u_char *t, *pos;
80:
81: if (!psLine || !*psLine)
82: return 0;
83:
84: pos = psLine + strlen((char*) psLine);
85: for (t = pos - 1; t > psLine && isspace(*t); t--);
86: *++t = 0;
87:
88: return pos - t;
89: }
90:
91: /*
92: * io_TrimStr() Remove left and right whitespaces from text string
93: * @psLine = Text string
94: * return: 0 nothing to do; !=0 Removed bytes
95: */
1.5 ! misho 96: inline int
! 97: io_TrimStr(u_char * __restrict psLine)
1.2 misho 98: {
99: int ret = 0;
100:
101: ret = io_LTrimStr(psLine);
102: ret += io_RTrimStr(psLine);
103:
104: return ret;
105: }
106:
107: /*
108: * io_UnquotStr() Remove quots from input text string
109: * @psLine = Text string
110: * return: 0 nothing to do; 1 successful unquoted string
111: */
1.5 ! misho 112: inline int
! 113: io_UnquotStr(u_char * __restrict psLine)
1.2 misho 114: {
115: char *pos, *str = NULL;
116: int flg;
117:
118: if (!psLine)
119: return 0;
120:
121: switch (*psLine) {
122: case '`':
123: case '"':
124: case '\'':
125: str = strdup((char*) psLine + 1);
126: for (pos = str, flg = 0; *pos; flg = ('\\' == *pos), pos++) {
127: if (!flg && *pos == *psLine) {
128: *pos = 0;
129: strlcpy((char*) psLine, str, strlen((char*) psLine) + 1);
130: break;
131: }
132: }
133: free(str);
134: return 1;
135: }
136:
137: return 0;
138: }
139:
140: /*
1.3 misho 141: * io_Ch2Hex() Convert from Char string to Hex string
1.2 misho 142: * @psLine = Text string
143: * @lineLen = Length of Text string
1.3 misho 144: * return: NULL nothing to do or error; !=0 Allocated new converted data without term\0 (must be free)
1.2 misho 145: */
1.5 ! misho 146: inline u_char *
! 147: io_Ch2Hex(u_char *psLine, int lineLen)
1.2 misho 148: {
149: register int i;
1.3 misho 150: char szWork[3];
151: u_char *str;
1.2 misho 152:
153: if (!psLine || !*psLine || !lineLen)
154: return NULL;
155:
1.3 misho 156: str = malloc(lineLen / 2);
1.2 misho 157: if (!str) {
158: LOGERR;
159: return NULL;
160: } else
1.3 misho 161: memset(str, 0, lineLen / 2);
1.2 misho 162:
1.3 misho 163: for (i = 0; i < lineLen && psLine[i * 2]; i++) {
164: strlcpy(szWork, (char*) &psLine[i * 2], 3);
165: str[i] = (u_char) strtol(szWork, NULL, 16);
1.2 misho 166: }
167:
168: return str;
169: }
170:
171:
172: /*
1.3 misho 173: * io_Hex2Ch() Convert from Hex string to Char string
1.2 misho 174: * @psLine = Text string
175: * @lineLen = Length of Text string
176: * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
177: */
1.5 ! misho 178: inline char *
! 179: io_Hex2Ch(u_char *psLine, int lineLen)
1.2 misho 180: {
181: register int i;
182: char szWork[3], *str;
183:
184: if (!psLine || !*psLine || !lineLen)
185: return NULL;
186:
1.3 misho 187: str = malloc(lineLen * 2 + 1);
1.2 misho 188: if (!str) {
189: LOGERR;
190: return NULL;
191: } else
1.3 misho 192: memset(str, 0, lineLen * 2 + 1);
1.2 misho 193:
1.3 misho 194: for (i = 0; i <= lineLen; i++) {
195: memset(szWork, 0, 3);
196: snprintf(szWork, 3, "%02X", (u_char) psLine[i]);
197: strncat(str, szWork, 2);
1.2 misho 198: }
199:
200: return str;
201: }
1.5 ! misho 202:
! 203: /*
! 204: * io_CopyEnv() Copy environment to new environment array;
! 205: * @oldenv = Environment array
! 206: * return: NULL error; !=NULL Allocated new environment array(must be free)
! 207: */
! 208: char **
! 209: io_CopyEnv(const char **oldenv)
! 210: {
! 211: char **newenv, **el;
! 212: register int i, num;
! 213:
! 214: if (!oldenv)
! 215: return NULL;
! 216: else
! 217: newenv = el = NULL;
! 218:
! 219: /* count items environment */
! 220: for (i = num = 0; oldenv[i]; i++)
! 221: if (*strchr(oldenv[i], '='))
! 222: num++;
! 223:
! 224: /* create and copy new environment */
! 225: newenv = calloc(num + 1, sizeof(char*));
! 226: if (!newenv) {
! 227: LOGERR;
! 228: return NULL;
! 229: } else
! 230: el = newenv;
! 231:
! 232: for (i = 0; oldenv[i]; i++)
! 233: if (*strchr(oldenv[i], '=')) {
! 234: *el = strdup(oldenv[i]);
! 235: el++;
! 236: }
! 237: *el = NULL;
! 238:
! 239: return newenv;
! 240: }
! 241:
! 242: /*
! 243: * io_ExecArgs() Build exec arguments from other array
! 244: * @psProg = Program name for execute
! 245: * @oldarg = Arguments array
! 246: * return: NULL error; !=NULL Allocated execution array(must be free)
! 247: */
! 248: char **
! 249: io_ExecArgs(const char *psProg, const char **oldarg)
! 250: {
! 251: char **newarg, **el;
! 252: register int i, num;
! 253:
! 254: if (!psProg || !oldarg)
! 255: return NULL;
! 256: else
! 257: newarg = el = NULL;
! 258:
! 259: /* count items arguments */
! 260: for (num = 0; oldarg[num]; num++);
! 261:
! 262: /* create and copy new arguments */
! 263: newarg = calloc(num + 2, sizeof(char*));
! 264: if (!newarg) {
! 265: LOGERR;
! 266: return NULL;
! 267: } else
! 268: el = newarg;
! 269:
! 270: *el = strdup(psProg);
! 271: el++;
! 272:
! 273: for (i = 0; oldarg[i]; i++, el++)
! 274: *el = strdup(oldarg[i]);
! 275: *el = NULL;
! 276:
! 277: return newarg;
! 278: }
! 279:
! 280: /*
! 281: * io_FreeNullTerm() Free dynamic allocated null terminated array with strings
! 282: * @arr = Pointer to array for free
! 283: * return: none
! 284: */
! 285: inline void
! 286: io_FreeNullTerm(char *** __restrict arr)
! 287: {
! 288: char **a;
! 289:
! 290: if (arr && *arr) {
! 291: a = *arr;
! 292: while (a && *a)
! 293: free(*a++);
! 294: free(*arr);
! 295: *arr = NULL;
! 296: }
! 297: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>