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 <stdbool.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <str.h>
|
---|
39 | #include <str_error.h>
|
---|
40 | #include <tmpfile.h>
|
---|
41 | #include <vfs/vfs.h>
|
---|
42 |
|
---|
43 | /** Static buffer for tmpnam function */
|
---|
44 | static char tmpnam_buf[L_tmpnam];
|
---|
45 |
|
---|
46 | /** Get stream position.
|
---|
47 | *
|
---|
48 | * @param stream Stream
|
---|
49 | * @param pos Place to store position
|
---|
50 | *
|
---|
51 | * @return Zero on success, non-zero on failure
|
---|
52 | */
|
---|
53 | int fgetpos(FILE *stream, fpos_t *pos)
|
---|
54 | {
|
---|
55 | off64_t p;
|
---|
56 |
|
---|
57 | p = ftell64(stream);
|
---|
58 | if (p < 0)
|
---|
59 | return -1;
|
---|
60 |
|
---|
61 | pos->pos = p;
|
---|
62 | return 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Get stream position.
|
---|
66 | *
|
---|
67 | * @param stream Stream
|
---|
68 | * @param pos Position
|
---|
69 | *
|
---|
70 | * @return Zero on sucess, non-zero on failure
|
---|
71 | */
|
---|
72 | int fsetpos(FILE *stream, const fpos_t *pos)
|
---|
73 | {
|
---|
74 | int rc;
|
---|
75 |
|
---|
76 | rc = fseek64(stream, pos->pos, SEEK_SET);
|
---|
77 | if (rc < 0)
|
---|
78 | return -1;
|
---|
79 |
|
---|
80 | return 0;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /** Rename file or directory (C standard) */
|
---|
84 | int rename(const char *old_path, const char *new_path)
|
---|
85 | {
|
---|
86 | errno_t rc;
|
---|
87 |
|
---|
88 | rc = vfs_rename_path(old_path, new_path);
|
---|
89 | if (rc != EOK) {
|
---|
90 | /*
|
---|
91 | * Note that ISO C leaves the value of errno undefined,
|
---|
92 | * whereas according to UN*X standards, it is set.
|
---|
93 | */
|
---|
94 | errno = rc;
|
---|
95 | return -1;
|
---|
96 | }
|
---|
97 |
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** Remove file or directory (C standard) */
|
---|
102 | int remove(const char *path)
|
---|
103 | {
|
---|
104 | errno_t rc;
|
---|
105 |
|
---|
106 | rc = vfs_unlink_path(path);
|
---|
107 | if (rc != EOK) {
|
---|
108 | /*
|
---|
109 | * Note that ISO C leaves the value of errno undefined,
|
---|
110 | * whereas according to UN*X standards, it is set.
|
---|
111 | */
|
---|
112 | errno = rc;
|
---|
113 | return -1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Create a temporary file.
|
---|
120 | *
|
---|
121 | * @return Open stream descriptor or @c NULL on error. In that case
|
---|
122 | * errno is set (UN*X). Note that ISO C leaves the value of errno
|
---|
123 | * undefined.
|
---|
124 | */
|
---|
125 | FILE *tmpfile(void)
|
---|
126 | {
|
---|
127 | int file;
|
---|
128 | FILE *stream;
|
---|
129 |
|
---|
130 | file = __tmpfile();
|
---|
131 | if (file < 0) {
|
---|
132 | printf("file is < 0\n");
|
---|
133 | errno = EEXIST;
|
---|
134 | return NULL;
|
---|
135 | }
|
---|
136 |
|
---|
137 | stream = fdopen(file, "w+");
|
---|
138 | if (stream == NULL) {
|
---|
139 | printf("stream = NULL\n");
|
---|
140 | vfs_put(file);
|
---|
141 | errno = EACCES;
|
---|
142 | return NULL;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return stream;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Create name for a temporary file.
|
---|
149 | *
|
---|
150 | * @param s Pointer to array of at least L_tmpnam bytes or @c NULL.
|
---|
151 | * @return The pointer @a s or pointer to internal static buffer on success,
|
---|
152 | * @c NULL on error.
|
---|
153 | */
|
---|
154 | char *tmpnam(char *s)
|
---|
155 | {
|
---|
156 | char *p;
|
---|
157 |
|
---|
158 | p = (s != NULL) ? s : tmpnam_buf;
|
---|
159 | return __tmpnam(p);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** Print error message and string representation of @c errno.
|
---|
163 | *
|
---|
164 | * @param s Error message
|
---|
165 | */
|
---|
166 | void perror(const char *s)
|
---|
167 | {
|
---|
168 | if (s != NULL && *s != '\0')
|
---|
169 | fprintf(stderr, "%s: %s\n", s, str_error(errno));
|
---|
170 | else
|
---|
171 | fprintf(stderr, "%s\n", str_error(errno));
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /** @}
|
---|
176 | */
|
---|