Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/stdio.c

    r5a6cc679 rbfe90b6  
    11/*
    2  * Copyright (c) 2017 Jiri Svoboda
     2 * Copyright (c) 2018 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3434
    3535#include <errno.h>
     36#include <stdbool.h>
    3637#include <stdio.h>
     38#include <str.h>
     39#include <str_error.h>
     40#include <tmpfile.h>
    3741#include <vfs/vfs.h>
     42
     43/** Static buffer for tmpnam function */
     44static char tmpnam_buf[L_tmpnam];
     45
     46/** Get stream position.
     47 *
     48 * @param stream Stream
     49 * @param pos Place to store position
     50 *
     51 * @return Zero on success, non-zero on failure
     52 */
     53int fgetpos(FILE *stream, fpos_t *pos)
     54{
     55        off64_t p;
     56
     57        p = ftell64(stream);
     58        if (p < 0)
     59                return -1;
     60
     61        pos->pos = p;
     62        return 0;
     63}
     64
     65/** Get stream position.
     66 *
     67 * @param stream Stream
     68 * @param pos Position
     69 *
     70 * @return Zero on sucess, non-zero on failure
     71 */
     72int fsetpos(FILE *stream, const fpos_t *pos)
     73{
     74        int rc;
     75
     76        rc = fseek64(stream, pos->pos, SEEK_SET);
     77        if (rc < 0)
     78                return -1;
     79
     80        return 0;
     81}
    3882
    3983/** Rename file or directory (C standard) */
     
    4488        rc = vfs_rename_path(old_path, new_path);
    4589        if (rc != EOK) {
     90                /*
     91                 * Note that ISO C leaves the value of errno undefined,
     92                 * whereas according to UN*X standards, it is set.
     93                 */
    4694                errno = rc;
    4795                return -1;
     
    58106        rc = vfs_unlink_path(path);
    59107        if (rc != EOK) {
     108                /*
     109                 * Note that ISO C leaves the value of errno undefined,
     110                 * whereas according to UN*X standards, it is set.
     111                 */
    60112                errno = rc;
    61113                return -1;
     
    65117}
    66118
     119/** Create a temporary file.
     120 *
     121 * @return Open stream descriptor or @c NULL on error. In that case
     122 *         errno is set (UN*X). Note that ISO C leaves the value of errno
     123 *         undefined.
     124 */
     125FILE *tmpfile(void)
     126{
     127        int file;
     128        FILE *stream;
     129
     130        file = __tmpfile();
     131        if (file < 0) {
     132                printf("file is < 0\n");
     133                errno = EEXIST;
     134                return NULL;
     135        }
     136
     137        stream = fdopen(file, "w+");
     138        if (stream == NULL) {
     139                printf("stream = NULL\n");
     140                vfs_put(file);
     141                errno = EACCES;
     142                return NULL;
     143        }
     144
     145        return stream;
     146}
     147
     148/** Create name for a temporary file.
     149 *
     150 * @param s Pointer to array of at least L_tmpnam bytes or @c NULL.
     151 * @return The pointer @a s or pointer to internal static buffer on success,
     152 *         @c NULL on error.
     153 */
     154char *tmpnam(char *s)
     155{
     156        char *p;
     157
     158        p = (s != NULL) ? s : tmpnam_buf;
     159        return __tmpnam(p);
     160}
     161
     162/** Print error message and string representation of @c errno.
     163 *
     164 * @param s Error message
     165 */
     166void perror(const char *s)
     167{
     168        if (s != NULL && *s != '\0')
     169                fprintf(stderr, "%s: %s\n", s, str_error(errno));
     170        else
     171                fprintf(stderr, "%s\n", str_error(errno));
     172}
     173
     174
    67175/** @}
    68176 */
Note: See TracChangeset for help on using the changeset viewer.