source: mainline/uspace/lib/c/generic/stdio.c@ 777832e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 777832e was 777832e, checked in by Jiri Svoboda <jiri@…>, 7 years ago

fgetpos, fsetpos, perror.

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * Copyright (c) 2018 Jiri Svoboda
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>
37#include <str_error.h>
38#include <vfs/vfs.h>
39
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 */
47int 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 */
66int 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
77/** Rename file or directory (C standard) */
78int rename(const char *old_path, const char *new_path)
79{
80 errno_t rc;
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) */
92int remove(const char *path)
93{
94 errno_t rc;
95
96 rc = vfs_unlink_path(path);
97 if (rc != EOK) {
98 errno = rc;
99 return -1;
100 }
101
102 return 0;
103}
104
105/** Print error message and string representation of @c errno.
106 *
107 * @param s Error message
108 */
109void 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
117/** @}
118 */
Note: See TracBrowser for help on using the repository browser.