Changeset 8cd8bf6 in mainline for uspace/lib/posix/unistd.c


Ignore:
Timestamp:
2011-07-08T17:25:53Z (13 years ago)
Author:
Petr Koupy <petr.koupy@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
11809eab
Parents:
f5b2522 (diff), ddc63fd (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge libposix changes.

File:
1 edited

Legend:

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

    rf5b2522 r8cd8bf6  
    3838#include "internal/common.h"
    3939#include "unistd.h"
    40 #include <task.h>
     40
     41#include "errno.h"
     42#include "string.h"
     43#include "fcntl.h"
     44
     45#include "libc/task.h"
     46#include "libc/stats.h"
     47#include "libc/malloc.h"
    4148
    4249/* Array of environment variable strings (NAME=VALUE). */
    43 char **environ = NULL;
    44 
    45 /**
    46  * Dummy function. Always returns false, because there is no easy way to find
    47  * out under HelenOS.
    48  *
    49  * @param fd
    50  * @return Always false.
     50char **posix_environ = NULL;
     51
     52/**
     53 * Get current user name.
     54 *
     55 * @return User name (static) string or NULL if not found.
     56 */
     57char *posix_getlogin(void)
     58{
     59        /* There is currently no support for user accounts in HelenOS. */
     60        return (char *) "user";
     61}
     62
     63/**
     64 * Get current user name.
     65 *
     66 * @param name Pointer to a user supplied buffer.
     67 * @param namesize Length of the buffer.
     68 * @return Zero on success, error code otherwise.
     69 */
     70int posix_getlogin_r(char *name, size_t namesize)
     71{
     72        /* There is currently no support for user accounts in HelenOS. */
     73        if (namesize >= 5) {
     74                posix_strcpy(name, (char *) "user");
     75                return 0;
     76        } else {
     77                errno = ERANGE;
     78                return -1;
     79        }
     80}
     81
     82/**
     83 * Test whether open file descriptor is associated with a terminal.
     84 *
     85 * @param fd Open file descriptor to test.
     86 * @return Boolean result of the test.
    5187 */
    5288int posix_isatty(int fd)
    5389{
     90        /* Always returns false, because there is no easy way to find
     91     * out under HelenOS. */
    5492        return false;
    5593}
    5694
    5795/**
    58  *
    59  * @return
     96 * Determine the page size of the current run of the process.
     97 *
     98 * @return Page size of the process.
    6099 */
    61100int posix_getpagesize(void)
     
    65104
    66105/**
    67  *
    68  * @return
     106 * Get the process ID of the calling process.
     107 *
     108 * @return Process ID.
    69109 */
    70110posix_pid_t posix_getpid(void)
     
    74114
    75115/**
    76  *
    77  * @return
     116 * Get the real user ID of the calling process.
     117 *
     118 * @return User ID.
    78119 */
    79120posix_uid_t posix_getuid(void)
    80121{
    81         // TODO
    82         not_implemented();
    83 }
    84 
    85 /**
    86  *
    87  * @return
     122        /* There is currently no support for user accounts in HelenOS. */
     123        return 0;
     124}
     125
     126/**
     127 * Get the real group ID of the calling process.
     128 *
     129 * @return Group ID.
    88130 */
    89131posix_gid_t posix_getgid(void)
    90132{
    91         // TODO
    92         not_implemented();
    93 }
    94 
    95 /**
    96  *
    97  * @param path
    98  * @param amode
    99  * @return
     133        /* There is currently no support for user accounts in HelenOS. */
     134        return 0;
     135}
     136
     137/**
     138 * Determine accessibility of a file.
     139 *
     140 * @param path File to check accessibility for.
     141 * @param amode Either check for existence or intended access mode.
     142 * @return Zero on success, -1 otherwise.
    100143 */
    101144int posix_access(const char *path, int amode)
    102145{
    103         // TODO
    104         not_implemented();
    105 }
    106 
    107 /**
    108  *
    109  * @param name
    110  * @return
     146        if (amode == F_OK) {
     147                /* Check file existence by attempt to open it. */
     148                int fd = open(path, O_RDONLY);
     149                if (fd < 0) {
     150                        /* FIXME: open() returns error code as negative retval. */
     151                        errno = -fd;
     152                        fd = -1;
     153                } else {
     154                        close(fd);
     155                }
     156                return fd;
     157        } else if (amode & (X_OK | W_OK | R_OK)) {
     158                /* HelenOS doesn't support permissions, return success. */
     159                return 0;
     160        } else {
     161                /* Invalid amode argument. */
     162                errno = EINVAL;
     163                return -1;
     164        }
     165}
     166
     167/**
     168 * Get configurable system variables.
     169 *
     170 * @param name Variable name.
     171 * @return Variable value.
    111172 */
    112173long posix_sysconf(int name)
    113174{
    114         // TODO
    115         not_implemented();
     175        long clk_tck = 0;
     176        size_t cpu_count = 0;
     177        stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
     178        if (cpu_stats && cpu_count > 0) {
     179                clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
     180        }
     181        free(cpu_stats);
     182        cpu_stats = 0;
     183
     184        long phys_pages = 0;
     185        long avphys_pages = 0;
     186        stats_physmem_t *mem_stats = stats_get_physmem();
     187        if (mem_stats) {
     188                phys_pages = (long) (mem_stats->total / getpagesize());
     189                avphys_pages = (long) (mem_stats->free / getpagesize());
     190        }
     191        free(mem_stats);
     192        mem_stats = 0;
     193
     194        switch (name) {
     195        case _SC_PHYS_PAGES:
     196                return phys_pages;
     197        case _SC_AVPHYS_PAGES:
     198                return avphys_pages;
     199        case _SC_PAGESIZE:
     200                return getpagesize();
     201        case _SC_CLK_TCK:
     202                return clk_tck;
     203        default:
     204                errno = EINVAL;
     205                return -1;
     206        }
    116207}
    117208
Note: See TracChangeset for help on using the changeset viewer.