source: mainline/uspace/lib/posix/source/unistd.c@ 0d0b319

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0d0b319 was 0d0b319, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Flip error constants to positive values, and update libposix for the change.

  • Property mode set to 100644
File size: 10.7 KB
RevLine 
[a16210b5]1/*
2 * Copyright (c) 2011 Jiri Zarevucky
[4f4b4e7]3 * Copyright (c) 2011 Petr Koupy
[a16210b5]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
[4f4b4e7]30/** @addtogroup libposix
31 * @{
32 */
[4cf8ca6]33/** @file Miscellaneous standard definitions.
[4f4b4e7]34 */
35
[491e1ee]36#define LIBPOSIX_INTERNAL
[fdf97f6]37#define __POSIX_DEF__(x) posix_##x
[4f4b4e7]38
[9b1503e]39#include "internal/common.h"
[a3da2b2]40#include "posix/unistd.h"
[ec18957a]41
[0d0b319]42#include <errno.h>
43
[a3da2b2]44#include "posix/string.h"
45#include "posix/fcntl.h"
[ec18957a]46
47#include "libc/task.h"
[1165a419]48#include "libc/thread.h"
[ec18957a]49#include "libc/stats.h"
[fb872c1]50#include "libc/malloc.h"
[58898d1d]51#include "libc/vfs/vfs.h"
52
[582a0b8]53#include <libarch/config.h>
54
[0d0b319]55// FIXME: replace with a hash table
[58898d1d]56aoff64_t posix_pos[MAX_OPEN_FILES];
[a16210b5]57
[b4d6252]58/* Array of environment variable strings (NAME=VALUE). */
[7530a00]59char **posix_environ = NULL;
[a92f13d]60char *posix_optarg;
[b4d6252]61
[1165a419]62/**
63 * Sleep for the specified number of seconds.
64 *
65 * Note that POSIX allows this call to be interrupted and then the return
66 * value represents remaining seconds for the sleep. HelenOS does not offer
67 * such functionality and thus always the whole sleep is taken.
68 *
69 * @param seconds Number of seconds to sleep.
70 * @return Always 0 on HelenOS.
71 */
72unsigned int posix_sleep(unsigned int seconds)
73{
74 return thread_sleep(seconds);
75}
76
[244d6fd]77/**
78 * Get current user name.
[fb872c1]79 *
80 * @return User name (static) string or NULL if not found.
[244d6fd]81 */
82char *posix_getlogin(void)
83{
[fb872c1]84 /* There is currently no support for user accounts in HelenOS. */
[244d6fd]85 return (char *) "user";
86}
87
88/**
89 * Get current user name.
90 *
91 * @param name Pointer to a user supplied buffer.
92 * @param namesize Length of the buffer.
[fb872c1]93 * @return Zero on success, error code otherwise.
[244d6fd]94 */
95int posix_getlogin_r(char *name, size_t namesize)
96{
[fb872c1]97 /* There is currently no support for user accounts in HelenOS. */
98 if (namesize >= 5) {
99 posix_strcpy(name, (char *) "user");
100 return 0;
101 } else {
102 errno = ERANGE;
103 return -1;
104 }
[244d6fd]105}
106
[4f4b4e7]107/**
[fb872c1]108 * Test whether open file descriptor is associated with a terminal.
[8d7e82c1]109 *
[fb872c1]110 * @param fd Open file descriptor to test.
111 * @return Boolean result of the test.
[4f4b4e7]112 */
113int posix_isatty(int fd)
114{
[4c8f5e7]115 // TODO
[fb872c1]116 /* Always returns false, because there is no easy way to find
[d542aad]117 * out under HelenOS. */
[94f8b81c]118 return 0;
[a16210b5]119}
120
[221afc9e]121/**
122 * Get the pathname of the current working directory.
123 *
124 * @param buf Buffer into which the pathname shall be put.
125 * @param size Size of the buffer.
126 * @return Buffer pointer on success, NULL on failure.
127 */
128char *posix_getcwd(char *buf, size_t size)
129{
[0d0b319]130 if (failed(vfs_cwd_get(buf, size)))
[221afc9e]131 return NULL;
[d96d9bc]132 return buf;
[221afc9e]133}
134
[75406dc]135/**
136 * Change the current working directory.
137 *
138 * @param path New working directory.
139 */
140int posix_chdir(const char *path)
141{
[0d0b319]142 if (failed(vfs_cwd_set(path)))
[d96d9bc]143 return -1;
144 return 0;
[75406dc]145}
146
[72ec8cc]147/**
[fb872c1]148 * Determine the page size of the current run of the process.
149 *
150 * @return Page size of the process.
[72ec8cc]151 */
152int posix_getpagesize(void)
153{
[582a0b8]154 return PAGE_SIZE;
[72ec8cc]155}
156
[823a929]157/**
[fb872c1]158 * Get the process ID of the calling process.
159 *
160 * @return Process ID.
[823a929]161 */
162posix_pid_t posix_getpid(void)
163{
164 return task_get_id();
165}
166
[59f799b]167/**
[fb872c1]168 * Get the real user ID of the calling process.
[59f799b]169 *
[fb872c1]170 * @return User ID.
[59f799b]171 */
172posix_uid_t posix_getuid(void)
173{
[fb872c1]174 /* There is currently no support for user accounts in HelenOS. */
175 return 0;
[59f799b]176}
177
178/**
[fb872c1]179 * Get the real group ID of the calling process.
[59f799b]180 *
[fb872c1]181 * @return Group ID.
[59f799b]182 */
183posix_gid_t posix_getgid(void)
184{
[fb872c1]185 /* There is currently no support for user accounts in HelenOS. */
186 return 0;
[59f799b]187}
188
[75406dc]189/**
190 * Close a file.
191 *
[2a53f71]192 * @param fildes File descriptor of the opened file.
[75406dc]193 * @return 0 on success, -1 on error.
194 */
195int posix_close(int fildes)
196{
[58898d1d]197 posix_pos[fildes] = 0;
[0d0b319]198 if (failed(vfs_put(fildes)))
[9c4cf0d]199 return -1;
200 else
201 return 0;
[75406dc]202}
203
[221afc9e]204/**
205 * Read from a file.
206 *
207 * @param fildes File descriptor of the opened file.
208 * @param buf Buffer to which the read bytes shall be stored.
209 * @param nbyte Upper limit on the number of read bytes.
210 * @return Number of read bytes on success, -1 otherwise.
211 */
212ssize_t posix_read(int fildes, void *buf, size_t nbyte)
213{
[8e3498b]214 size_t nread;
[0d0b319]215 if (failed(vfs_read(fildes, &posix_pos[fildes], buf, nbyte, &nread)))
[ce04ea44]216 return -1;
[8e3498b]217 return (ssize_t) nread;
[75406dc]218}
219
220/**
221 * Write to a file.
222 *
223 * @param fildes File descriptor of the opened file.
224 * @param buf Buffer to write.
225 * @param nbyte Size of the buffer.
226 * @return Number of written bytes on success, -1 otherwise.
227 */
228ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
229{
[8e3498b]230 size_t nwr;
[0d0b319]231 if (failed(vfs_write(fildes, &posix_pos[fildes], buf, nbyte, &nwr)))
[ce04ea44]232 return -1;
[8e3498b]233 return nwr;
[75406dc]234}
235
[e3480d5]236/**
237 * Reposition read/write file offset
238 *
239 * @param fildes File descriptor of the opened file.
[bf963b9]240 * @param offset New offset in the file.
241 * @param whence The position from which the offset argument is specified.
[e3480d5]242 * @return Upon successful completion, returns the resulting offset
243 * as measured in bytes from the beginning of the file, -1 otherwise.
244 */
245posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
246{
[58898d1d]247 struct stat st;
248
249 switch (whence) {
250 case SEEK_SET:
251 posix_pos[fildes] = offset;
252 break;
253 case SEEK_CUR:
254 posix_pos[fildes] += offset;
255 break;
256 case SEEK_END:
[0d0b319]257 if (failed(vfs_stat(fildes, &st)))
[58898d1d]258 return -1;
259 posix_pos[fildes] = st.size + offset;
260 break;
261 }
[59f388a]262 if (posix_pos[fildes] > INT64_MAX) {
263 /* The native width is too large for the POSIX interface. */
264 errno = ERANGE;
265 return -1;
266 }
[58898d1d]267 return posix_pos[fildes];
[e3480d5]268}
269
[75406dc]270/**
271 * Requests outstanding data to be written to the underlying storage device.
272 *
[2a53f71]273 * @param fildes File descriptor of the opened file.
274 * @return Zero on success, -1 otherwise.
[75406dc]275 */
276int posix_fsync(int fildes)
277{
[0d0b319]278 if (failed(vfs_sync(fildes)))
[a56cef9]279 return -1;
280 else
281 return 0;
[75406dc]282}
283
[2a53f71]284/**
285 * Truncate a file to a specified length.
286 *
287 * @param fildes File descriptor of the opened file.
288 * @param length New length of the file.
289 * @return Zero on success, -1 otherwise.
290 */
[75406dc]291int posix_ftruncate(int fildes, posix_off_t length)
292{
[0d0b319]293 if (failed(vfs_resize(fildes, (aoff64_t) length)))
[67e881c]294 return -1;
295 else
296 return 0;
[221afc9e]297}
298
[58115ae]299/**
300 * Remove a directory.
301 *
302 * @param path Directory pathname.
303 * @return Zero on success, -1 otherwise.
304 */
305int posix_rmdir(const char *path)
306{
[0d0b319]307 if (failed(vfs_unlink_path(path)))
[79ea5af]308 return -1;
309 else
310 return 0;
[58115ae]311}
312
[221afc9e]313/**
314 * Remove a link to a file.
315 *
316 * @param path File pathname.
317 * @return Zero on success, -1 otherwise.
318 */
319int posix_unlink(const char *path)
320{
[0d0b319]321 if (failed(vfs_unlink_path(path)))
[79ea5af]322 return -1;
323 else
324 return 0;
[75406dc]325}
326
[2a53f71]327/**
328 * Duplicate an open file descriptor.
329 *
330 * @param fildes File descriptor to be duplicated.
331 * @return On success, new file descriptor for the same file, otherwise -1.
332 */
[75406dc]333int posix_dup(int fildes)
334{
335 return posix_fcntl(fildes, F_DUPFD, 0);
336}
337
[2a53f71]338/**
339 * Duplicate an open file descriptor.
340 *
341 * @param fildes File descriptor to be duplicated.
342 * @param fildes2 File descriptor to be paired with the same file description
343 * as is paired fildes.
344 * @return fildes2 on success, -1 otherwise.
345 */
[75406dc]346int posix_dup2(int fildes, int fildes2)
347{
[f77c1c9]348 int file;
[0d0b319]349 if (failed(vfs_clone(fildes, fildes2, false, &file))) {
[f77c1c9]350 return -1;
351 }
352 return file;
[221afc9e]353}
354
[b08ef1fd]355/**
[fb872c1]356 * Determine accessibility of a file.
357 *
358 * @param path File to check accessibility for.
359 * @param amode Either check for existence or intended access mode.
360 * @return Zero on success, -1 otherwise.
[b08ef1fd]361 */
362int posix_access(const char *path, int amode)
363{
[75406dc]364 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
365 /* HelenOS doesn't support permissions, permission checks
366 * are equal to existence check.
367 *
368 * Check file existence by attempting to open it.
369 */
[b19e892]370 int fd = posix_open(path, O_RDONLY);
371 if (fd < 0)
[955c2b0]372 return -1;
[9c4cf0d]373 posix_close(fd);
[955c2b0]374 return 0;
[fb872c1]375 } else {
376 /* Invalid amode argument. */
377 errno = EINVAL;
378 return -1;
379 }
[b08ef1fd]380}
381
[59f799b]382/**
[fb872c1]383 * Get configurable system variables.
[59f799b]384 *
[fb872c1]385 * @param name Variable name.
386 * @return Variable value.
[59f799b]387 */
388long posix_sysconf(int name)
389{
[fb872c1]390 long clk_tck = 0;
391 size_t cpu_count = 0;
392 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
393 if (cpu_stats && cpu_count > 0) {
394 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
395 }
[94f8b81c]396 if (cpu_stats) {
397 free(cpu_stats);
398 cpu_stats = 0;
399 }
[fb872c1]400
401 long phys_pages = 0;
402 long avphys_pages = 0;
403 stats_physmem_t *mem_stats = stats_get_physmem();
404 if (mem_stats) {
[582a0b8]405 phys_pages = (long) (mem_stats->total / posix_getpagesize());
406 avphys_pages = (long) (mem_stats->free / posix_getpagesize());
[94f8b81c]407 free(mem_stats);
408 mem_stats = 0;
[fb872c1]409 }
410
411 switch (name) {
412 case _SC_PHYS_PAGES:
413 return phys_pages;
414 case _SC_AVPHYS_PAGES:
415 return avphys_pages;
416 case _SC_PAGESIZE:
[582a0b8]417 return posix_getpagesize();
[fb872c1]418 case _SC_CLK_TCK:
419 return clk_tck;
420 default:
421 errno = EINVAL;
422 return -1;
423 }
[59f799b]424}
425
[823a929]426/**
427 *
428 * @param path
429 * @param name
430 * @return
431 */
432long posix_pathconf(const char *path, int name)
433{
434 // TODO: low priority, just a compile-time dependency of binutils
435 not_implemented();
[820104d]436 return -1;
[823a929]437}
438
439/**
440 *
441 * @return
442 */
443posix_pid_t posix_fork(void)
444{
445 // TODO: low priority, just a compile-time dependency of binutils
446 not_implemented();
[820104d]447 return -1;
[823a929]448}
449
450/**
451 *
452 * @param path
453 * @param argv
454 * @return
455 */
456int posix_execv(const char *path, char *const argv[])
457{
458 // TODO: low priority, just a compile-time dependency of binutils
459 not_implemented();
[820104d]460 return -1;
[823a929]461}
462
463/**
464 *
465 * @param file
466 * @param argv
467 * @return
468 */
469int posix_execvp(const char *file, char *const argv[])
470{
471 // TODO: low priority, just a compile-time dependency of binutils
472 not_implemented();
[820104d]473 return -1;
[823a929]474}
475
476/**
477 *
478 * @param fildes
479 * @return
480 */
481int posix_pipe(int fildes[2])
482{
483 // TODO: low priority, just a compile-time dependency of binutils
484 not_implemented();
[820104d]485 return -1;
[823a929]486}
487
[5759642f]488unsigned int posix_alarm(unsigned int seconds)
489{
490 not_implemented();
491 return 0;
492}
493
[4f4b4e7]494/** @}
495 */
Note: See TracBrowser for help on using the repository browser.