Changeset 1cc4a09 in mainline for uspace/lib/posix/stdio.c
- Timestamp:
- 2011-08-14T00:19:31Z (14 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.