1 | /*
|
---|
2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
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
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @file HelenOS-specific code. */
|
---|
30 |
|
---|
31 | #include <errno.h>
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <str.h>
|
---|
35 | #include <task.h>
|
---|
36 |
|
---|
37 | #include "os.h"
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Using HelenOS-specific string API.
|
---|
41 | */
|
---|
42 |
|
---|
43 | /** Concatenate two strings. */
|
---|
44 | char *os_str_acat(const char *a, const char *b)
|
---|
45 | {
|
---|
46 | int a_size, b_size;
|
---|
47 | char *str;
|
---|
48 |
|
---|
49 | a_size = str_size(a);
|
---|
50 | b_size = str_size(b);
|
---|
51 |
|
---|
52 | str = malloc(a_size + b_size + 1);
|
---|
53 | if (str == NULL) {
|
---|
54 | printf("Memory allocation error.\n");
|
---|
55 | exit(1);
|
---|
56 | }
|
---|
57 |
|
---|
58 | memcpy(str, a, a_size);
|
---|
59 | memcpy(str + a_size, b, b_size);
|
---|
60 | str[a_size + b_size] = '\0';
|
---|
61 |
|
---|
62 | return str;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Compare two strings. */
|
---|
66 | int os_str_cmp(const char *a, const char *b)
|
---|
67 | {
|
---|
68 | return str_cmp(a, b);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Duplicate string. */
|
---|
72 | char *os_str_dup(const char *str)
|
---|
73 | {
|
---|
74 | return str_dup(str);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** Get character from string at the given index. */
|
---|
78 | int os_str_get_char(const char *str, int index, int *out_char)
|
---|
79 | {
|
---|
80 | size_t offset;
|
---|
81 | int i;
|
---|
82 | wchar_t c;
|
---|
83 |
|
---|
84 | if (index < 0)
|
---|
85 | return EINVAL;
|
---|
86 |
|
---|
87 | offset = 0;
|
---|
88 | for (i = 0; i <= index; ++i) {
|
---|
89 | c = str_decode(str, &offset, STR_NO_LIMIT);
|
---|
90 | if (c == '\0')
|
---|
91 | return EINVAL;
|
---|
92 | if (c == U_SPECIAL)
|
---|
93 | return EIO;
|
---|
94 | }
|
---|
95 |
|
---|
96 | *out_char = (int) c;
|
---|
97 | return EOK;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Simple command execution. */
|
---|
101 | int os_exec(char *const cmd[])
|
---|
102 | {
|
---|
103 | task_id_t tid;
|
---|
104 | task_exit_t texit;
|
---|
105 | int retval;
|
---|
106 |
|
---|
107 | tid = task_spawn(cmd[0], (char const * const *) cmd);
|
---|
108 | if (tid == 0) {
|
---|
109 | printf("Error: Failed spawning '%s'.\n", cmd[0]);
|
---|
110 | exit(1);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* XXX Handle exit status and return value. */
|
---|
114 | task_wait(tid, &texit, &retval);
|
---|
115 |
|
---|
116 | return EOK;
|
---|
117 | }
|
---|