source: mainline/uspace/app/sbi/src/os/posix.c@ c5ad226

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c5ad226 was c5cb943d, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Update SBI to rev. 291.

  • Property mode set to 100644
File size: 5.9 KB
Line 
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 POSIX-specific code. */
30
31#include <assert.h>
32#include <libgen.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/types.h>
37#include <sys/wait.h>
38#include <unistd.h>
39#include <errno.h>
40#include "../mytypes.h"
41
42#include "os.h"
43
44/** Path to executable file via which we have been invoked. */
45static char *ef_path;
46
47/*
48 * The string functions are in fact standard C, but would not work under
49 * HelenOS.
50 *
51 * XXX String functions used here only work with 8-bit text encoding.
52 */
53
54/** Concatenate two strings.
55 *
56 * @param a First string
57 * @param b Second string
58 * @return New string, concatenation of @a a and @a b.
59 */
60char *os_str_acat(const char *a, const char *b)
61{
62 int a_len, b_len;
63 char *str;
64
65 a_len = strlen(a);
66 b_len = strlen(b);
67
68 str = malloc(a_len + b_len + 1);
69 if (str == NULL) {
70 printf("Memory allocation error.\n");
71 exit(1);
72 }
73
74 memcpy(str, a, a_len);
75 memcpy(str + a_len, b, b_len);
76 str[a_len + b_len] = '\0';
77
78 return str;
79}
80
81/** Return slice (substring) of a string.
82 *
83 * Copies the specified range of characters from @a str and returns it
84 * as a newly allocated string. @a start + @a length must be less than
85 * or equal to the length of @a str.
86 *
87 * @param str String
88 * @param start Index of first character (starting from zero).
89 * @param length Number of characters to copy.
90 *
91 * @return Newly allocated string holding the slice.
92 */
93char *os_str_aslice(const char *str, size_t start, size_t length)
94{
95 char *slice;
96
97 assert(start + length <= strlen(str));
98 slice = malloc(length + 1);
99 if (slice == NULL) {
100 printf("Memory allocation error.\n");
101 exit(1);
102 }
103
104 strncpy(slice, str + start, length);
105 slice[length] = '\0';
106
107 return slice;
108}
109
110/** Compare two strings.
111 *
112 * @param a First string
113 * @param b Second string
114 * @return Zero if equal, nonzero if not equal
115 */
116int os_str_cmp(const char *a, const char *b)
117{
118 return strcmp(a, b);
119}
120
121/** Return number of characters in string.
122 *
123 * @param str String
124 * @return Number of characters in @a str.
125 */
126size_t os_str_length(const char *str)
127{
128 return strlen(str);
129}
130
131/** Duplicate string.
132 *
133 * @param str String
134 * @return New string, duplicate of @a str.
135 */
136char *os_str_dup(const char *str)
137{
138 return strdup(str);
139}
140
141/** Get character from string at the given index.
142 *
143 * @param str String
144 * @param index Character index (starting from zero).
145 * @param out_char Place to store character.
146 * @return EOK on success, EINVAL if index is out of bounds,
147 * EIO on decoding error.
148 */
149int os_str_get_char(const char *str, int index, int *out_char)
150{
151 size_t len;
152
153 len = strlen(str);
154 if (index < 0 || (size_t) index >= len)
155 return EINVAL;
156
157 *out_char = str[index];
158 return EOK;
159}
160
161/** Convert character to new string.
162 *
163 * @param chr Character
164 * @return Newly allocated string.
165 */
166char *os_chr_to_astr(wchar_t chr)
167{
168 char *str;
169
170 str = malloc(2);
171 if (str == NULL) {
172 printf("Memory allocation error.\n");
173 exit(1);
174 }
175
176 str[0] = (char) chr;
177 str[1] = '\0';
178
179 return str;
180}
181
182#define OS_INPUT_BUFFER_SIZE 256
183static char os_input_buffer[OS_INPUT_BUFFER_SIZE];
184
185/** Display survival help message. */
186void os_input_disp_help(void)
187{
188 printf("Send ^C (SIGINT) to quit.\n");
189}
190
191/** Read one line of input from the user.
192 *
193 * @param ptr Place to store pointer to new string.
194 */
195int os_input_line(char **ptr)
196{
197 if (fgets(os_input_buffer, OS_INPUT_BUFFER_SIZE, stdin) == NULL)
198 os_input_buffer[0] = '\0';
199
200 if (ferror(stdin)) {
201 *ptr = NULL;
202 return EIO;
203 }
204
205 *ptr = strdup(os_input_buffer);
206 return EOK;
207}
208
209/** Simple command execution.
210 *
211 * @param cmd Command and arguments (NULL-terminated list of strings.)
212 * Command is present just one, not duplicated.
213 */
214int os_exec(char *const cmd[])
215{
216 pid_t pid;
217 int status;
218
219 pid = vfork();
220
221 if (pid == 0) {
222 execvp(cmd[0], cmd);
223 /* If we get here, exec failed. */
224 exit(1);
225 } else if (pid == -1) {
226 /* fork() failed */
227 return EBUSY;
228 }
229
230 if (waitpid(pid, &status, 0) == -1) {
231 /* waitpid() failed */
232 printf("Error: Waitpid failed.\n");
233 exit(1);
234 }
235
236 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
237 printf("Error: Exec failed or child returned non-zero "
238 "exit status.\n");
239 }
240
241 return EOK;
242}
243
244/** Store the executable file path via which we were executed.
245 *
246 * @param path Executable path via which we were executed.
247 */
248void os_store_ef_path(char *path)
249{
250 ef_path = path;
251}
252
253/** Return path to the Sysel library
254 *
255 * @return New string. Caller should deallocate it using @c free().
256 */
257char *os_get_lib_path(void)
258{
259 return os_str_acat(dirname(ef_path), "/lib");
260}
Note: See TracBrowser for help on using the repository browser.