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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b5b5d84 was fcab7ef, checked in by Jakub Jermar <jakub@…>, 9 years ago

Merge dup2() into vfs_clone()

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