2 * Copyright (c) 2010 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. */
36 #include <types/common.h>
40 #include "../mytypes.h"
44 /** Path to executable file via which we have been invoked. */
48 * The string functions are in fact standard C, but would not work under
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
)
68 str
= malloc(a_len
+ b_len
+ 1);
70 printf("Memory allocation error.\n");
74 memcpy(str
, a
, a_len
);
75 memcpy(str
+ a_len
, b
, b_len
);
76 str
[a_len
+ b_len
] = '\0';
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.
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
)
97 assert(start
+ length
<= strlen(str
));
98 slice
= malloc(length
+ 1);
100 printf("Memory allocation error.\n");
104 strncpy(slice
, str
+ start
, length
);
105 slice
[length
] = '\0';
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
)
121 /** Return number of characters in string.
124 * @return Number of characters in @a str.
126 size_t os_str_length(const char *str
)
131 /** Duplicate string.
134 * @return New string, duplicate of @a str.
136 char *os_str_dup(const char *str
)
141 /** Get character from string at the given index.
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
)
154 if (index
< 0 || (size_t) index
>= len
)
157 *out_char
= str
[index
];
161 /** Convert character to new string.
163 * @param chr Character
164 * @return Newly allocated string.
166 char *os_chr_to_astr(char32_t chr
)
172 printf("Memory allocation error.\n");
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';
207 *ptr
= strdup(os_input_buffer
);
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
[])
225 /* If we get here, exec failed. */
227 } else if (pid
== -1) {
232 if (waitpid(pid
, &status
, 0) == -1) {
233 /* waitpid() failed */
234 printf("Error: Waitpid failed.\n");
238 if (WIFEXITED(status
) && WEXITSTATUS(status
) != 0) {
239 printf("Error: Exec failed or child returned non-zero "
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
)
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");