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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e3480d5 was e3480d5, checked in by Maurizio Lombardi <m.lombardi85@…>, 12 years ago

add lseek() to the posix library, move SEEK_CUR, SEEK_SET and SEEK_END from stdio.h to unistd.h

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