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

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

Rename chdir() to vfs_cwd_set() and getcwd() to vfs_cwd_get()

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