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

Last change on this file was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 6.0 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 <types/common.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 */
116errno_t 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 */
149errno_t 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(char32_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 */
195errno_t os_input_line(const char *prompt, char **ptr)
196{
197 printf("%s", prompt);
198
199 if (fgets(os_input_buffer, OS_INPUT_BUFFER_SIZE, stdin) == NULL)
200 os_input_buffer[0] = '\0';
201
202 if (ferror(stdin)) {
203 *ptr = NULL;
204 return EIO;
205 }
206
207 *ptr = strdup(os_input_buffer);
208 return EOK;
209}
210
211/** Simple command execution.
212 *
213 * @param cmd Command and arguments (NULL-terminated list of strings.)
214 * Command is present just one, not duplicated.
215 */
216errno_t os_exec(char *const cmd[])
217{
218 pid_t pid;
219 int status;
220
221 pid = vfork();
222
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;
230 }
231
232 if (waitpid(pid, &status, 0) == -1) {
233 /* waitpid() failed */
234 printf("Error: Waitpid failed.\n");
235 exit(1);
236 }
237
238 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
239 printf("Error: Exec failed or child returned non-zero "
240 "exit status.\n");
241 }
242
243 return EOK;
244}
245
246/** Store the executable file path via which we were executed.
247 *
248 * @param path Executable path via which we were executed.
249 */
250void os_store_ef_path(char *path)
251{
252 ef_path = path;
253}
254
255/** Return path to the Sysel library
256 *
257 * @return New string. Caller should deallocate it using @c free().
258 */
259char *os_get_lib_path(void)
260{
261 return os_str_acat(dirname(ef_path), "/lib");
262}
Note: See TracBrowser for help on using the repository browser.