Changes in / [d85a01c:1814ee4d] in mainline
- Location:
- uspace/lib/posix
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/assert.h
rd85a01c r1814ee4d 40 40 41 41 #ifndef NDEBUG 42 #define assert(expr) \ 43 do { \ 44 if (!(expr)) { \ 45 assert_abort(#expr, __FILE__, __LINE__); \ 46 } \ 47 } while (0) 42 #define assert(expr) ((expr) ? (void) 0 : assert_abort(#expr, __FILE__, __LINE__)) 48 43 #else 49 44 #define assert(expr) ((void) 0) -
uspace/lib/posix/ctype.c
rd85a01c r1814ee4d 94 94 int posix_isprint(int c) 95 95 { 96 return !posix_iscntrl(c);96 return posix_isascii(c) && !posix_iscntrl(c); 97 97 } 98 98 -
uspace/lib/posix/fnmatch.c
rd85a01c r1814ee4d 525 525 static char *_casefold(const char *s) 526 526 { 527 assert(s != NULL); 527 528 char *result = strdup(s); 528 529 for (char *i = result; *i != '\0'; ++i) { … … 542 543 int posix_fnmatch(const char *pattern, const char *string, int flags) 543 544 { 545 assert(pattern != NULL); 546 assert(string != NULL); 547 544 548 // TODO: don't fold everything in advance, but only when needed 545 549 -
uspace/lib/posix/internal/common.h
rd85a01c r1814ee4d 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 45 55 #endif /* LIBPOSIX_COMMON_H_ */ 46 56 -
uspace/lib/posix/pwd.c
rd85a01c r1814ee4d 39 39 #include "errno.h" 40 40 41 // TODO: documentation42 43 41 static bool entry_read = false; 44 42 … … 46 44 static const struct posix_passwd dummy_pwd = { 47 45 .pw_name = (char *) "user", 48 .pw_uid = 1,49 .pw_gid = 1,46 .pw_uid = 0, 47 .pw_gid = 0, 50 48 .pw_dir = (char *) "/", 51 49 .pw_shell = (char *) "/app/bdsh" … … 115 113 { 116 114 assert(name != NULL); 115 assert(pwd != NULL); 116 assert(buffer != NULL); 117 assert(result != NULL); 117 118 118 119 if (posix_strcmp(name, "user") != 0) { … … 121 122 } 122 123 123 return posix_getpwuid_r( 1, pwd, buffer, bufsize, result);124 return posix_getpwuid_r(0, pwd, buffer, bufsize, result); 124 125 } 125 126 … … 132 133 struct posix_passwd *posix_getpwuid(posix_uid_t uid) 133 134 { 134 if (uid != 1) {135 if (uid != 0) { 135 136 return NULL; 136 137 } … … 159 160 '/', '\0', 'b', 'd', 's', 'h', '\0' }; 160 161 161 if (uid != 1) {162 if (uid != 0) { 162 163 *result = NULL; 163 164 return 0; … … 171 172 172 173 pwd->pw_name = (char *) bf; 173 pwd->pw_uid = 1;174 pwd->pw_gid = 1;174 pwd->pw_uid = 0; 175 pwd->pw_gid = 0; 175 176 pwd->pw_dir = (char *) bf + 5; 176 177 pwd->pw_shell = (char *) bf + 7; -
uspace/lib/posix/pwd.h
rd85a01c r1814ee4d 35 35 #ifndef POSIX_PWD_H_ 36 36 #define POSIX_PWD_H_ 37 38 // TODO: documentation39 37 40 38 #include "sys/types.h" -
uspace/lib/posix/signal.c
rd85a01c r1814ee4d 66 66 67 67 /** 68 * 69 * @param signo 68 * Default signal handler. Executes the default action for each signal, 69 * as reasonable within HelenOS. 70 * 71 * @param signo Signal number. 70 72 */ 71 73 void __posix_default_signal_handler(int signo) … … 75 77 abort(); 76 78 case SIGQUIT: 77 fprintf(stderr, "Quit signal raised. Exiting. ");79 fprintf(stderr, "Quit signal raised. Exiting.\n"); 78 80 exit(EXIT_FAILURE); 79 81 case SIGINT: 80 fprintf(stderr, "Interrupt signal caught. Exiting. ");82 fprintf(stderr, "Interrupt signal caught. Exiting.\n"); 81 83 exit(EXIT_FAILURE); 82 84 case SIGTERM: 83 fprintf(stderr, "Termination signal caught. Exiting. ");85 fprintf(stderr, "Termination signal caught. Exiting.\n"); 84 86 exit(EXIT_FAILURE); 85 87 case SIGSTOP: 86 fprintf(stderr, "Stop signal caught, but unsupported. Ignoring. ");88 fprintf(stderr, "Stop signal caught, but unsupported. Ignoring.\n"); 87 89 break; 88 90 case SIGKILL: -
uspace/lib/posix/stdio.c
rd85a01c r1814ee4d 50 50 #include "libc/str.h" 51 51 #include "libc/malloc.h" 52 #include "libc/adt/list.h" 53 #include "libc/sys/stat.h" 52 54 53 55 … … 252 254 assert(mode != NULL); 253 255 assert(stream != NULL); 254 256 257 /* Retieve the node. */ 258 struct stat st; 259 int rc; 260 255 261 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; 262 rc = fstat(stream->fd, &st); 263 } else { 264 rc = stat(filename, &st); 265 } 266 267 if (rc != EOK) { 268 fclose(stream); 269 errno = -rc; 265 270 return NULL; 266 271 } 267 268 FILE* copy = malloc(sizeof(FILE)); 269 if (copy == NULL) { 270 errno = ENOMEM; 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. */ 271 284 return NULL; 272 285 } 273 memcpy(copy, stream, sizeof(FILE)); 274 fclose(copy); /* copy is now freed */ 275 276 copy = fopen(filename, mode); /* open new stream */ 277 if (copy == NULL) { 278 /* fopen() sets errno */ 279 return NULL; 280 } 281 282 /* move the new stream to the original location */ 283 memcpy(stream, copy, sizeof (FILE)); 284 free(copy); 285 286 /* update references in the file list */ 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. */ 287 304 stream->link.next->prev = &stream->link; 288 305 stream->link.prev->next = &stream->link; … … 676 693 677 694 /** 678 * Remove a file .695 * Remove a file or directory. 679 696 * 680 697 * @param path Pathname of the file that shall be removed. 681 * @return Zero on success, -1 otherwise.698 * @return Zero on success, -1 (with errno set) otherwise. 682 699 */ 683 700 int posix_remove(const char *path) 684 701 { 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); 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); 688 733 } 689 734 -
uspace/lib/posix/stdio.h
rd85a01c r1814ee4d 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 118 121 /* Temporary Files */ 119 122 #undef L_tmpnam … … 170 173 #define remove posix_remove 171 174 175 #define rename posix_rename 176 172 177 #define tmpnam posix_tmpnam 173 178 #endif -
uspace/lib/posix/sys/wait.c
rd85a01c r1814ee4d 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 reason 63 * for unexpected termination at the moment. 64 */ 65 return SIGABRT; 66 } 67 41 68 /** 42 69 * … … 46 73 posix_pid_t posix_wait(int *stat_ptr) 47 74 { 48 // TODO: low priority, just a compile-time dependency of binutils 49 not_implemented(); 75 /* HelenOS does not support this. */ 76 errno = ENOSYS; 77 return (posix_pid_t) -1; 50 78 } 51 79 … … 59 87 posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options) 60 88 { 61 // TODO: low priority, just a compile-time dependency of binutils 62 not_implemented(); 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; 63 113 } 64 114 -
uspace/lib/posix/sys/wait.h
rd85a01c r1814ee4d 38 38 #include "types.h" 39 39 40 #undef WIFEXITED 41 #undef WEXITSTATUS 42 #undef WIFSIGNALED 43 #undef WTERMSIG 44 #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 40 54 extern posix_pid_t posix_wait(int *stat_ptr); 41 55 extern posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options); … … 43 57 #ifndef LIBPOSIX_INTERNAL 44 58 #define wait posix_wait 45 #define waitpid 59 #define waitpid posix_waitpid 46 60 #endif 47 61 -
uspace/lib/posix/unistd.c
rd85a01c r1814ee4d 110 110 return NULL; 111 111 } 112 113 /* Save the original value to comply with the "no modification on 114 * success" semantics. 115 */ 116 int orig_errno = errno; 117 errno = EOK; 118 112 119 char *ret = getcwd(buf, size); 113 if (ret == NULL && errno == EOK) { 114 errno = ERANGE; 115 } 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 116 130 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); 117 141 } 118 142 … … 157 181 /* There is currently no support for user accounts in HelenOS. */ 158 182 return 0; 183 } 184 185 /** 186 * Close a file. 187 * 188 * @param fildes 189 * @return 0 on success, -1 on error. 190 */ 191 int posix_close(int fildes) 192 { 193 return errnify(close, fildes); 159 194 } 160 195 … … 169 204 ssize_t posix_read(int fildes, void *buf, size_t nbyte) 170 205 { 171 int rc = read(fildes, buf, nbyte); 172 if (rc < 0) { 173 errno = -rc; 174 return -1; 175 } else { 176 return rc; 177 } 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); 178 246 } 179 247 … … 186 254 int posix_unlink(const char *path) 187 255 { 188 int rc = unlink(path); 189 if (rc < 0) { 190 errno = -rc; 191 return -1; 192 } else { 193 return rc; 194 } 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); 195 267 } 196 268 … … 204 276 int posix_access(const char *path, int amode) 205 277 { 206 if (amode == F_OK) { 207 /* Check file existence by attempt to open it. */ 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 */ 208 284 int fd = open(path, O_RDONLY); 209 if (fd != -1) { 210 close(fd); 285 if (fd < 0) { 286 errno = -fd; 287 return -1; 211 288 } 212 return fd; 213 } else if (amode & (X_OK | W_OK | R_OK)) { 214 /* HelenOS doesn't support permissions, return success. */ 289 close(fd); 215 290 return 0; 216 291 } else { -
uspace/lib/posix/unistd.h
rd85a01c r1814ee4d 60 60 /* Working Directory */ 61 61 extern char *posix_getcwd(char *buf, size_t size); 62 extern int posix_chdir(const char *path); 62 63 63 64 /* Query Memory Parameters */ … … 69 70 extern posix_gid_t posix_getgid(void); 70 71 71 /* File Input/Output */ 72 /* File Manipulation */ 73 extern int posix_close(int fildes); 74 72 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); 73 77 74 /* Deleting Files */ 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); 75 82 extern int posix_unlink(const char *path); 83 84 extern int posix_dup(int fildes); 85 extern int posix_dup2(int fildes, int fildes2); 76 86 77 87 /* Standard Streams */ … … 144 154 145 155 #define getcwd posix_getcwd 156 #define chdir posix_chdir 146 157 147 158 #define isatty posix_isatty … … 154 165 #define getgid posix_getgid 155 166 167 #define close posix_close 156 168 #define read posix_read 157 169 #define write posix_write 170 #define fsync posix_fsync 171 #define ftruncate posix_ftruncate 172 #define rmdir posix_rmdir 158 173 #define unlink posix_unlink 174 #define dup posix_dup 175 #define dup2 posix_dup2 159 176 160 177 #define access posix_access
Note:
See TracChangeset
for help on using the changeset viewer.