source: mainline/uspace/lib/posix/unistd.c@ 903bd436

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 903bd436 was 2a53f71, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

Added missing comments to the newly implemented functions.

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