[b942a66] | 1 | /*
|
---|
[777832e] | 2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
[b942a66] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file C standard file manipulation functions
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <stdio.h>
|
---|
[777832e] | 37 | #include <str_error.h>
|
---|
[b942a66] | 38 | #include <vfs/vfs.h>
|
---|
| 39 |
|
---|
[777832e] | 40 | /** Get stream position.
|
---|
| 41 | *
|
---|
| 42 | * @param stream Stream
|
---|
| 43 | * @param pos Place to store position
|
---|
| 44 | *
|
---|
| 45 | * @return Zero on success, non-zero on failure
|
---|
| 46 | */
|
---|
| 47 | int fgetpos(FILE *stream, fpos_t *pos)
|
---|
| 48 | {
|
---|
| 49 | off64_t p;
|
---|
| 50 |
|
---|
| 51 | p = ftell64(stream);
|
---|
| 52 | if (p < 0)
|
---|
| 53 | return -1;
|
---|
| 54 |
|
---|
| 55 | pos->pos = p;
|
---|
| 56 | return 0;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /** Get stream position.
|
---|
| 60 | *
|
---|
| 61 | * @param stream Stream
|
---|
| 62 | * @param pos Position
|
---|
| 63 | *
|
---|
| 64 | * @return Zero on sucess, non-zero on failure
|
---|
| 65 | */
|
---|
| 66 | int fsetpos(FILE *stream, const fpos_t *pos)
|
---|
| 67 | {
|
---|
| 68 | int rc;
|
---|
| 69 |
|
---|
| 70 | rc = fseek64(stream, pos->pos, SEEK_SET);
|
---|
| 71 | if (rc < 0)
|
---|
| 72 | return -1;
|
---|
| 73 |
|
---|
| 74 | return 0;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[b942a66] | 77 | /** Rename file or directory (C standard) */
|
---|
| 78 | int rename(const char *old_path, const char *new_path)
|
---|
| 79 | {
|
---|
[b7fd2a0] | 80 | errno_t rc;
|
---|
[b942a66] | 81 |
|
---|
| 82 | rc = vfs_rename_path(old_path, new_path);
|
---|
| 83 | if (rc != EOK) {
|
---|
| 84 | errno = rc;
|
---|
| 85 | return -1;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | return 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /** Remove file or directory (C standard) */
|
---|
| 92 | int remove(const char *path)
|
---|
| 93 | {
|
---|
[b7fd2a0] | 94 | errno_t rc;
|
---|
[b942a66] | 95 |
|
---|
| 96 | rc = vfs_unlink_path(path);
|
---|
| 97 | if (rc != EOK) {
|
---|
| 98 | errno = rc;
|
---|
| 99 | return -1;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return 0;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[777832e] | 105 | /** Print error message and string representation of @c errno.
|
---|
| 106 | *
|
---|
| 107 | * @param s Error message
|
---|
| 108 | */
|
---|
| 109 | void perror(const char *s)
|
---|
| 110 | {
|
---|
| 111 | if (s != NULL && *s != '\0')
|
---|
| 112 | fprintf(stderr, "%s: %s\n", s, str_error(errno));
|
---|
| 113 | else
|
---|
| 114 | fprintf(stderr, "%s\n", str_error(errno));
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[b942a66] | 117 | /** @}
|
---|
| 118 | */
|
---|