source: mainline/uspace/lib/posix/source/unistd.c@ f2c3fed

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

UNIX-like I/O functions should use errno to return error code for many reasons.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * Copyright (c) 2011 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Miscellaneous standard definitions.
34 */
35
36#define LIBPOSIX_INTERNAL
37#define __POSIX_DEF__(x) posix_##x
38
39#include "internal/common.h"
40#include "posix/unistd.h"
41
42#include "posix/errno.h"
43#include "posix/string.h"
44#include "posix/fcntl.h"
45
46#include "libc/task.h"
47#include "libc/stats.h"
48#include "libc/malloc.h"
49
50/* Array of environment variable strings (NAME=VALUE). */
51char **posix_environ = NULL;
52char *posix_optarg;
53
54/**
55 * Get current user name.
56 *
57 * @return User name (static) string or NULL if not found.
58 */
59char *posix_getlogin(void)
60{
61 /* There is currently no support for user accounts in HelenOS. */
62 return (char *) "user";
63}
64
65/**
66 * Get current user name.
67 *
68 * @param name Pointer to a user supplied buffer.
69 * @param namesize Length of the buffer.
70 * @return Zero on success, error code otherwise.
71 */
72int posix_getlogin_r(char *name, size_t namesize)
73{
74 /* There is currently no support for user accounts in HelenOS. */
75 if (namesize >= 5) {
76 posix_strcpy(name, (char *) "user");
77 return 0;
78 } else {
79 errno = ERANGE;
80 return -1;
81 }
82}
83
84/**
85 * Test whether open file descriptor is associated with a terminal.
86 *
87 * @param fd Open file descriptor to test.
88 * @return Boolean result of the test.
89 */
90int posix_isatty(int fd)
91{
92 // TODO
93 /* Always returns false, because there is no easy way to find
94 * out under HelenOS. */
95 return 0;
96}
97
98/**
99 * Get the pathname of the current working directory.
100 *
101 * @param buf Buffer into which the pathname shall be put.
102 * @param size Size of the buffer.
103 * @return Buffer pointer on success, NULL on failure.
104 */
105char *posix_getcwd(char *buf, size_t size)
106{
107 char *p = getcwd(buf, size);
108
109 if (p == NULL) {
110 errno = -errno;
111 return NULL;
112 }
113
114 return p;
115}
116
117/**
118 * Change the current working directory.
119 *
120 * @param path New working directory.
121 */
122int posix_chdir(const char *path)
123{
124 return negerrno(chdir, path);
125}
126
127/**
128 * Determine the page size of the current run of the process.
129 *
130 * @return Page size of the process.
131 */
132int posix_getpagesize(void)
133{
134 return getpagesize();
135}
136
137/**
138 * Get the process ID of the calling process.
139 *
140 * @return Process ID.
141 */
142posix_pid_t posix_getpid(void)
143{
144 return task_get_id();
145}
146
147/**
148 * Get the real user ID of the calling process.
149 *
150 * @return User ID.
151 */
152posix_uid_t posix_getuid(void)
153{
154 /* There is currently no support for user accounts in HelenOS. */
155 return 0;
156}
157
158/**
159 * Get the real group ID of the calling process.
160 *
161 * @return Group ID.
162 */
163posix_gid_t posix_getgid(void)
164{
165 /* There is currently no support for user accounts in HelenOS. */
166 return 0;
167}
168
169/**
170 * Close a file.
171 *
172 * @param fildes File descriptor of the opened file.
173 * @return 0 on success, -1 on error.
174 */
175int posix_close(int fildes)
176{
177 return negerrno(close, fildes);
178}
179
180/**
181 * Read from a file.
182 *
183 * @param fildes File descriptor of the opened file.
184 * @param buf Buffer to which the read bytes shall be stored.
185 * @param nbyte Upper limit on the number of read bytes.
186 * @return Number of read bytes on success, -1 otherwise.
187 */
188ssize_t posix_read(int fildes, void *buf, size_t nbyte)
189{
190 return negerrno(read, fildes, buf, nbyte);
191}
192
193/**
194 * Write to a file.
195 *
196 * @param fildes File descriptor of the opened file.
197 * @param buf Buffer to write.
198 * @param nbyte Size of the buffer.
199 * @return Number of written bytes on success, -1 otherwise.
200 */
201ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
202{
203 return negerrno(write, fildes, buf, nbyte);
204}
205
206/**
207 * Reposition read/write file offset
208 *
209 * @param fildes File descriptor of the opened file.
210 * @param offset New offset in the file.
211 * @param whence The position from which the offset argument is specified.
212 * @return Upon successful completion, returns the resulting offset
213 * as measured in bytes from the beginning of the file, -1 otherwise.
214 */
215posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
216{
217 return negerrno(lseek, fildes, offset, whence);
218}
219
220/**
221 * Requests outstanding data to be written to the underlying storage device.
222 *
223 * @param fildes File descriptor of the opened file.
224 * @return Zero on success, -1 otherwise.
225 */
226int posix_fsync(int fildes)
227{
228 return negerrno(fsync, fildes);
229}
230
231/**
232 * Truncate a file to a specified length.
233 *
234 * @param fildes File descriptor of the opened file.
235 * @param length New length of the file.
236 * @return Zero on success, -1 otherwise.
237 */
238int posix_ftruncate(int fildes, posix_off_t length)
239{
240 return negerrno(ftruncate, fildes, (aoff64_t) length);
241}
242
243/**
244 * Remove a directory.
245 *
246 * @param path Directory pathname.
247 * @return Zero on success, -1 otherwise.
248 */
249int posix_rmdir(const char *path)
250{
251 return negerrno(rmdir, path);
252}
253
254/**
255 * Remove a link to a file.
256 *
257 * @param path File pathname.
258 * @return Zero on success, -1 otherwise.
259 */
260int posix_unlink(const char *path)
261{
262 return negerrno(unlink, path);
263}
264
265/**
266 * Duplicate an open file descriptor.
267 *
268 * @param fildes File descriptor to be duplicated.
269 * @return On success, new file descriptor for the same file, otherwise -1.
270 */
271int posix_dup(int fildes)
272{
273 return posix_fcntl(fildes, F_DUPFD, 0);
274}
275
276/**
277 * Duplicate an open file descriptor.
278 *
279 * @param fildes File descriptor to be duplicated.
280 * @param fildes2 File descriptor to be paired with the same file description
281 * as is paired fildes.
282 * @return fildes2 on success, -1 otherwise.
283 */
284int posix_dup2(int fildes, int fildes2)
285{
286 return negerrno(dup2, fildes, fildes2);
287}
288
289/**
290 * Determine accessibility of a file.
291 *
292 * @param path File to check accessibility for.
293 * @param amode Either check for existence or intended access mode.
294 * @return Zero on success, -1 otherwise.
295 */
296int posix_access(const char *path, int amode)
297{
298 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
299 /* HelenOS doesn't support permissions, permission checks
300 * are equal to existence check.
301 *
302 * Check file existence by attempting to open it.
303 */
304 int fd = negerrno(open, path, O_RDONLY);
305 if (fd < 0) {
306 /* errno was set by open() */
307 return -1;
308 }
309 close(fd);
310 return 0;
311 } else {
312 /* Invalid amode argument. */
313 errno = EINVAL;
314 return -1;
315 }
316}
317
318/**
319 * Get configurable system variables.
320 *
321 * @param name Variable name.
322 * @return Variable value.
323 */
324long posix_sysconf(int name)
325{
326 long clk_tck = 0;
327 size_t cpu_count = 0;
328 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
329 if (cpu_stats && cpu_count > 0) {
330 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
331 }
332 if (cpu_stats) {
333 free(cpu_stats);
334 cpu_stats = 0;
335 }
336
337 long phys_pages = 0;
338 long avphys_pages = 0;
339 stats_physmem_t *mem_stats = stats_get_physmem();
340 if (mem_stats) {
341 phys_pages = (long) (mem_stats->total / getpagesize());
342 avphys_pages = (long) (mem_stats->free / getpagesize());
343 free(mem_stats);
344 mem_stats = 0;
345 }
346
347 switch (name) {
348 case _SC_PHYS_PAGES:
349 return phys_pages;
350 case _SC_AVPHYS_PAGES:
351 return avphys_pages;
352 case _SC_PAGESIZE:
353 return getpagesize();
354 case _SC_CLK_TCK:
355 return clk_tck;
356 default:
357 errno = EINVAL;
358 return -1;
359 }
360}
361
362/**
363 *
364 * @param path
365 * @param name
366 * @return
367 */
368long posix_pathconf(const char *path, int name)
369{
370 // TODO: low priority, just a compile-time dependency of binutils
371 not_implemented();
372 return -1;
373}
374
375/**
376 *
377 * @return
378 */
379posix_pid_t posix_fork(void)
380{
381 // TODO: low priority, just a compile-time dependency of binutils
382 not_implemented();
383 return -1;
384}
385
386/**
387 *
388 * @param path
389 * @param argv
390 * @return
391 */
392int posix_execv(const char *path, char *const argv[])
393{
394 // TODO: low priority, just a compile-time dependency of binutils
395 not_implemented();
396 return -1;
397}
398
399/**
400 *
401 * @param file
402 * @param argv
403 * @return
404 */
405int posix_execvp(const char *file, char *const argv[])
406{
407 // TODO: low priority, just a compile-time dependency of binutils
408 not_implemented();
409 return -1;
410}
411
412/**
413 *
414 * @param fildes
415 * @return
416 */
417int posix_pipe(int fildes[2])
418{
419 // TODO: low priority, just a compile-time dependency of binutils
420 not_implemented();
421 return -1;
422}
423
424unsigned int posix_alarm(unsigned int seconds)
425{
426 not_implemented();
427 return 0;
428}
429
430/** @}
431 */
Note: See TracBrowser for help on using the repository browser.