Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset fb872c1 in mainline


Ignore:
Timestamp:
2011-07-01T23:36:00Z (11 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master
Children:
1978a5f
Parents:
06cb827
Message:

Implemented and commented some functions from unistd.c.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/unistd.c

    r06cb827 rfb872c1  
    3939#include "unistd.h"
    4040#include <task.h>
     41#include <errno.h>
     42#include "string.h"
     43#include "fcntl.h"
     44#include <stats.h>
     45#include "libc/malloc.h"
    4146
    4247/* Array of environment variable strings (NAME=VALUE). */
     
    4550/**
    4651 * Get current user name.
     52 *
     53 * @return User name (static) string or NULL if not found.
    4754 */
    4855char *posix_getlogin(void)
    4956{
    50         // TODO
     57        /* There is currently no support for user accounts in HelenOS. */
    5158        return (char *) "user";
    5259}
     
    5764 * @param name Pointer to a user supplied buffer.
    5865 * @param namesize Length of the buffer.
    59  * @return
     66 * @return Zero on success, error code otherwise.
    6067 */
    6168int posix_getlogin_r(char *name, size_t namesize)
    6269{
    63         // TODO
    64         not_implemented();
    65 }
    66 
    67 /**
    68  * Dummy function. Always returns false, because there is no easy way to find
    69  * out under HelenOS.
    70  *
    71  * @param fd
    72  * @return Always false.
     70        /* There is currently no support for user accounts in HelenOS. */
     71        if (namesize >= 5) {
     72                posix_strcpy(name, (char *) "user");
     73                return 0;
     74        } else {
     75                errno = ERANGE;
     76                return -1;
     77        }
     78}
     79
     80/**
     81 * Test whether open file descriptor is associated with a terminal.
     82 *
     83 * @param fd Open file descriptor to test.
     84 * @return Boolean result of the test.
    7385 */
    7486int posix_isatty(int fd)
    7587{
     88        /* Always returns false, because there is no easy way to find
     89     * out under HelenOS. */
    7690        return false;
    7791}
    7892
    7993/**
    80  *
    81  * @return
     94 * Determine the page size of the current run of the process.
     95 *
     96 * @return Page size of the process.
    8297 */
    8398int posix_getpagesize(void)
     
    87102
    88103/**
    89  *
    90  * @return
     104 * Get the process ID of the calling process.
     105 *
     106 * @return Process ID.
    91107 */
    92108posix_pid_t posix_getpid(void)
     
    96112
    97113/**
    98  *
    99  * @return
     114 * Get the real user ID of the calling process.
     115 *
     116 * @return User ID.
    100117 */
    101118posix_uid_t posix_getuid(void)
    102119{
    103         // TODO
    104         not_implemented();
    105 }
    106 
    107 /**
    108  *
    109  * @return
     120        /* There is currently no support for user accounts in HelenOS. */
     121        return 0;
     122}
     123
     124/**
     125 * Get the real group ID of the calling process.
     126 *
     127 * @return Group ID.
    110128 */
    111129posix_gid_t posix_getgid(void)
    112130{
    113         // TODO
    114         not_implemented();
    115 }
    116 
    117 /**
    118  *
    119  * @param path
    120  * @param amode
    121  * @return
     131        /* There is currently no support for user accounts in HelenOS. */
     132        return 0;
     133}
     134
     135/**
     136 * Determine accessibility of a file.
     137 *
     138 * @param path File to check accessibility for.
     139 * @param amode Either check for existence or intended access mode.
     140 * @return Zero on success, -1 otherwise.
    122141 */
    123142int posix_access(const char *path, int amode)
    124143{
    125         // TODO
    126         not_implemented();
    127 }
    128 
    129 /**
    130  *
    131  * @param name
    132  * @return
     144        if (amode == F_OK) {
     145                /* Check file existence by attempt to open it. */
     146                int fd = open(path, O_RDONLY);
     147                // TODO: propagate a POSIX compatible errno
     148                int rc = fd < 0 ? -1 : 0;
     149                close(fd);
     150                return rc;
     151        } else if (amode & (X_OK | W_OK | R_OK)) {
     152                /* HelenOS doesn't support permissions, return success. */
     153                return 0;
     154        } else {
     155                /* Invalid amode argument. */
     156                errno = EINVAL;
     157                return -1;
     158        }
     159}
     160
     161/**
     162 * Get configurable system variables.
     163 *
     164 * @param name Variable name.
     165 * @return Variable value.
    133166 */
    134167long posix_sysconf(int name)
    135168{
    136         // TODO
    137         not_implemented();
     169        long clk_tck = 0;
     170        size_t cpu_count = 0;
     171        stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
     172        if (cpu_stats && cpu_count > 0) {
     173                clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
     174        }
     175        free(cpu_stats);
     176        cpu_stats = 0;
     177
     178        long phys_pages = 0;
     179        long avphys_pages = 0;
     180        stats_physmem_t *mem_stats = stats_get_physmem();
     181        if (mem_stats) {
     182                phys_pages = (long) (mem_stats->total / getpagesize());
     183                avphys_pages = (long) (mem_stats->free / getpagesize());
     184        }
     185        free(mem_stats);
     186        mem_stats = 0;
     187
     188        switch (name) {
     189        case _SC_PHYS_PAGES:
     190                return phys_pages;
     191        case _SC_AVPHYS_PAGES:
     192                return avphys_pages;
     193        case _SC_PAGESIZE:
     194                return getpagesize();
     195        case _SC_CLK_TCK:
     196                return clk_tck;
     197        default:
     198                errno = EINVAL;
     199                return -1;
     200        }
    138201}
    139202
Note: See TracChangeset for help on using the changeset viewer.