forked from Radmind/radmind
-
Notifications
You must be signed in to change notification settings - Fork 1
/
argcargv.c
100 lines (84 loc) · 1.66 KB
/
argcargv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2003 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
/*
* Return parsed argc/argv from the net.
*/
#include "config.h"
#include <stdlib.h>
#include "argcargv.h"
#define ACV_ARGC 10
#define ACV_WHITE 0
#define ACV_WORD 1
static ACAV *acavg = NULL;
ACAV*
acav_alloc( void )
{
ACAV *acav;
if ( ( acav = (ACAV*)malloc( sizeof( ACAV ) ) ) == NULL ) {
return( NULL );
}
if ( ( acav->acv_argv =
(char **)malloc( sizeof(char *) * ( ACV_ARGC ) ) ) == NULL ) {
free( acav );
return( NULL );
}
acav->acv_argc = ACV_ARGC;
return( acav );
}
/*
* acav->acv_argv = **argv[] if passed an ACAV
*/
int
acav_parse( ACAV *acav, char *line, char **argv[] )
{
int ac;
int state;
if ( acav == NULL ) {
if ( acavg == NULL ) {
if (( acavg = acav_alloc()) == NULL ) {
return( -1 );
}
}
acav = acavg;
}
ac = 0;
state = ACV_WHITE;
for ( ; *line != '\0'; line++ ) {
switch ( *line ) {
case ' ' :
case '\t' :
case '\n' :
if ( state == ACV_WORD ) {
*line = '\0';
state = ACV_WHITE;
}
break;
default :
if ( state == ACV_WHITE ) {
acav->acv_argv[ ac++ ] = line;
if ( ac >= acav->acv_argc ) {
/* realloc */
if (( acav->acv_argv = (char **)realloc( acav->acv_argv,
sizeof( char * ) * ( acav->acv_argc + ACV_ARGC )))
== NULL ) {
return( -1 );
}
acav->acv_argc += ACV_ARGC;
}
state = ACV_WORD;
}
}
}
acav->acv_argv[ ac ] = NULL;
*argv = acav->acv_argv;
return( ac );
}
int
acav_free( ACAV *acav )
{
free( acav->acv_argv );
free( acav );
return( 0 );
}