Changeset 00c2de63 in mainline for uspace/lib/posix/stdio.c
- Timestamp:
- 2011-07-29T14:50:22Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b6759f4
- Parents:
- 6c69d19 (diff), 7ae249d (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
r6c69d19 r00c2de63 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
Note:
See TracChangeset
for help on using the changeset viewer.