Changeset 1cc4a09 in mainline for uspace/lib/posix
- Timestamp:
- 2011-08-14T00:19:31Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b705ecc
- Parents:
- 6d100fd (diff), e0e922d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- uspace/lib/posix
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/signal.c
r6d100fd r1cc4a09 252 252 " or fully unsupported signal. This handler may only be" 253 253 " invoked by the raise() function, which may not be what" 254 " the application developer intended .\nSignal name");254 " the application developer intended"); 255 255 } 256 256 … … 360 360 } 361 361 362 if (signo > _TOP_SIGNAL) { 363 errno = EINVAL; 364 return -1; 365 } 366 362 367 if (pid == (posix_pid_t) task_get_id()) { 363 368 return posix_raise(signo); 364 }365 366 if (pid > _TOP_SIGNAL) {367 errno = EINVAL;368 return -1;369 369 } 370 370 -
uspace/lib/posix/stdio.c
r6d100fd r1cc4a09 44 44 #include "assert.h" 45 45 #include "errno.h" 46 #include "stdlib.h" 46 47 #include "string.h" 47 48 #include "sys/types.h" 49 #include "unistd.h" 48 50 49 51 #include "libc/io/printf_core.h" … … 255 257 assert(stream != NULL); 256 258 257 /* Ret ieve the node. */259 /* Retrieve the node. */ 258 260 struct stat st; 259 261 int rc; … … 263 265 } else { 264 266 rc = stat(filename, &st); 267 if (-rc == ENOENT) { 268 /* file does not exist, create new file */ 269 FILE* tmp = fopen(filename, mode); 270 if (tmp != NULL) { 271 fclose(tmp); 272 /* try again */ 273 rc = stat(filename, &st); 274 } 275 } 265 276 } 266 277 … … 724 735 * Rename a file or directory. 725 736 * 726 * @param old 727 * @param new 737 * @param old Old pathname. 738 * @param new New pathname. 728 739 * @return Zero on success, -1 (with errno set) otherwise. 729 740 */ … … 734 745 735 746 /** 736 * 737 * @param s 738 * @return 747 * Get a unique temporary file name (obsolete). 748 * 749 * @param s Buffer for the file name. Must be at least L_tmpnam bytes long. 750 * @return The value of s on success, NULL on failure. 739 751 */ 740 752 char *posix_tmpnam(char *s) 741 753 { 742 // TODO: low priority, just a compile-time dependency of binutils 743 not_implemented(); 754 assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX")); 755 756 static char buffer[L_tmpnam + 1]; 757 if (s == NULL) { 758 s = buffer; 759 } 760 761 posix_strcpy(s, "/tmp/tnXXXXXX"); 762 posix_mktemp(s); 763 764 if (*s == '\0') { 765 /* Errno set by mktemp(). */ 766 return NULL; 767 } 768 769 return s; 770 } 771 772 /** 773 * Get an unique temporary file name with additional constraints (obsolete). 774 * 775 * @param dir Path to directory, where the file should be created. 776 * @param pfx Optional prefix up to 5 characters long. 777 * @return Newly allocated unique path for temporary file. NULL on failure. 778 */ 779 char *posix_tempnam(const char *dir, const char *pfx) 780 { 781 /* Sequence number of the filename. */ 782 static int seq = 0; 783 784 size_t dir_len = posix_strlen(dir); 785 if (dir[dir_len - 1] == '/') { 786 dir_len--; 787 } 788 789 size_t pfx_len = posix_strlen(pfx); 790 if (pfx_len > 5) { 791 pfx_len = 5; 792 } 793 794 char *result = malloc(dir_len + /* slash*/ 1 + 795 pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1); 796 797 if (result == NULL) { 798 errno = ENOMEM; 799 return NULL; 800 } 801 802 char *res_ptr = result; 803 posix_strncpy(res_ptr, dir, dir_len); 804 res_ptr += dir_len; 805 posix_strncpy(res_ptr, pfx, pfx_len); 806 res_ptr += pfx_len; 807 808 for (; seq < 1000; ++seq) { 809 snprintf(res_ptr, 8, "%03d.tmp", seq); 810 811 int orig_errno = errno; 812 errno = 0; 813 /* Check if the file exists. */ 814 if (posix_access(result, F_OK) == -1) { 815 if (errno == ENOENT) { 816 errno = orig_errno; 817 break; 818 } else { 819 /* errno set by access() */ 820 return NULL; 821 } 822 } 823 } 824 825 if (seq == 1000) { 826 free(result); 827 errno = EINVAL; 828 return NULL; 829 } 830 831 return result; 832 } 833 834 /** 835 * Create and open an unique temporary file. 836 * The file is automatically removed when the stream is closed. 837 * 838 * @param dir Path to directory, where the file should be created. 839 * @param pfx Optional prefix up to 5 characters long. 840 * @return Newly allocated unique path for temporary file. NULL on failure. 841 */ 842 FILE *posix_tmpfile(void) 843 { 844 char filename[] = "/tmp/tfXXXXXX"; 845 int fd = posix_mkstemp(filename); 846 if (fd == -1) { 847 /* errno set by mkstemp(). */ 848 return NULL; 849 } 850 851 /* Unlink the created file, so that it's removed on close(). */ 852 posix_unlink(filename); 853 return fdopen(fd, "w+"); 744 854 } 745 855 -
uspace/lib/posix/stdio.h
r6d100fd r1cc4a09 123 123 #define L_tmpnam PATH_MAX 124 124 extern char *posix_tmpnam(char *s); 125 extern char *posix_tempnam(const char *dir, const char *pfx); 126 extern FILE *posix_tmpfile(void); 125 127 126 128 #ifndef LIBPOSIX_INTERNAL 129 /* DEBUG macro does not belong to POSIX stdio.h. Its unconditional 130 * definition in the native stdio.h causes unexpected behaviour of 131 * applications which uses their own DEBUG macro (e.g. debugging 132 * output is printed even if not desirable). */ 133 #undef DEBUG 134 127 135 #define ctermid posix_ctermid 128 136 … … 176 184 177 185 #define tmpnam posix_tmpnam 186 #define tempnam posix_tempnam 187 #define tmpfile posix_tmpfile 178 188 #endif 179 189 -
uspace/lib/posix/stdlib.c
r6d100fd r1cc4a09 40 40 41 41 #include "errno.h" 42 #include "fcntl.h" 42 43 #include "limits.h" 44 #include "string.h" 45 #include "sys/stat.h" 46 #include "unistd.h" 43 47 44 48 #include "libc/sort.h" … … 385 389 386 390 /** 387 * 388 * @param tmpl 389 * @return 391 * Creates and opens an unique temporary file from template. 392 * 393 * @param tmpl Template. Last six characters must be XXXXXX. 394 * @return The opened file descriptor or -1 on error. 395 */ 396 int posix_mkstemp(char *tmpl) 397 { 398 int fd = -1; 399 400 char *tptr = tmpl + posix_strlen(tmpl) - 6; 401 402 while (fd < 0) { 403 if (*posix_mktemp(tmpl) == '\0') { 404 /* Errno set by mktemp(). */ 405 return -1; 406 } 407 408 fd = open(tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 409 410 if (fd == -1) { 411 /* Restore template to it's original state. */ 412 snprintf(tptr, 7, "XXXXXX"); 413 } 414 } 415 416 return fd; 417 } 418 419 /** 420 * Creates an unique temporary file name from template. 421 * 422 * @param tmpl Template. Last six characters must be XXXXXX. 423 * @return The value of tmpl. The template is modified in place. 424 * If no temporary file name can be created, template is 425 * reduced to an empty string. 390 426 */ 391 427 char *posix_mktemp(char *tmpl) 392 428 { 393 // TODO: low priority, just a compile-time dependency of binutils 394 not_implemented(); 429 int tmpl_len = posix_strlen(tmpl); 430 if (tmpl_len < 6) { 431 errno = EINVAL; 432 *tmpl = '\0'; 433 return tmpl; 434 } 435 436 char *tptr = tmpl + tmpl_len - 6; 437 if (posix_strcmp(tptr, "XXXXXX") != 0) { 438 errno = EINVAL; 439 *tmpl = '\0'; 440 return tmpl; 441 } 442 443 static int seq = 0; 444 445 for (; seq < 1000000; ++seq) { 446 snprintf(tptr, 7, "%06d", seq); 447 448 int orig_errno = errno; 449 errno = 0; 450 /* Check if the file exists. */ 451 if (posix_access(tmpl, F_OK) == -1) { 452 if (errno == ENOENT) { 453 errno = orig_errno; 454 break; 455 } else { 456 /* errno set by access() */ 457 *tmpl = '\0'; 458 return tmpl; 459 } 460 } 461 } 462 463 if (seq == 10000000) { 464 errno = EEXIST; 465 *tmpl = '\0'; 466 return tmpl; 467 } 468 469 return tmpl; 395 470 } 396 471 -
uspace/lib/posix/stdlib.h
r6d100fd r1cc4a09 113 113 extern void posix_free(void *ptr); 114 114 115 /* Temporary Files */ 116 extern int posix_mkstemp(char *tmpl); 117 115 118 /* Legacy Declarations */ 116 119 extern char *posix_mktemp(char *tmpl); … … 158 161 #define free posix_free 159 162 163 #define mkstemp posix_mkstemp 164 160 165 #define mktemp posix_mktemp 161 166 #define getloadavg bsd_getloadavg -
uspace/lib/posix/sys/wait.c
r6d100fd r1cc4a09 67 67 68 68 /** 69 * Wait for any child process to stop or terminate. 69 70 * 70 * @param stat_ptr 71 * @return 71 * @param stat_ptr Location of the final status code of the child process. 72 * @return ID of the child process for which status is reported, 73 * -1 on signal interrupt, (pid_t)-1 otherwise. 72 74 */ 73 75 posix_pid_t posix_wait(int *stat_ptr) … … 79 81 80 82 /** 83 * Wait for a child process to stop or terminate. 81 84 * 82 * @param pid 83 * @param stat_ptr 84 * @param options 85 * @return 85 * @param pid What child process shall the caller wait for. See POSIX manual 86 * for details. 87 * @param stat_ptr Location of the final status code of the child process. 88 * @param options Constraints of the waiting. See POSIX manual for details. 89 * @return ID of the child process for which status is reported, 90 * -1 on signal interrupt, 0 if non-blocking wait is requested but there is 91 * no child process whose status can be reported, (pid_t)-1 otherwise. 86 92 */ 87 93 posix_pid_t posix_waitpid(posix_pid_t pid, int *stat_ptr, int options) -
uspace/lib/posix/unistd.c
r6d100fd r1cc4a09 186 186 * Close a file. 187 187 * 188 * @param fildes 188 * @param fildes File descriptor of the opened file. 189 189 * @return 0 on success, -1 on error. 190 190 */ … … 223 223 * Requests outstanding data to be written to the underlying storage device. 224 224 * 225 * @param fildes 225 * @param fildes File descriptor of the opened file. 226 * @return Zero on success, -1 otherwise. 226 227 */ 227 228 int posix_fsync(int fildes) … … 230 231 } 231 232 233 /** 234 * Truncate a file to a specified length. 235 * 236 * @param fildes File descriptor of the opened file. 237 * @param length New length of the file. 238 * @return Zero on success, -1 otherwise. 239 */ 232 240 int posix_ftruncate(int fildes, posix_off_t length) 233 241 { … … 257 265 } 258 266 267 /** 268 * Duplicate an open file descriptor. 269 * 270 * @param fildes File descriptor to be duplicated. 271 * @return On success, new file descriptor for the same file, otherwise -1. 272 */ 259 273 int posix_dup(int fildes) 260 274 { … … 262 276 } 263 277 278 /** 279 * Duplicate an open file descriptor. 280 * 281 * @param fildes File descriptor to be duplicated. 282 * @param fildes2 File descriptor to be paired with the same file description 283 * as is paired fildes. 284 * @return fildes2 on success, -1 otherwise. 285 */ 264 286 int posix_dup2(int fildes, int fildes2) 265 287 { -
uspace/lib/posix/unistd.h
r6d100fd r1cc4a09 72 72 /* File Manipulation */ 73 73 extern int posix_close(int fildes); 74 75 74 extern ssize_t posix_read(int fildes, void *buf, size_t nbyte); 76 75 extern ssize_t posix_write(int fildes, const void *buf, size_t nbyte); 77 78 76 extern int posix_fsync(int fildes); 79 77 extern int posix_ftruncate(int fildes, posix_off_t length); 80 81 78 extern int posix_rmdir(const char *path); 82 79 extern int posix_unlink(const char *path); 83 84 80 extern int posix_dup(int fildes); 85 81 extern int posix_dup2(int fildes, int fildes2);
Note:
See TracChangeset
for help on using the changeset viewer.
