posix.c

00001 /*
00002  * Copyright (c) 2010 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00031 #include <assert.h>
00032 #include <libgen.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <sys/types.h>
00037 #include <sys/wait.h>
00038 #include <unistd.h>
00039 #include <errno.h>
00040 #include "../mytypes.h"
00041 
00042 #include "os.h"
00043 
00045 static char *ef_path;
00046 
00047 /*
00048  * The string functions are in fact standard C, but would not work under
00049  * HelenOS.
00050  * 
00051  * XXX String functions used here only work with 8-bit text encoding.
00052  */
00053 
00060 char *os_str_acat(const char *a, const char *b)
00061 {
00062         int a_len, b_len;
00063         char *str;
00064 
00065         a_len = strlen(a);
00066         b_len = strlen(b);
00067 
00068         str = malloc(a_len + b_len + 1);
00069         if (str == NULL) {
00070                 printf("Memory allocation error.\n");
00071                 exit(1);
00072         }
00073 
00074         memcpy(str, a, a_len);
00075         memcpy(str + a_len, b, b_len);
00076         str[a_len + b_len] = '\0';
00077 
00078         return str;
00079 }
00080 
00093 char *os_str_aslice(const char *str, size_t start, size_t length)
00094 {
00095         char *slice;
00096 
00097         assert(start + length <= strlen(str));
00098         slice = malloc(length + 1);
00099         if (slice == NULL) {
00100                 printf("Memory allocation error.\n");
00101                 exit(1);
00102         }
00103 
00104         strncpy(slice, str + start, length);
00105         slice[length] = '\0';
00106 
00107         return slice;
00108 }
00109 
00116 int os_str_cmp(const char *a, const char *b)
00117 {
00118         return strcmp(a, b);
00119 }
00120 
00126 size_t os_str_length(const char *str)
00127 {
00128         return strlen(str);
00129 }
00130 
00136 char *os_str_dup(const char *str)
00137 {
00138         return strdup(str);
00139 }
00140 
00149 int os_str_get_char(const char *str, int index, int *out_char)
00150 {
00151         size_t len;
00152 
00153         len = strlen(str);
00154         if (index < 0 || (size_t) index >= len)
00155                 return EINVAL;
00156 
00157         *out_char = str[index];
00158         return EOK;
00159 }
00160 
00166 char *os_chr_to_astr(wchar_t chr)
00167 {
00168         char *str;
00169 
00170         str = malloc(2);
00171         if (str == NULL) {
00172                 printf("Memory allocation error.\n");
00173                 exit(1);
00174         }
00175 
00176         str[0] = (char) chr;
00177         str[1] = '\0';
00178 
00179         return str;
00180 }
00181 
00182 #define OS_INPUT_BUFFER_SIZE 256
00183 static char os_input_buffer[OS_INPUT_BUFFER_SIZE];
00184 
00186 void os_input_disp_help(void)
00187 {
00188         printf("Send ^C (SIGINT) to quit.\n");
00189 }
00190 
00195 int os_input_line(char **ptr)
00196 {
00197         if (fgets(os_input_buffer, OS_INPUT_BUFFER_SIZE, stdin) == NULL)
00198                 os_input_buffer[0] = '\0';
00199 
00200         if (ferror(stdin)) {
00201                 *ptr = NULL;
00202                 return EIO;
00203         }
00204 
00205         *ptr = strdup(os_input_buffer);
00206         return EOK;
00207 }
00208 
00214 int os_exec(char *const cmd[])
00215 {
00216         pid_t pid;
00217         int status;
00218 
00219         pid = vfork();
00220 
00221         if (pid == 0) {
00222                 execvp(cmd[0], cmd);
00223                 /* If we get here, exec failed. */
00224                 exit(1);
00225         } else if (pid == -1) {
00226                 /* fork() failed */
00227                 return EBUSY;
00228         }
00229 
00230         if (waitpid(pid, &status, 0) == -1) {
00231                 /* waitpid() failed */
00232                 printf("Error: Waitpid failed.\n");
00233                 exit(1);
00234         }
00235 
00236         if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
00237                 printf("Error: Exec failed or child returned non-zero "
00238                     "exit status.\n");
00239         }
00240 
00241         return EOK;
00242 }
00243 
00248 void os_store_ef_path(char *path)
00249 {
00250         ef_path = path;
00251 }
00252 
00257 char *os_get_lib_path(void)
00258 {
00259         return os_str_acat(dirname(ef_path), "/lib");
00260 }

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7