Log files should have .txt extension.
[helenos.git] / uspace / app / sbi / src / os / posix.c
blob8abb977b7d4367bb77777860eafd6443d5fbd472
1 /*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @file POSIX-specific code. */
31 #include <assert.h>
32 #include <libgen.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <types/common.h>
37 #include <sys/wait.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include "../mytypes.h"
42 #include "os.h"
44 /** Path to executable file via which we have been invoked. */
45 static char *ef_path;
48 * The string functions are in fact standard C, but would not work under
49 * HelenOS.
51 * XXX String functions used here only work with 8-bit text encoding.
54 /** Concatenate two strings.
56 * @param a First string
57 * @param b Second string
58 * @return New string, concatenation of @a a and @a b.
60 char *os_str_acat(const char *a, const char *b)
62 int a_len, b_len;
63 char *str;
65 a_len = strlen(a);
66 b_len = strlen(b);
68 str = malloc(a_len + b_len + 1);
69 if (str == NULL) {
70 printf("Memory allocation error.\n");
71 exit(1);
74 memcpy(str, a, a_len);
75 memcpy(str + a_len, b, b_len);
76 str[a_len + b_len] = '\0';
78 return str;
81 /** Return slice (substring) of a string.
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.
87 * @param str String
88 * @param start Index of first character (starting from zero).
89 * @param length Number of characters to copy.
91 * @return Newly allocated string holding the slice.
93 char *os_str_aslice(const char *str, size_t start, size_t length)
95 char *slice;
97 assert(start + length <= strlen(str));
98 slice = malloc(length + 1);
99 if (slice == NULL) {
100 printf("Memory allocation error.\n");
101 exit(1);
104 strncpy(slice, str + start, length);
105 slice[length] = '\0';
107 return slice;
110 /** Compare two strings.
112 * @param a First string
113 * @param b Second string
114 * @return Zero if equal, nonzero if not equal
116 errno_t os_str_cmp(const char *a, const char *b)
118 return strcmp(a, b);
121 /** Return number of characters in string.
123 * @param str String
124 * @return Number of characters in @a str.
126 size_t os_str_length(const char *str)
128 return strlen(str);
131 /** Duplicate string.
133 * @param str String
134 * @return New string, duplicate of @a str.
136 char *os_str_dup(const char *str)
138 return strdup(str);
141 /** Get character from string at the given index.
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.
149 errno_t os_str_get_char(const char *str, int index, int *out_char)
151 size_t len;
153 len = strlen(str);
154 if (index < 0 || (size_t) index >= len)
155 return EINVAL;
157 *out_char = str[index];
158 return EOK;
161 /** Convert character to new string.
163 * @param chr Character
164 * @return Newly allocated string.
166 char *os_chr_to_astr(char32_t chr)
168 char *str;
170 str = malloc(2);
171 if (str == NULL) {
172 printf("Memory allocation error.\n");
173 exit(1);
176 str[0] = (char) chr;
177 str[1] = '\0';
179 return str;
182 #define OS_INPUT_BUFFER_SIZE 256
183 static char os_input_buffer[OS_INPUT_BUFFER_SIZE];
185 /** Display survival help message. */
186 void os_input_disp_help(void)
188 printf("Send ^C (SIGINT) to quit.\n");
191 /** Read one line of input from the user.
193 * @param ptr Place to store pointer to new string.
195 errno_t os_input_line(const char *prompt, char **ptr)
197 printf("%s", prompt);
199 if (fgets(os_input_buffer, OS_INPUT_BUFFER_SIZE, stdin) == NULL)
200 os_input_buffer[0] = '\0';
202 if (ferror(stdin)) {
203 *ptr = NULL;
204 return EIO;
207 *ptr = strdup(os_input_buffer);
208 return EOK;
211 /** Simple command execution.
213 * @param cmd Command and arguments (NULL-terminated list of strings.)
214 * Command is present just one, not duplicated.
216 errno_t os_exec(char *const cmd[])
218 pid_t pid;
219 int status;
221 pid = vfork();
223 if (pid == 0) {
224 execvp(cmd[0], cmd);
225 /* If we get here, exec failed. */
226 exit(1);
227 } else if (pid == -1) {
228 /* fork() failed */
229 return EBUSY;
232 if (waitpid(pid, &status, 0) == -1) {
233 /* waitpid() failed */
234 printf("Error: Waitpid failed.\n");
235 exit(1);
238 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
239 printf("Error: Exec failed or child returned non-zero "
240 "exit status.\n");
243 return EOK;
246 /** Store the executable file path via which we were executed.
248 * @param path Executable path via which we were executed.
250 void os_store_ef_path(char *path)
252 ef_path = path;
255 /** Return path to the Sysel library
257 * @return New string. Caller should deallocate it using @c free().
259 char *os_get_lib_path(void)
261 return os_str_acat(dirname(ef_path), "/lib");