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

Last change on this file since 08e103d4 was 08e103d4, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use clearer naming for string length functions

This and the following commit change the names of functions, as well as
their documentation, to use unambiguous terms "bytes" and "code points"
instead of ambiguous terms "size", "length", and "characters".

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[94d484a]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
[051bc69a]31#include <assert.h>
[074444f]32#include <libgen.h>
[94d484a]33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
[cf13b17]36#include <types/common.h>
[94d484a]37#include <sys/wait.h>
38#include <unistd.h>
39#include <errno.h>
40#include "../mytypes.h"
41
42#include "os.h"
43
[074444f]44/** Path to executable file via which we have been invoked. */
45static char *ef_path;
46
[94d484a]47/*
48 * The string functions are in fact standard C, but would not work under
49 * HelenOS.
[1b20da0]50 *
[38aaacc2]51 * XXX String functions used here only work with 8-bit text encoding.
[94d484a]52 */
53
[38aaacc2]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 */
[94d484a]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
[051bc69a]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
[38aaacc2]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 */
[b7fd2a0]116errno_t os_str_cmp(const char *a, const char *b)
[94d484a]117{
118 return strcmp(a, b);
119}
120
[38aaacc2]121/** Return number of characters in string.
122 *
123 * @param str String
124 * @return Number of characters in @a str.
125 */
[08e103d4]126size_t os_str_code_points(const char *str)
[074444f]127{
128 return strlen(str);
129}
130
[38aaacc2]131/** Duplicate string.
132 *
133 * @param str String
134 * @return New string, duplicate of @a str.
135 */
[94d484a]136char *os_str_dup(const char *str)
137{
138 return strdup(str);
139}
140
[38aaacc2]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 */
[b7fd2a0]149errno_t os_str_get_char(const char *str, int index, int *out_char)
[d0febca]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
[c5cb943d]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
[1ebc1a62]182#define OS_INPUT_BUFFER_SIZE 256
183static char os_input_buffer[OS_INPUT_BUFFER_SIZE];
184
[23de644]185/** Display survival help message. */
186void os_input_disp_help(void)
187{
188 printf("Send ^C (SIGINT) to quit.\n");
189}
190
[38aaacc2]191/** Read one line of input from the user.
192 *
193 * @param ptr Place to store pointer to new string.
194 */
[b7fd2a0]195errno_t os_input_line(const char *prompt, char **ptr)
[1ebc1a62]196{
[9be9c4d]197 printf("%s", prompt);
198
[1ebc1a62]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
[38aaacc2]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 */
[b7fd2a0]216errno_t os_exec(char *const cmd[])
[94d484a]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}
[074444f]245
[38aaacc2]246/** Store the executable file path via which we were executed.
247 *
248 * @param path Executable path via which we were executed.
249 */
[074444f]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.