Changes in uspace/lib/posix/stdio.c [75406dc:cfbb5d18] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/stdio.c
r75406dc rcfbb5d18 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
Note:
See TracChangeset
for help on using the changeset viewer.