Changeset 3e01316f in mainline for uspace/lib/posix/stdio.c
- Timestamp:
- 2011-08-17T18:04:50Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0cf27ee
- Parents:
- e898296d (diff), e6165be (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/stdio.c
re898296d r3e01316f 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 … … 273 284 fdi_node_t node = { 274 285 .fs_handle = st.fs_handle, 275 . devmap_handle = st.devmap_handle,286 .service_id = st.service_id, 276 287 .index = st.index 277 288 }; … … 306 317 307 318 return stream; 308 }309 310 /**311 *312 * @param buf313 * @param size314 * @param mode315 * @return316 */317 FILE *posix_fmemopen(void *restrict buf, size_t size,318 const char *restrict mode)319 {320 // TODO321 not_implemented();322 }323 324 /**325 *326 * @param bufp327 * @param sizep328 * @return329 */330 FILE *posix_open_memstream(char **bufp, size_t *sizep)331 {332 // TODO333 not_implemented();334 319 } 335 320 … … 724 709 * Rename a file or directory. 725 710 * 726 * @param old 727 * @param new 711 * @param old Old pathname. 712 * @param new New pathname. 728 713 * @return Zero on success, -1 (with errno set) otherwise. 729 714 */ … … 734 719 735 720 /** 736 * 737 * @param s 738 * @return 721 * Get a unique temporary file name (obsolete). 722 * 723 * @param s Buffer for the file name. Must be at least L_tmpnam bytes long. 724 * @return The value of s on success, NULL on failure. 739 725 */ 740 726 char *posix_tmpnam(char *s) 741 727 { 742 // TODO: low priority, just a compile-time dependency of binutils 743 not_implemented(); 728 assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX")); 729 730 static char buffer[L_tmpnam + 1]; 731 if (s == NULL) { 732 s = buffer; 733 } 734 735 posix_strcpy(s, "/tmp/tnXXXXXX"); 736 posix_mktemp(s); 737 738 if (*s == '\0') { 739 /* Errno set by mktemp(). */ 740 return NULL; 741 } 742 743 return s; 744 } 745 746 /** 747 * Get an unique temporary file name with additional constraints (obsolete). 748 * 749 * @param dir Path to directory, where the file should be created. 750 * @param pfx Optional prefix up to 5 characters long. 751 * @return Newly allocated unique path for temporary file. NULL on failure. 752 */ 753 char *posix_tempnam(const char *dir, const char *pfx) 754 { 755 /* Sequence number of the filename. */ 756 static int seq = 0; 757 758 size_t dir_len = posix_strlen(dir); 759 if (dir[dir_len - 1] == '/') { 760 dir_len--; 761 } 762 763 size_t pfx_len = posix_strlen(pfx); 764 if (pfx_len > 5) { 765 pfx_len = 5; 766 } 767 768 char *result = malloc(dir_len + /* slash*/ 1 + 769 pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1); 770 771 if (result == NULL) { 772 errno = ENOMEM; 773 return NULL; 774 } 775 776 char *res_ptr = result; 777 posix_strncpy(res_ptr, dir, dir_len); 778 res_ptr += dir_len; 779 posix_strncpy(res_ptr, pfx, pfx_len); 780 res_ptr += pfx_len; 781 782 for (; seq < 1000; ++seq) { 783 snprintf(res_ptr, 8, "%03d.tmp", seq); 784 785 int orig_errno = errno; 786 errno = 0; 787 /* Check if the file exists. */ 788 if (posix_access(result, F_OK) == -1) { 789 if (errno == ENOENT) { 790 errno = orig_errno; 791 break; 792 } else { 793 /* errno set by access() */ 794 return NULL; 795 } 796 } 797 } 798 799 if (seq == 1000) { 800 free(result); 801 errno = EINVAL; 802 return NULL; 803 } 804 805 return result; 806 } 807 808 /** 809 * Create and open an unique temporary file. 810 * The file is automatically removed when the stream is closed. 811 * 812 * @param dir Path to directory, where the file should be created. 813 * @param pfx Optional prefix up to 5 characters long. 814 * @return Newly allocated unique path for temporary file. NULL on failure. 815 */ 816 FILE *posix_tmpfile(void) 817 { 818 char filename[] = "/tmp/tfXXXXXX"; 819 int fd = posix_mkstemp(filename); 820 if (fd == -1) { 821 /* errno set by mkstemp(). */ 822 return NULL; 823 } 824 825 /* Unlink the created file, so that it's removed on close(). */ 826 posix_unlink(filename); 827 return fdopen(fd, "w+"); 744 828 } 745 829
Note:
See TracChangeset
for help on using the changeset viewer.