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
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * Copyright (c) 2011 Petr Koupy
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
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Miscellaneous standard definitions.
34 */
35
36#define LIBPOSIX_INTERNAL
37
38#include "internal/common.h"
39#include "posix/unistd.h"
40
41#include "posix/errno.h"
42#include "posix/string.h"
43#include "posix/fcntl.h"
44
45#include "libc/task.h"
46#include "libc/stats.h"
47#include "libc/malloc.h"
48
49/* Array of environment variable strings (NAME=VALUE). */
50char **posix_environ = NULL;
51char *posix_optarg;
52
53/**
54 * Get current user name.
55 *
56 * @return User name (static) string or NULL if not found.
57 */
58char *posix_getlogin(void)
59{
60 /* There is currently no support for user accounts in HelenOS. */
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.
69 * @return Zero on success, error code otherwise.
70 */
71int posix_getlogin_r(char *name, size_t namesize)
72{
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 }
81}
82
83/**
84 * Test whether open file descriptor is associated with a terminal.
85 *
86 * @param fd Open file descriptor to test.
87 * @return Boolean result of the test.
88 */
89int posix_isatty(int fd)
90{
91 // TODO
92 /* Always returns false, because there is no easy way to find
93 * out under HelenOS. */
94 return 0;
95}
96
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 }
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
121 char *ret = getcwd(buf, size);
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;
130 }
131
132 return ret;
133}
134
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
145/**
146 * Determine the page size of the current run of the process.
147 *
148 * @return Page size of the process.
149 */
150int posix_getpagesize(void)
151{
152 return getpagesize();
153}
154
155/**
156 * Get the process ID of the calling process.
157 *
158 * @return Process ID.
159 */
160posix_pid_t posix_getpid(void)
161{
162 return task_get_id();
163}
164
165/**
166 * Get the real user ID of the calling process.
167 *
168 * @return User ID.
169 */
170posix_uid_t posix_getuid(void)
171{
172 /* There is currently no support for user accounts in HelenOS. */
173 return 0;
174}
175
176/**
177 * Get the real group ID of the calling process.
178 *
179 * @return Group ID.
180 */
181posix_gid_t posix_getgid(void)
182{
183 /* There is currently no support for user accounts in HelenOS. */
184 return 0;
185}
186
187/**
188 * Close a file.
189 *
190 * @param fildes File descriptor of the opened file.
191 * @return 0 on success, -1 on error.
192 */
193int posix_close(int fildes)
194{
195 return errnify(close, fildes);
196}
197
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{
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 *
227 * @param fildes File descriptor of the opened file.
228 * @return Zero on success, -1 otherwise.
229 */
230int posix_fsync(int fildes)
231{
232 return errnify(fsync, fildes);
233}
234
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 */
242int posix_ftruncate(int fildes, posix_off_t length)
243{
244 return errnify(ftruncate, fildes, (aoff64_t) length);
245}
246
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{
255 return errnify(rmdir, path);
256}
257
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{
266 return errnify(unlink, path);
267}
268
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 */
275int posix_dup(int fildes)
276{
277 return posix_fcntl(fildes, F_DUPFD, 0);
278}
279
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 */
288int posix_dup2(int fildes, int fildes2)
289{
290 return errnify(dup2, fildes, fildes2);
291}
292
293/**
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.
299 */
300int posix_access(const char *path, int amode)
301{
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 */
308 int fd = open(path, O_RDONLY);
309 if (fd < 0) {
310 errno = -fd;
311 return -1;
312 }
313 close(fd);
314 return 0;
315 } else {
316 /* Invalid amode argument. */
317 errno = EINVAL;
318 return -1;
319 }
320}
321
322/**
323 * Get configurable system variables.
324 *
325 * @param name Variable name.
326 * @return Variable value.
327 */
328long posix_sysconf(int name)
329{
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 }
336 if (cpu_stats) {
337 free(cpu_stats);
338 cpu_stats = 0;
339 }
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());
347 free(mem_stats);
348 mem_stats = 0;
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 }
364}
365
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
423/** @}
424 */
Note: See TracBrowser for help on using the repository browser.