source: mainline/uspace/lib/posix/src/unistd.c@ 3061bc1

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

style: Remove trailing whitespace on non-empty lines, in certain file types.

Command used: tools/srepl '\([^[:space:]]\)\s\+$' '\1' -- *.c *.h *.py *.sh *.s *.S *.ag

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