Changes in / [1814ee4d:d85a01c] in mainline
- Location:
- uspace/lib/posix
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/assert.h
r1814ee4d rd85a01c 40 40 41 41 #ifndef NDEBUG 42 #define assert(expr) ((expr) ? (void) 0 : assert_abort(#expr, __FILE__, __LINE__)) 42 #define assert(expr) \ 43 do { \ 44 if (!(expr)) { \ 45 assert_abort(#expr, __FILE__, __LINE__); \ 46 } \ 47 } while (0) 43 48 #else 44 49 #define assert(expr) ((void) 0) -
uspace/lib/posix/ctype.c
r1814ee4d rd85a01c 94 94 int posix_isprint(int c) 95 95 { 96 return posix_isascii(c) &&!posix_iscntrl(c);96 return !posix_iscntrl(c); 97 97 } 98 98 -
uspace/lib/posix/fnmatch.c
r1814ee4d rd85a01c 525 525 static char *_casefold(const char *s) 526 526 { 527 assert(s != NULL);528 527 char *result = strdup(s); 529 528 for (char *i = result; *i != '\0'; ++i) { … … 543 542 int posix_fnmatch(const char *pattern, const char *string, int flags) 544 543 { 545 assert(pattern != NULL);546 assert(string != NULL);547 548 544 // TODO: don't fold everything in advance, but only when needed 549 545 -
uspace/lib/posix/internal/common.h
r1814ee4d rd85a01c 43 43 __func__, __FILE__, __LINE__), abort()) 44 44 45 /* A little helper macro to avoid typing this over and over. */46 #define errnify(func, ...) ({ \47 int rc = func(__VA_ARGS__); \48 if (rc < 0) { \49 errno = -rc; \50 rc = -1; \51 } \52 rc; \53 })54 55 45 #endif /* LIBPOSIX_COMMON_H_ */ 56 46 -
uspace/lib/posix/pwd.c
r1814ee4d rd85a01c 39 39 #include "errno.h" 40 40 41 // TODO: documentation 42 41 43 static bool entry_read = false; 42 44 … … 44 46 static const struct posix_passwd dummy_pwd = { 45 47 .pw_name = (char *) "user", 46 .pw_uid = 0,47 .pw_gid = 0,48 .pw_uid = 1, 49 .pw_gid = 1, 48 50 .pw_dir = (char *) "/", 49 51 .pw_shell = (char *) "/app/bdsh" … … 113 115 { 114 116 assert(name != NULL); 115 assert(pwd != NULL);116 assert(buffer != NULL);117 assert(result != NULL);118 117 119 118 if (posix_strcmp(name, "user") != 0) { … … 122 121 } 123 122 124 return posix_getpwuid_r( 0, pwd, buffer, bufsize, result);123 return posix_getpwuid_r(1, pwd, buffer, bufsize, result); 125 124 } 126 125 … … 133 132 struct posix_passwd *posix_getpwuid(posix_uid_t uid) 134 133 { 135 if (uid != 0) {134 if (uid != 1) { 136 135 return NULL; 137 136 } … … 160 159 '/', '\0', 'b', 'd', 's', 'h', '\0' }; 161 160 162 if (uid != 0) {161 if (uid != 1) { 163 162 *result = NULL; 164 163 return 0; … … 172 171 173 172 pwd->pw_name = (char *) bf; 174 pwd->pw_uid = 0;175 pwd->pw_gid = 0;173 pwd->pw_uid = 1; 174 pwd->pw_gid = 1; 176 175 pwd->pw_dir = (char *) bf + 5; 177 176 pwd->pw_shell = (char *) bf + 7; -
uspace/lib/posix/pwd.h
r1814ee4d rd85a01c 35 35 #ifndef POSIX_PWD_H_ 36 36 #define POSIX_PWD_H_ 37 38 // TODO: documentation 37 39 38 40 #include "sys/types.h" -
uspace/lib/posix/signal.c
r1814ee4d rd85a01c 66 66 67 67 /** 68 * Default signal handler. Executes the default action for each signal, 69 * as reasonable within HelenOS. 70 * 71 * @param signo Signal number. 68 * 69 * @param signo 72 70 */ 73 71 void __posix_default_signal_handler(int signo) … … 77 75 abort(); 78 76 case SIGQUIT: 79 fprintf(stderr, "Quit signal raised. Exiting. \n");77 fprintf(stderr, "Quit signal raised. Exiting."); 80 78 exit(EXIT_FAILURE); 81 79 case SIGINT: 82 fprintf(stderr, "Interrupt signal caught. Exiting. \n");80 fprintf(stderr, "Interrupt signal caught. Exiting."); 83 81 exit(EXIT_FAILURE); 84 82 case SIGTERM: 85 fprintf(stderr, "Termination signal caught. Exiting. \n");83 fprintf(stderr, "Termination signal caught. Exiting."); 86 84 exit(EXIT_FAILURE); 87 85 case SIGSTOP: 88 fprintf(stderr, "Stop signal caught, but unsupported. Ignoring. \n");86 fprintf(stderr, "Stop signal caught, but unsupported. Ignoring."); 89 87 break; 90 88 case SIGKILL: -
uspace/lib/posix/stdio.c
r1814ee4d rd85a01c 50 50 #include "libc/str.h" 51 51 #include "libc/malloc.h" 52 #include "libc/adt/list.h"53 #include "libc/sys/stat.h"54 52 55 53 … … 254 252 assert(mode != NULL); 255 253 assert(stream != NULL); 254 255 if (filename == NULL) { 256 // TODO 257 258 /* print error to stderr as well, to avoid hard to find problems 259 * with buggy apps that expect this to work 260 */ 261 fprintf(stderr, 262 "ERROR: Application wants to use freopen() to change mode of opened stream.\n" 263 " libposix does not support that yet, the application may function improperly.\n"); 264 errno = ENOTSUP; 265 return NULL; 266 } 267 268 FILE* copy = malloc(sizeof(FILE)); 269 if (copy == NULL) { 270 errno = ENOMEM; 271 return NULL; 272 } 273 memcpy(copy, stream, sizeof(FILE)); 274 fclose(copy); /* copy is now freed */ 256 275 257 /* Retieve the node. */ 258 struct stat st; 259 int rc; 276 copy = fopen(filename, mode); /* open new stream */ 277 if (copy == NULL) { 278 /* fopen() sets errno */ 279 return NULL; 280 } 260 281 261 if (filename == NULL) { 262 rc = fstat(stream->fd, &st); 263 } else { 264 rc = stat(filename, &st); 265 } 282 /* move the new stream to the original location */ 283 memcpy(stream, copy, sizeof (FILE)); 284 free(copy); 266 285 267 if (rc != EOK) { 268 fclose(stream); 269 errno = -rc; 270 return NULL; 271 } 272 273 fdi_node_t node = { 274 .fs_handle = st.fs_handle, 275 .devmap_handle = st.devmap_handle, 276 .index = st.index 277 }; 278 279 /* Open a new stream. */ 280 FILE* new = fopen_node(&node, mode); 281 if (new == NULL) { 282 fclose(stream); 283 /* fopen_node() sets errno. */ 284 return NULL; 285 } 286 287 /* Close the original stream without freeing it (ignoring errors). */ 288 if (stream->buf != NULL) { 289 fflush(stream); 290 } 291 if (stream->sess != NULL) { 292 async_hangup(stream->sess); 293 } 294 if (stream->fd >= 0) { 295 close(stream->fd); 296 } 297 list_remove(&stream->link); 298 299 /* Move the new stream to the original location. */ 300 memcpy(stream, new, sizeof (FILE)); 301 free(new); 302 303 /* Update references in the file list. */ 286 /* update references in the file list */ 304 287 stream->link.next->prev = &stream->link; 305 288 stream->link.prev->next = &stream->link; … … 693 676 694 677 /** 695 * Remove a file or directory.678 * Remove a file. 696 679 * 697 680 * @param path Pathname of the file that shall be removed. 698 * @return Zero on success, -1 (with errno set)otherwise.681 * @return Zero on success, -1 otherwise. 699 682 */ 700 683 int posix_remove(const char *path) 701 684 { 702 struct stat st; 703 int rc = stat(path, &st); 704 705 if (rc != EOK) { 706 errno = -rc; 707 return -1; 708 } 709 710 if (st.is_directory) { 711 rc = rmdir(path); 712 } else { 713 rc = unlink(path); 714 } 715 716 if (rc != EOK) { 717 errno = -rc; 718 return -1; 719 } 720 return 0; 721 } 722 723 /** 724 * Rename a file or directory. 725 * 726 * @param old 727 * @param new 728 * @return Zero on success, -1 (with errno set) otherwise. 729 */ 730 int posix_rename(const char *old, const char *new) 731 { 732 return errnify(rename, old, new); 685 // FIXME: unlink() and rmdir() seem to be equivalent at the moment, 686 // but that does not have to be true forever 687 return unlink(path); 733 688 } 734 689 -
uspace/lib/posix/stdio.h
r1814ee4d rd85a01c 116 116 extern int posix_remove(const char *path); 117 117 118 /* Renaming Files */119 extern int posix_rename(const char *old, const char *new);120 121 118 /* Temporary Files */ 122 119 #undef L_tmpnam … … 173 170 #define remove posix_remove 174 171 175 #define rename posix_rename176 177 172 #define tmpnam posix_tmpnam 178 173 #endif -
uspace/lib/posix/sys/wait.c
r1814ee4d rd85a01c 39 39 #include "wait.h" 40 40 41 #include "../libc/task.h"42 #include "../assert.h"43 #include "../errno.h"44 #include "../limits.h"45 #include "../signal.h"46 47 int __posix_wifexited(int status) {48 return status != INT_MIN;49 }50 51 int __posix_wexitstatus(int status) {52 assert(__posix_wifexited(status));53 return status;54 }55 56 int __posix_wifsignaled(int status) {57 return status == INT_MIN;58 }59 60 int __posix_wtermsig(int status) {61 assert(__posix_wifsignaled(status));62 /* There is no way to distinguish reason63 * for unexpected termination at the moment.64 */65 return SIGABRT;66 }67 68 41 /** 69 42 * … … 73 46 posix_pid_t posix_wait(int *stat_ptr) 74 47 { 75 /* HelenOS does not support this. */ 76 errno = ENOSYS; 77 return (posix_pid_t) -1; 48 // TODO: low priority, just a compile-time dependency of binutils 49 not_implemented(); 78 50 } 79 51 … … 87 59 posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options) 88 60 { 89 assert(stat_ptr != NULL); 90 assert(options == 0 /* None of the options are supported. */); 91 92 task_exit_t texit; 93 int retval; 94 95 int rc = task_wait((task_id_t) pid, &texit, &retval); 96 97 if (rc < 0) { 98 /* Unable to retrieve status. */ 99 errno = -rc; 100 return (posix_pid_t) -1; 101 } 102 103 if (texit == TASK_EXIT_NORMAL) { 104 // FIXME: relies on application not returning this value 105 assert(retval != INT_MIN); 106 *stat_ptr = retval; 107 } else { 108 /* Reserve the lowest value for unexpected termination. */ 109 *stat_ptr = INT_MIN; 110 } 111 112 return pid; 61 // TODO: low priority, just a compile-time dependency of binutils 62 not_implemented(); 113 63 } 114 64 -
uspace/lib/posix/sys/wait.h
r1814ee4d rd85a01c 38 38 #include "types.h" 39 39 40 #undef WIFEXITED41 #undef WEXITSTATUS42 #undef WIFSIGNALED43 #undef WTERMSIG44 #define WIFEXITED(status) __posix_wifexited(status)45 #define WEXITSTATUS(status) __posix_wexitstatus(status)46 #define WIFSIGNALED(status) __posix_wifsignaled(status)47 #define WTERMSIG(status) __posix_wtermsig(status)48 49 extern int __posix_wifexited(int status);50 extern int __posix_wexitstatus(int status);51 extern int __posix_wifsignaled(int status);52 extern int __posix_wtermsig(int status);53 54 40 extern posix_pid_t posix_wait(int *stat_ptr); 55 41 extern posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options); … … 57 43 #ifndef LIBPOSIX_INTERNAL 58 44 #define wait posix_wait 59 #define waitpid 45 #define waitpid posix_waitpid 60 46 #endif 61 47 -
uspace/lib/posix/unistd.c
r1814ee4d rd85a01c 110 110 return NULL; 111 111 } 112 113 /* Save the original value to comply with the "no modification on114 * success" semantics.115 */116 int orig_errno = errno;117 errno = EOK;118 119 112 char *ret = getcwd(buf, size); 120 if (ret == NULL) { 121 /* Check errno to avoid shadowing other possible errors. */ 122 if (errno == EOK) { 123 errno = ERANGE; 124 } 125 } else { 126 /* Success, restore previous errno value. */ 127 errno = orig_errno; 128 } 129 113 if (ret == NULL && errno == EOK) { 114 errno = ERANGE; 115 } 130 116 return ret; 131 }132 133 /**134 * Change the current working directory.135 *136 * @param path New working directory.137 */138 int posix_chdir(const char *path)139 {140 return errnify(chdir, path);141 117 } 142 118 … … 181 157 /* There is currently no support for user accounts in HelenOS. */ 182 158 return 0; 183 }184 185 /**186 * Close a file.187 *188 * @param fildes189 * @return 0 on success, -1 on error.190 */191 int posix_close(int fildes)192 {193 return errnify(close, fildes);194 159 } 195 160 … … 204 169 ssize_t posix_read(int fildes, void *buf, size_t nbyte) 205 170 { 206 return errnify(read, fildes, buf, nbyte); 207 } 208 209 /** 210 * Write to a file. 211 * 212 * @param fildes File descriptor of the opened file. 213 * @param buf Buffer to write. 214 * @param nbyte Size of the buffer. 215 * @return Number of written bytes on success, -1 otherwise. 216 */ 217 ssize_t posix_write(int fildes, const void *buf, size_t nbyte) 218 { 219 return errnify(write, fildes, buf, nbyte); 220 } 221 222 /** 223 * Requests outstanding data to be written to the underlying storage device. 224 * 225 * @param fildes 226 */ 227 int posix_fsync(int fildes) 228 { 229 return errnify(fsync, fildes); 230 } 231 232 int posix_ftruncate(int fildes, posix_off_t length) 233 { 234 return errnify(ftruncate, fildes, (aoff64_t) length); 235 } 236 237 /** 238 * Remove a directory. 239 * 240 * @param path Directory pathname. 241 * @return Zero on success, -1 otherwise. 242 */ 243 int posix_rmdir(const char *path) 244 { 245 return errnify(rmdir, path); 171 int rc = read(fildes, buf, nbyte); 172 if (rc < 0) { 173 errno = -rc; 174 return -1; 175 } else { 176 return rc; 177 } 246 178 } 247 179 … … 254 186 int posix_unlink(const char *path) 255 187 { 256 return errnify(unlink, path); 257 } 258 259 int posix_dup(int fildes) 260 { 261 return posix_fcntl(fildes, F_DUPFD, 0); 262 } 263 264 int posix_dup2(int fildes, int fildes2) 265 { 266 return errnify(dup2, fildes, fildes2); 188 int rc = unlink(path); 189 if (rc < 0) { 190 errno = -rc; 191 return -1; 192 } else { 193 return rc; 194 } 267 195 } 268 196 … … 276 204 int posix_access(const char *path, int amode) 277 205 { 278 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) { 279 /* HelenOS doesn't support permissions, permission checks 280 * are equal to existence check. 281 * 282 * Check file existence by attempting to open it. 283 */ 206 if (amode == F_OK) { 207 /* Check file existence by attempt to open it. */ 284 208 int fd = open(path, O_RDONLY); 285 if (fd < 0) { 286 errno = -fd; 287 return -1; 209 if (fd != -1) { 210 close(fd); 288 211 } 289 close(fd); 212 return fd; 213 } else if (amode & (X_OK | W_OK | R_OK)) { 214 /* HelenOS doesn't support permissions, return success. */ 290 215 return 0; 291 216 } else { -
uspace/lib/posix/unistd.h
r1814ee4d rd85a01c 60 60 /* Working Directory */ 61 61 extern char *posix_getcwd(char *buf, size_t size); 62 extern int posix_chdir(const char *path);63 62 64 63 /* Query Memory Parameters */ … … 70 69 extern posix_gid_t posix_getgid(void); 71 70 72 /* File Manipulation*/73 extern int posix_close(int fildes);71 /* File Input/Output */ 72 extern ssize_t posix_read(int fildes, void *buf, size_t nbyte); 74 73 75 extern ssize_t posix_read(int fildes, void *buf, size_t nbyte); 76 extern ssize_t posix_write(int fildes, const void *buf, size_t nbyte); 77 78 extern int posix_fsync(int fildes); 79 extern int posix_ftruncate(int fildes, posix_off_t length); 80 81 extern int posix_rmdir(const char *path); 74 /* Deleting Files */ 82 75 extern int posix_unlink(const char *path); 83 84 extern int posix_dup(int fildes);85 extern int posix_dup2(int fildes, int fildes2);86 76 87 77 /* Standard Streams */ … … 154 144 155 145 #define getcwd posix_getcwd 156 #define chdir posix_chdir157 146 158 147 #define isatty posix_isatty … … 165 154 #define getgid posix_getgid 166 155 167 #define close posix_close168 156 #define read posix_read 169 #define write posix_write 170 #define fsync posix_fsync 171 #define ftruncate posix_ftruncate 172 #define rmdir posix_rmdir 157 173 158 #define unlink posix_unlink 174 #define dup posix_dup175 #define dup2 posix_dup2176 159 177 160 #define access posix_access
Note:
See TracChangeset
for help on using the changeset viewer.