Changeset 777832e in mainline


Ignore:
Timestamp:
2018-06-21T12:27:09Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
296890f3
Parents:
7d7bc09
git-author:
Jiri Svoboda <jiri@…> (2018-06-20 19:26:46)
git-committer:
Jiri Svoboda <jiri@…> (2018-06-21 12:27:09)
Message:

fgetpos, fsetpos, perror.

Location:
uspace
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/Makefile

    r7d7bc09 r777832e  
    194194        test/qsort.c \
    195195        test/sprintf.c \
     196        test/stdio.c \
    196197        test/stdlib.c \
    197198        test/str.c
  • uspace/lib/c/generic/stdio.c

    r7d7bc09 r777832e  
    11/*
    2  * Copyright (c) 2017 Jiri Svoboda
     2 * Copyright (c) 2018 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3535#include <errno.h>
    3636#include <stdio.h>
     37#include <str_error.h>
    3738#include <vfs/vfs.h>
     39
     40/** Get stream position.
     41 *
     42 * @param stream Stream
     43 * @param pos Place to store position
     44 *
     45 * @return Zero on success, non-zero on failure
     46 */
     47int fgetpos(FILE *stream, fpos_t *pos)
     48{
     49        off64_t p;
     50
     51        p = ftell64(stream);
     52        if (p < 0)
     53                return -1;
     54
     55        pos->pos = p;
     56        return 0;
     57}
     58
     59/** Get stream position.
     60 *
     61 * @param stream Stream
     62 * @param pos Position
     63 *
     64 * @return Zero on sucess, non-zero on failure
     65 */
     66int fsetpos(FILE *stream, const fpos_t *pos)
     67{
     68        int rc;
     69
     70        rc = fseek64(stream, pos->pos, SEEK_SET);
     71        if (rc < 0)
     72                return -1;
     73
     74        return 0;
     75}
    3876
    3977/** Rename file or directory (C standard) */
     
    65103}
    66104
     105/** Print error message and string representation of @c errno.
     106 *
     107 * @param s Error message
     108 */
     109void perror(const char *s)
     110{
     111        if (s != NULL && *s != '\0')
     112                fprintf(stderr, "%s: %s\n", s, str_error(errno));
     113        else
     114                fprintf(stderr, "%s\n", str_error(errno));
     115}
     116
    67117/** @}
    68118 */
  • uspace/lib/c/include/stdio.h

    r7d7bc09 r777832e  
    11/*
    22 * Copyright (c) 2005 Martin Decky
     3 * Copyright (c) 2018 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    3637#define LIBC_STDIO_H_
    3738
     39#include <offset.h>
    3840#include <stdarg.h>
    3941#include <io/verify.h>
     
    5961#define BUFSIZ  4096
    6062
     63/** Max number of files that is guaranteed to be able to open at the same time */
     64#define FOPEN_MAX VFS_MAX_OPEN_FILES
     65
    6166/** Recommended size of fixed-size array for holding file names. */
    6267#define FILENAME_MAX 4096
     
    6570struct _IO_FILE;
    6671typedef struct _IO_FILE FILE;
     72
     73/** File position */
     74typedef struct {
     75        off64_t pos;
     76} fpos_t;
    6777
    6878extern FILE *stdin;
     
    124134extern size_t fwrite(const void *, size_t, size_t, FILE *);
    125135
     136extern int fgetpos(FILE *, fpos_t *);
     137extern int fsetpos(FILE *, const fpos_t *);
     138
    126139extern int fseek(FILE *, long, int);
    127140extern void rewind(FILE *);
     
    132145extern int ferror(FILE *);
    133146extern void clearerr(FILE *);
     147
     148extern void perror(const char *);
    134149
    135150extern void setvbuf(FILE *, void *, int, size_t);
  • uspace/lib/c/include/vfs/vfs.h

    r7d7bc09 r777832e  
    4545#include <offset.h>
    4646
    47 #define MAX_OPEN_FILES  128
     47#define VFS_MAX_OPEN_FILES      128
    4848
    4949enum vfs_change_state_type {
  • uspace/lib/posix/include/posix/stdio.h

    r7d7bc09 r777832e  
    5959    FILE *__restrict__ stream);
    6060
    61 /* Error Messages */
    62 extern void perror(const char *s);
    63 
    64 /* File Positioning */
    65 typedef struct {
    66         off64_t offset;
    67 } fpos_t;
    68 
    69 extern int fsetpos(FILE *stream, const fpos_t *pos);
    70 extern int fgetpos(FILE *__restrict__ stream, fpos_t *__restrict__ pos);
    7161extern int fseeko(FILE *stream, off_t offset, int whence);
    7262extern off_t ftello(FILE *stream);
  • uspace/lib/posix/src/internal/common.h

    r7d7bc09 r777832e  
    6666}
    6767
    68 extern aoff64_t posix_pos[MAX_OPEN_FILES];
     68extern aoff64_t posix_pos[VFS_MAX_OPEN_FILES];
    6969
    7070#endif /* LIBPOSIX_COMMON_H_ */
  • uspace/lib/posix/src/stdio.c

    r7d7bc09 r777832e  
    169169
    170170/**
    171  * Write error messages to standard error.
    172  *
    173  * @param s Error message.
    174  */
    175 void perror(const char *s)
    176 {
    177         if (s == NULL || s[0] == '\0') {
    178                 fprintf(stderr, "%s\n", strerror(errno));
    179         } else {
    180                 fprintf(stderr, "%s: %s\n", s, strerror(errno));
    181         }
    182 }
    183 
    184 /** Restores stream a to position previously saved with fgetpos().
    185  *
    186  * @param stream Stream to restore
    187  * @param pos Position to restore
    188  * @return Zero on success, non-zero (with errno set) on failure
    189  */
    190 int fsetpos(FILE *stream, const fpos_t *pos)
    191 {
    192         return fseek64(stream, pos->offset, SEEK_SET);
    193 }
    194 
    195 /** Saves the stream's position for later use by fsetpos().
    196  *
    197  * @param stream Stream to save
    198  * @param pos Place to store the position
    199  * @return Zero on success, non-zero (with errno set) on failure
    200  */
    201 int fgetpos(FILE *restrict stream, fpos_t *restrict pos)
    202 {
    203         off64_t ret = ftell64(stream);
    204         if (ret != -1) {
    205                 pos->offset = ret;
    206                 return 0;
    207         } else {
    208                 return -1;
    209         }
    210 }
    211 
    212 /**
    213171 * Reposition a file-position indicator in a stream.
    214172 *
  • uspace/lib/posix/src/unistd.c

    r7d7bc09 r777832e  
    5151
    5252// FIXME: replace with a hash table
    53 aoff64_t posix_pos[MAX_OPEN_FILES];
     53aoff64_t posix_pos[VFS_MAX_OPEN_FILES];
    5454
    5555/* Array of environment variable strings (NAME=VALUE). */
  • uspace/srv/vfs/vfs_file.c

    r7d7bc09 r777832e  
    7171        fibril_mutex_lock(&vfs_data->lock);
    7272        if (!vfs_data->files) {
    73                 vfs_data->files = malloc(MAX_OPEN_FILES * sizeof(vfs_file_t *));
     73                vfs_data->files = malloc(VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
    7474                if (!vfs_data->files) {
    7575                        fibril_mutex_unlock(&vfs_data->lock);
    7676                        return false;
    7777                }
    78                 memset(vfs_data->files, 0, MAX_OPEN_FILES * sizeof(vfs_file_t *));
     78                memset(vfs_data->files, 0, VFS_MAX_OPEN_FILES * sizeof(vfs_file_t *));
    7979        }
    8080        fibril_mutex_unlock(&vfs_data->lock);
     
    9090                return;
    9191
    92         for (i = 0; i < MAX_OPEN_FILES; i++) {
     92        for (i = 0; i < VFS_MAX_OPEN_FILES; i++) {
    9393                if (vfs_data->files[i])
    9494                        (void) _vfs_fd_free(vfs_data, i);
     
    199199        unsigned int i;
    200200        if (desc)
    201                 i = MAX_OPEN_FILES - 1;
     201                i = VFS_MAX_OPEN_FILES - 1;
    202202        else
    203203                i = 0;
     
    233233                        i--;
    234234                } else {
    235                         if (i == MAX_OPEN_FILES - 1)
     235                        if (i == VFS_MAX_OPEN_FILES - 1)
    236236                                break;
    237237
     
    261261static errno_t _vfs_fd_free_locked(vfs_client_data_t *vfs_data, int fd)
    262262{
    263         if ((fd < 0) || (fd >= MAX_OPEN_FILES) || !vfs_data->files[fd]) {
     263        if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES) || !vfs_data->files[fd]) {
    264264                return EBADF;
    265265        }
     
    311311
    312312        fibril_mutex_lock(&VFS_DATA->lock);
    313         if ((fd < 0) || (fd >= MAX_OPEN_FILES)) {
     313        if ((fd < 0) || (fd >= VFS_MAX_OPEN_FILES)) {
    314314                fibril_mutex_unlock(&VFS_DATA->lock);
    315315                return EBADF;
     
    342342
    343343        fibril_mutex_lock(&vfs_data->lock);
    344         if ((fd >= 0) && (fd < MAX_OPEN_FILES)) {
     344        if ((fd >= 0) && (fd < VFS_MAX_OPEN_FILES)) {
    345345                vfs_file_t *file = vfs_data->files[fd];
    346346                if (file != NULL) {
Note: See TracChangeset for help on using the changeset viewer.