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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aa58e52 was 582a0b8, checked in by Jakub Jermar <jakub@…>, 9 years ago

Remove unistd.h

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