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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a3da2b2 was a3da2b2, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Introduce include/ and source/ directories into libposix

  • Property mode set to 100644
File size: 9.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
[4f4b4e7]37
[9b1503e]38#include "internal/common.h"
[a3da2b2]39#include "posix/unistd.h"
[ec18957a]40
[a3da2b2]41#include "posix/errno.h"
42#include "posix/string.h"
43#include "posix/fcntl.h"
[ec18957a]44
45#include "libc/task.h"
46#include "libc/stats.h"
[fb872c1]47#include "libc/malloc.h"
[a16210b5]48
[b4d6252]49/* Array of environment variable strings (NAME=VALUE). */
[7530a00]50char **posix_environ = NULL;
[a92f13d]51char *posix_optarg;
[b4d6252]52
[244d6fd]53/**
54 * Get current user name.
[fb872c1]55 *
56 * @return User name (static) string or NULL if not found.
[244d6fd]57 */
58char *posix_getlogin(void)
59{
[fb872c1]60 /* There is currently no support for user accounts in HelenOS. */
[244d6fd]61 return (char *) "user";
62}
63
64/**
65 * Get current user name.
66 *
67 * @param name Pointer to a user supplied buffer.
68 * @param namesize Length of the buffer.
[fb872c1]69 * @return Zero on success, error code otherwise.
[244d6fd]70 */
71int posix_getlogin_r(char *name, size_t namesize)
72{
[fb872c1]73 /* There is currently no support for user accounts in HelenOS. */
74 if (namesize >= 5) {
75 posix_strcpy(name, (char *) "user");
76 return 0;
77 } else {
78 errno = ERANGE;
79 return -1;
80 }
[244d6fd]81}
82
[4f4b4e7]83/**
[fb872c1]84 * Test whether open file descriptor is associated with a terminal.
[8d7e82c1]85 *
[fb872c1]86 * @param fd Open file descriptor to test.
87 * @return Boolean result of the test.
[4f4b4e7]88 */
89int posix_isatty(int fd)
90{
[4c8f5e7]91 // TODO
[fb872c1]92 /* Always returns false, because there is no easy way to find
[d542aad]93 * out under HelenOS. */
[94f8b81c]94 return 0;
[a16210b5]95}
96
[221afc9e]97/**
98 * Get the pathname of the current working directory.
99 *
100 * @param buf Buffer into which the pathname shall be put.
101 * @param size Size of the buffer.
102 * @return Buffer pointer on success, NULL on failure.
103 */
104char *posix_getcwd(char *buf, size_t size)
105{
106 /* Native getcwd() does not set any errno despite the fact that general
107 * usage pattern of this function depends on it (caller is repeatedly
108 * guessing the required size of the buffer by checking for ERANGE on
109 * failure). */
110 if (size == 0) {
111 errno = EINVAL;
112 return NULL;
113 }
[9350bfdd]114
115 /* Save the original value to comply with the "no modification on
116 * success" semantics.
117 */
118 int orig_errno = errno;
119 errno = EOK;
120
[221afc9e]121 char *ret = getcwd(buf, size);
[9350bfdd]122 if (ret == NULL) {
123 /* Check errno to avoid shadowing other possible errors. */
124 if (errno == EOK) {
125 errno = ERANGE;
126 }
127 } else {
128 /* Success, restore previous errno value. */
129 errno = orig_errno;
[221afc9e]130 }
[9350bfdd]131
[221afc9e]132 return ret;
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{
142 return errnify(chdir, path);
143}
144
[72ec8cc]145/**
[fb872c1]146 * Determine the page size of the current run of the process.
147 *
148 * @return Page size of the process.
[72ec8cc]149 */
150int posix_getpagesize(void)
151{
152 return getpagesize();
153}
154
[823a929]155/**
[fb872c1]156 * Get the process ID of the calling process.
157 *
158 * @return Process ID.
[823a929]159 */
160posix_pid_t posix_getpid(void)
161{
162 return task_get_id();
163}
164
[59f799b]165/**
[fb872c1]166 * Get the real user ID of the calling process.
[59f799b]167 *
[fb872c1]168 * @return User ID.
[59f799b]169 */
170posix_uid_t posix_getuid(void)
171{
[fb872c1]172 /* There is currently no support for user accounts in HelenOS. */
173 return 0;
[59f799b]174}
175
176/**
[fb872c1]177 * Get the real group ID of the calling process.
[59f799b]178 *
[fb872c1]179 * @return Group ID.
[59f799b]180 */
181posix_gid_t posix_getgid(void)
182{
[fb872c1]183 /* There is currently no support for user accounts in HelenOS. */
184 return 0;
[59f799b]185}
186
[75406dc]187/**
188 * Close a file.
189 *
[2a53f71]190 * @param fildes File descriptor of the opened file.
[75406dc]191 * @return 0 on success, -1 on error.
192 */
193int posix_close(int fildes)
194{
195 return errnify(close, fildes);
196}
197
[221afc9e]198/**
199 * Read from a file.
200 *
201 * @param fildes File descriptor of the opened file.
202 * @param buf Buffer to which the read bytes shall be stored.
203 * @param nbyte Upper limit on the number of read bytes.
204 * @return Number of read bytes on success, -1 otherwise.
205 */
206ssize_t posix_read(int fildes, void *buf, size_t nbyte)
207{
[75406dc]208 return errnify(read, fildes, buf, nbyte);
209}
210
211/**
212 * Write to a file.
213 *
214 * @param fildes File descriptor of the opened file.
215 * @param buf Buffer to write.
216 * @param nbyte Size of the buffer.
217 * @return Number of written bytes on success, -1 otherwise.
218 */
219ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
220{
221 return errnify(write, fildes, buf, nbyte);
222}
223
224/**
225 * Requests outstanding data to be written to the underlying storage device.
226 *
[2a53f71]227 * @param fildes File descriptor of the opened file.
228 * @return Zero on success, -1 otherwise.
[75406dc]229 */
230int posix_fsync(int fildes)
231{
232 return errnify(fsync, fildes);
233}
234
[2a53f71]235/**
236 * Truncate a file to a specified length.
237 *
238 * @param fildes File descriptor of the opened file.
239 * @param length New length of the file.
240 * @return Zero on success, -1 otherwise.
241 */
[75406dc]242int posix_ftruncate(int fildes, posix_off_t length)
243{
244 return errnify(ftruncate, fildes, (aoff64_t) length);
[221afc9e]245}
246
[58115ae]247/**
248 * Remove a directory.
249 *
250 * @param path Directory pathname.
251 * @return Zero on success, -1 otherwise.
252 */
253int posix_rmdir(const char *path)
254{
[75406dc]255 return errnify(rmdir, path);
[58115ae]256}
257
[221afc9e]258/**
259 * Remove a link to a file.
260 *
261 * @param path File pathname.
262 * @return Zero on success, -1 otherwise.
263 */
264int posix_unlink(const char *path)
265{
[75406dc]266 return errnify(unlink, path);
267}
268
[2a53f71]269/**
270 * Duplicate an open file descriptor.
271 *
272 * @param fildes File descriptor to be duplicated.
273 * @return On success, new file descriptor for the same file, otherwise -1.
274 */
[75406dc]275int posix_dup(int fildes)
276{
277 return posix_fcntl(fildes, F_DUPFD, 0);
278}
279
[2a53f71]280/**
281 * Duplicate an open file descriptor.
282 *
283 * @param fildes File descriptor to be duplicated.
284 * @param fildes2 File descriptor to be paired with the same file description
285 * as is paired fildes.
286 * @return fildes2 on success, -1 otherwise.
287 */
[75406dc]288int posix_dup2(int fildes, int fildes2)
289{
290 return errnify(dup2, fildes, fildes2);
[221afc9e]291}
292
[b08ef1fd]293/**
[fb872c1]294 * Determine accessibility of a file.
295 *
296 * @param path File to check accessibility for.
297 * @param amode Either check for existence or intended access mode.
298 * @return Zero on success, -1 otherwise.
[b08ef1fd]299 */
300int posix_access(const char *path, int amode)
301{
[75406dc]302 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
303 /* HelenOS doesn't support permissions, permission checks
304 * are equal to existence check.
305 *
306 * Check file existence by attempting to open it.
307 */
[fb872c1]308 int fd = open(path, O_RDONLY);
[75406dc]309 if (fd < 0) {
310 errno = -fd;
[955c2b0]311 return -1;
[ec18957a]312 }
[75406dc]313 close(fd);
[955c2b0]314 return 0;
[fb872c1]315 } else {
316 /* Invalid amode argument. */
317 errno = EINVAL;
318 return -1;
319 }
[b08ef1fd]320}
321
[59f799b]322/**
[fb872c1]323 * Get configurable system variables.
[59f799b]324 *
[fb872c1]325 * @param name Variable name.
326 * @return Variable value.
[59f799b]327 */
328long posix_sysconf(int name)
329{
[fb872c1]330 long clk_tck = 0;
331 size_t cpu_count = 0;
332 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
333 if (cpu_stats && cpu_count > 0) {
334 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
335 }
[94f8b81c]336 if (cpu_stats) {
337 free(cpu_stats);
338 cpu_stats = 0;
339 }
[fb872c1]340
341 long phys_pages = 0;
342 long avphys_pages = 0;
343 stats_physmem_t *mem_stats = stats_get_physmem();
344 if (mem_stats) {
345 phys_pages = (long) (mem_stats->total / getpagesize());
346 avphys_pages = (long) (mem_stats->free / getpagesize());
[94f8b81c]347 free(mem_stats);
348 mem_stats = 0;
[fb872c1]349 }
350
351 switch (name) {
352 case _SC_PHYS_PAGES:
353 return phys_pages;
354 case _SC_AVPHYS_PAGES:
355 return avphys_pages;
356 case _SC_PAGESIZE:
357 return getpagesize();
358 case _SC_CLK_TCK:
359 return clk_tck;
360 default:
361 errno = EINVAL;
362 return -1;
363 }
[59f799b]364}
365
[823a929]366/**
367 *
368 * @param path
369 * @param name
370 * @return
371 */
372long posix_pathconf(const char *path, int name)
373{
374 // TODO: low priority, just a compile-time dependency of binutils
375 not_implemented();
376}
377
378/**
379 *
380 * @return
381 */
382posix_pid_t posix_fork(void)
383{
384 // TODO: low priority, just a compile-time dependency of binutils
385 not_implemented();
386}
387
388/**
389 *
390 * @param path
391 * @param argv
392 * @return
393 */
394int posix_execv(const char *path, char *const argv[])
395{
396 // TODO: low priority, just a compile-time dependency of binutils
397 not_implemented();
398}
399
400/**
401 *
402 * @param file
403 * @param argv
404 * @return
405 */
406int posix_execvp(const char *file, char *const argv[])
407{
408 // TODO: low priority, just a compile-time dependency of binutils
409 not_implemented();
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}
422
[4f4b4e7]423/** @}
424 */
Note: See TracBrowser for help on using the repository browser.