1 /*
2 * Copyright (c) 2016 Daichi GOTO
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "command.h"
29
30 void
strtoargv(char * argv[],char * str)31 strtoargv(char *argv[], char *str)
32 {
33 char *p, *s, *buf;;
34 int i, j;
35 p = str;
36 i = 1;
37 j = 0;
38 while (1) {
39 switch (*p) {
40 case '/':
41 if (p == str)
42 usage();
43 case '0':
44 case '1':
45 case '2':
46 case '3':
47 case '4':
48 case '5':
49 case '6':
50 case '7':
51 case '8':
52 case '9':
53 if (0 == j)
54 s = p;
55 ++j;
56 break;
57 case '\0':
58 default:
59 buf = malloc(sizeof(char) * (j + 1));
60 strncpy(buf, s, j);
61 *(buf+j) = '\0';
62 argv[i] = buf;
63 ++i;
64 j = 0;
65 break;
66 }
67 if ('\0' == *p)
68 break;
69 ++p;
70 }
71 }
72