source: mainline/uspace/lib/posix/src/unistd.c@ 3b1cc8d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3b1cc8d was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 10.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#include "internal/common.h"
37#include "posix/unistd.h"
38
39#include <errno.h>
40
41#include "posix/string.h"
42#include "posix/fcntl.h"
43
44#include "libc/task.h"
45#include "libc/thread.h"
46#include "libc/stats.h"
47#include "libc/malloc.h"
48#include "libc/vfs/vfs.h"
49
50#include <libarch/config.h>
51
52// FIXME: replace with a hash table
53aoff64_t posix_pos[MAX_OPEN_FILES];
54
55/* Array of environment variable strings (NAME=VALUE). */
56char **environ = NULL;
57
58/**
59 * Sleep for the specified number of seconds.
60 *
61 * Note that POSIX allows this call to be interrupted and then the return
62 * value represents remaining seconds for the sleep. HelenOS does not offer
63 * such functionality and thus always the whole sleep is taken.
64 *
65 * @param seconds Number of seconds to sleep.
66 * @return Always 0 on HelenOS.
67 */
68unsigned int sleep(unsigned int seconds)
69{
70 return thread_sleep(seconds);
71}
72
73/**
74 * Get current user name.
75 *
76 * @return User name (static) string or NULL if not found.
77 */
78char *getlogin(void)
79{
80 /* There is currently no support for user accounts in HelenOS. */
81 return (char *) "user";
82}
83
84/**
85 * Get current user name.
86 *
87 * @param name Pointer to a user supplied buffer.
88 * @param namesize Length of the buffer.
89 * @return Zero on success, error code otherwise.
90 */
91int getlogin_r(char *name, size_t namesize)
92{
93 /* There is currently no support for user accounts in HelenOS. */
94 if (namesize >= 5) {
95 strcpy(name, (char *) "user");
96 return 0;
97 } else {
98 errno = ERANGE;
99 return -1;
100 }
101}
102
103/**
104 * Test whether open file descriptor is associated with a terminal.
105 *
106 * @param fd Open file descriptor to test.
107 * @return Boolean result of the test.
108 */
109int isatty(int fd)
110{
111 // TODO
112 /*
113 * Always returns false, because there is no easy way to find
114 * out under HelenOS.
115 */
116 return 0;
117}
118
119/**
120 * Get the pathname of the current working directory.
121 *
122 * @param buf Buffer into which the pathname shall be put.
123 * @param size Size of the buffer.
124 * @return Buffer pointer on success, NULL on failure.
125 */
126char *getcwd(char *buf, size_t size)
127{
128 if (failed(vfs_cwd_get(buf, size)))
129 return NULL;
130 return buf;
131}
132
133/**
134 * Change the current working directory.
135 *
136 * @param path New working directory.
137 */
138int chdir(const char *path)
139{
140 if (failed(vfs_cwd_set(path)))
141 return -1;
142 return 0;
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 getpagesize(void)
151{
152 return PAGE_SIZE;
153}
154
155/**
156 * Get the process ID of the calling process.
157 *
158 * @return Process ID.
159 */
160pid_t 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 */
170uid_t 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 */
181gid_t 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 close(int fildes)
194{
195 posix_pos[fildes] = 0;
196 if (failed(vfs_put(fildes)))
197 return -1;
198 else
199 return 0;
200}
201
202/**
203 * Read from a file.
204 *
205 * @param fildes File descriptor of the opened file.
206 * @param buf Buffer to which the read bytes shall be stored.
207 * @param nbyte Upper limit on the number of read bytes.
208 * @return Number of read bytes on success, -1 otherwise.
209 */
210ssize_t read(int fildes, void *buf, size_t nbyte)
211{
212 size_t nread;
213 if (failed(vfs_read(fildes, &posix_pos[fildes], buf, nbyte, &nread)))
214 return -1;
215 return (ssize_t) nread;
216}
217
218/**
219 * Write to a file.
220 *
221 * @param fildes File descriptor of the opened file.
222 * @param buf Buffer to write.
223 * @param nbyte Size of the buffer.
224 * @return Number of written bytes on success, -1 otherwise.
225 */
226ssize_t write(int fildes, const void *buf, size_t nbyte)
227{
228 size_t nwr;
229 if (failed(vfs_write(fildes, &posix_pos[fildes], buf, nbyte, &nwr)))
230 return -1;
231 return nwr;
232}
233
234/**
235 * Reposition read/write file offset
236 *
237 * @param fildes File descriptor of the opened file.
238 * @param offset New offset in the file.
239 * @param whence The position from which the offset argument is specified.
240 * @return Upon successful completion, returns the resulting offset
241 * as measured in bytes from the beginning of the file, -1 otherwise.
242 */
243off_t lseek(int fildes, off_t offset, int whence)
244{
245 vfs_stat_t st;
246
247 switch (whence) {
248 case SEEK_SET:
249 posix_pos[fildes] = offset;
250 break;
251 case SEEK_CUR:
252 posix_pos[fildes] += offset;
253 break;
254 case SEEK_END:
255 if (failed(vfs_stat(fildes, &st)))
256 return -1;
257 posix_pos[fildes] = st.size + offset;
258 break;
259 }
260 if (posix_pos[fildes] > INT64_MAX) {
261 /* The native width is too large for the POSIX interface. */
262 errno = ERANGE;
263 return -1;
264 }
265 return posix_pos[fildes];
266}
267
268/**
269 * Requests outstanding data to be written to the underlying storage device.
270 *
271 * @param fildes File descriptor of the opened file.
272 * @return Zero on success, -1 otherwise.
273 */
274int fsync(int fildes)
275{
276 if (failed(vfs_sync(fildes)))
277 return -1;
278 else
279 return 0;
280}
281
282/**
283 * Truncate a file to a specified length.
284 *
285 * @param fildes File descriptor of the opened file.
286 * @param length New length of the file.
287 * @return Zero on success, -1 otherwise.
288 */
289int ftruncate(int fildes, off_t length)
290{
291 if (failed(vfs_resize(fildes, (aoff64_t) length)))
292 return -1;
293 else
294 return 0;
295}
296
297/**
298 * Remove a directory.
299 *
300 * @param path Directory pathname.
301 * @return Zero on success, -1 otherwise.
302 */
303int rmdir(const char *path)
304{
305 if (failed(vfs_unlink_path(path)))
306 return -1;
307 else
308 return 0;
309}
310
311/**
312 * Remove a link to a file.
313 *
314 * @param path File pathname.
315 * @return Zero on success, -1 otherwise.
316 */
317int unlink(const char *path)
318{
319 if (failed(vfs_unlink_path(path)))
320 return -1;
321 else
322 return 0;
323}
324
325/**
326 * Duplicate an open file descriptor.
327 *
328 * @param fildes File descriptor to be duplicated.
329 * @return On success, new file descriptor for the same file, otherwise -1.
330 */
331int dup(int fildes)
332{
333 return fcntl(fildes, F_DUPFD, 0);
334}
335
336/**
337 * Duplicate an open file descriptor.
338 *
339 * @param fildes File descriptor to be duplicated.
340 * @param fildes2 File descriptor to be paired with the same file description
341 * as is paired fildes.
342 * @return fildes2 on success, -1 otherwise.
343 */
344int dup2(int fildes, int fildes2)
345{
346 int file;
347 if (failed(vfs_clone(fildes, fildes2, false, &file))) {
348 return -1;
349 }
350 return file;
351}
352
353/**
354 * Determine accessibility of a file.
355 *
356 * @param path File to check accessibility for.
357 * @param amode Either check for existence or intended access mode.
358 * @return Zero on success, -1 otherwise.
359 */
360int access(const char *path, int amode)
361{
362 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
363 /*
364 * HelenOS doesn't support permissions, permission checks
365 * are equal to existence check.
366 *
367 * Check file existence by attempting to open it.
368 */
369 int fd = open(path, O_RDONLY);
370 if (fd < 0)
371 return -1;
372 close(fd);
373 return 0;
374 } else {
375 /* Invalid amode argument. */
376 errno = EINVAL;
377 return -1;
378 }
379}
380
381/**
382 * Get configurable system variables.
383 *
384 * @param name Variable name.
385 * @return Variable value.
386 */
387long sysconf(int name)
388{
389 long clk_tck = 0;
390 size_t cpu_count = 0;
391 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
392 if (cpu_stats && cpu_count > 0) {
393 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
394 }
395 if (cpu_stats) {
396 free(cpu_stats);
397 cpu_stats = 0;
398 }
399
400 long phys_pages = 0;
401 long avphys_pages = 0;
402 stats_physmem_t *mem_stats = stats_get_physmem();
403 if (mem_stats) {
404 phys_pages = (long) (mem_stats->total / getpagesize());
405 avphys_pages = (long) (mem_stats->free / getpagesize());
406 free(mem_stats);
407 mem_stats = 0;
408 }
409
410 switch (name) {
411 case _SC_PHYS_PAGES:
412 return phys_pages;
413 case _SC_AVPHYS_PAGES:
414 return avphys_pages;
415 case _SC_PAGESIZE:
416 return getpagesize();
417 case _SC_CLK_TCK:
418 return clk_tck;
419 default:
420 errno = EINVAL;
421 return -1;
422 }
423}
424
425/**
426 *
427 * @param path
428 * @param name
429 * @return
430 */
431long pathconf(const char *path, int name)
432{
433 // TODO: low priority, just a compile-time dependency of binutils
434 not_implemented();
435 return -1;
436}
437
438/**
439 *
440 * @return
441 */
442pid_t fork(void)
443{
444 // TODO: low priority, just a compile-time dependency of binutils
445 not_implemented();
446 return -1;
447}
448
449/**
450 *
451 * @param path
452 * @param argv
453 * @return
454 */
455int execv(const char *path, char *const argv[])
456{
457 // TODO: low priority, just a compile-time dependency of binutils
458 not_implemented();
459 return -1;
460}
461
462/**
463 *
464 * @param file
465 * @param argv
466 * @return
467 */
468int execvp(const char *file, char *const argv[])
469{
470 // TODO: low priority, just a compile-time dependency of binutils
471 not_implemented();
472 return -1;
473}
474
475/**
476 *
477 * @param fildes
478 * @return
479 */
480int pipe(int fildes[2])
481{
482 // TODO: low priority, just a compile-time dependency of binutils
483 not_implemented();
484 return -1;
485}
486
487unsigned int alarm(unsigned int seconds)
488{
489 not_implemented();
490 return 0;
491}
492
493/** @}
494 */
Note: See TracBrowser for help on using the repository browser.