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

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

Implemented and commented some functions from unistd.c.

  • Property mode set to 100644
File size: 5.8 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 */
33/** @file
34 */
35
[491e1ee]36#define LIBPOSIX_INTERNAL
[4f4b4e7]37
[9b1503e]38#include "internal/common.h"
[a16210b5]39#include "unistd.h"
[823a929]40#include <task.h>
[fb872c1]41#include <errno.h>
42#include "string.h"
43#include "fcntl.h"
44#include <stats.h>
45#include "libc/malloc.h"
[a16210b5]46
[b4d6252]47/* Array of environment variable strings (NAME=VALUE). */
[7530a00]48char **posix_environ = NULL;
[b4d6252]49
[244d6fd]50/**
51 * Get current user name.
[fb872c1]52 *
53 * @return User name (static) string or NULL if not found.
[244d6fd]54 */
55char *posix_getlogin(void)
56{
[fb872c1]57 /* There is currently no support for user accounts in HelenOS. */
[244d6fd]58 return (char *) "user";
59}
60
61/**
62 * Get current user name.
63 *
64 * @param name Pointer to a user supplied buffer.
65 * @param namesize Length of the buffer.
[fb872c1]66 * @return Zero on success, error code otherwise.
[244d6fd]67 */
68int posix_getlogin_r(char *name, size_t namesize)
69{
[fb872c1]70 /* There is currently no support for user accounts in HelenOS. */
71 if (namesize >= 5) {
72 posix_strcpy(name, (char *) "user");
73 return 0;
74 } else {
75 errno = ERANGE;
76 return -1;
77 }
[244d6fd]78}
79
[4f4b4e7]80/**
[fb872c1]81 * Test whether open file descriptor is associated with a terminal.
[8d7e82c1]82 *
[fb872c1]83 * @param fd Open file descriptor to test.
84 * @return Boolean result of the test.
[4f4b4e7]85 */
86int posix_isatty(int fd)
87{
[fb872c1]88 /* Always returns false, because there is no easy way to find
89 * out under HelenOS. */
[8d7e82c1]90 return false;
[a16210b5]91}
92
[72ec8cc]93/**
[fb872c1]94 * Determine the page size of the current run of the process.
95 *
96 * @return Page size of the process.
[72ec8cc]97 */
98int posix_getpagesize(void)
99{
100 return getpagesize();
101}
102
[823a929]103/**
[fb872c1]104 * Get the process ID of the calling process.
105 *
106 * @return Process ID.
[823a929]107 */
108posix_pid_t posix_getpid(void)
109{
110 return task_get_id();
111}
112
[59f799b]113/**
[fb872c1]114 * Get the real user ID of the calling process.
[59f799b]115 *
[fb872c1]116 * @return User ID.
[59f799b]117 */
118posix_uid_t posix_getuid(void)
119{
[fb872c1]120 /* There is currently no support for user accounts in HelenOS. */
121 return 0;
[59f799b]122}
123
124/**
[fb872c1]125 * Get the real group ID of the calling process.
[59f799b]126 *
[fb872c1]127 * @return Group ID.
[59f799b]128 */
129posix_gid_t posix_getgid(void)
130{
[fb872c1]131 /* There is currently no support for user accounts in HelenOS. */
132 return 0;
[59f799b]133}
134
[b08ef1fd]135/**
[fb872c1]136 * Determine accessibility of a file.
137 *
138 * @param path File to check accessibility for.
139 * @param amode Either check for existence or intended access mode.
140 * @return Zero on success, -1 otherwise.
[b08ef1fd]141 */
142int posix_access(const char *path, int amode)
143{
[fb872c1]144 if (amode == F_OK) {
145 /* Check file existence by attempt to open it. */
146 int fd = open(path, O_RDONLY);
147 // TODO: propagate a POSIX compatible errno
148 int rc = fd < 0 ? -1 : 0;
149 close(fd);
150 return rc;
151 } else if (amode & (X_OK | W_OK | R_OK)) {
152 /* HelenOS doesn't support permissions, return success. */
153 return 0;
154 } else {
155 /* Invalid amode argument. */
156 errno = EINVAL;
157 return -1;
158 }
[b08ef1fd]159}
160
[59f799b]161/**
[fb872c1]162 * Get configurable system variables.
[59f799b]163 *
[fb872c1]164 * @param name Variable name.
165 * @return Variable value.
[59f799b]166 */
167long posix_sysconf(int name)
168{
[fb872c1]169 long clk_tck = 0;
170 size_t cpu_count = 0;
171 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
172 if (cpu_stats && cpu_count > 0) {
173 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
174 }
175 free(cpu_stats);
176 cpu_stats = 0;
177
178 long phys_pages = 0;
179 long avphys_pages = 0;
180 stats_physmem_t *mem_stats = stats_get_physmem();
181 if (mem_stats) {
182 phys_pages = (long) (mem_stats->total / getpagesize());
183 avphys_pages = (long) (mem_stats->free / getpagesize());
184 }
185 free(mem_stats);
186 mem_stats = 0;
187
188 switch (name) {
189 case _SC_PHYS_PAGES:
190 return phys_pages;
191 case _SC_AVPHYS_PAGES:
192 return avphys_pages;
193 case _SC_PAGESIZE:
194 return getpagesize();
195 case _SC_CLK_TCK:
196 return clk_tck;
197 default:
198 errno = EINVAL;
199 return -1;
200 }
[59f799b]201}
202
[823a929]203/**
204 *
205 * @param path
206 * @param name
207 * @return
208 */
209long posix_pathconf(const char *path, int name)
210{
211 // TODO: low priority, just a compile-time dependency of binutils
212 not_implemented();
213}
214
215/**
216 *
217 * @return
218 */
219posix_pid_t posix_fork(void)
220{
221 // TODO: low priority, just a compile-time dependency of binutils
222 not_implemented();
223}
224
225/**
226 *
227 * @param path
228 * @param argv
229 * @return
230 */
231int posix_execv(const char *path, char *const argv[])
232{
233 // TODO: low priority, just a compile-time dependency of binutils
234 not_implemented();
235}
236
237/**
238 *
239 * @param file
240 * @param argv
241 * @return
242 */
243int posix_execvp(const char *file, char *const argv[])
244{
245 // TODO: low priority, just a compile-time dependency of binutils
246 not_implemented();
247}
248
249/**
250 *
251 * @param fildes
252 * @return
253 */
254int posix_pipe(int fildes[2])
255{
256 // TODO: low priority, just a compile-time dependency of binutils
257 not_implemented();
258}
259
[4f4b4e7]260/** @}
261 */
Note: See TracBrowser for help on using the repository browser.