Changeset 4e6a610 in mainline for uspace/lib/c


Ignore:
Timestamp:
2018-06-25T09:54:28Z (7 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
bfe90b6
Parents:
fb0ec570
git-author:
Jiri Svoboda <jiri@…> (2018-06-24 17:51:54)
git-committer:
Jiri Svoboda <jiri@…> (2018-06-25 09:54:28)
Message:

Temporary file functions rework. Fix libposix access() not working on directories.

Location:
uspace/lib/c
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/Makefile

    rfb0ec570 r4e6a610  
    4141EXTRA_OUTPUT = $(LINKER_SCRIPTS)
    4242EXTRA_CLEAN = $(LINKER_SCRIPTS)
     43EXTRA_TEST_CFLAGS = -Wno-deprecated-declarations
    4344LIBRARY = libc
    4445SOVERSION = 0.0
     
    151152        generic/adt/prodcons.c \
    152153        generic/time.c \
     154        generic/tmpfile.c \
    153155        generic/stdio.c \
    154156        generic/stdlib.c \
  • uspace/lib/c/generic/stdio.c

    rfb0ec570 r4e6a610  
    3434
    3535#include <errno.h>
     36#include <stdbool.h>
    3637#include <stdio.h>
     38#include <str.h>
    3739#include <str_error.h>
     40#include <tmpfile.h>
    3841#include <vfs/vfs.h>
     42
     43/** Static buffer for tmpnam function */
     44static char tmpnam_buf[L_tmpnam];
    3945
    4046/** Get stream position.
     
    8288        rc = vfs_rename_path(old_path, new_path);
    8389        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                 */
    8494                errno = rc;
    8595                return -1;
     
    96106        rc = vfs_unlink_path(path);
    97107        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                 */
    98112                errno = rc;
    99113                return -1;
     
    101115
    102116        return 0;
     117}
     118
     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        printf("returning stream\n");
     146        return stream;
     147}
     148
     149/** Create name for a temporary file.
     150 *
     151 * @param s Pointer to array of at least L_tmpnam bytes or @c NULL.
     152 * @return The pointer @a s or pointer to internal static buffer on success,
     153 *         @c NULL on error.
     154 */
     155char *tmpnam(char *s)
     156{
     157        char *p;
     158
     159        p = (s != NULL) ? s : tmpnam_buf;
     160        return __tmpnam(p);
    103161}
    104162
     
    115173}
    116174
     175
    117176/** @}
    118177 */
  • uspace/lib/c/include/stdio.h

    rfb0ec570 r4e6a610  
    4040#include <stdarg.h>
    4141#include <io/verify.h>
     42#include <_bits/NULL.h>
    4243#include <_bits/size_t.h>
    4344#include <_bits/wchar_t.h>
    4445#include <_bits/wint_t.h>
    45 
    46 #define EOF  (-1)
    47 
    48 #ifndef SEEK_SET
    49 #define SEEK_SET  0
    50 #endif
    51 
    52 #ifndef SEEK_CUR
    53 #define SEEK_CUR  1
    54 #endif
    55 
    56 #ifndef SEEK_END
    57 #define SEEK_END  2
    58 #endif
    59 
    60 /** Default size for stream I/O buffers */
    61 #define BUFSIZ  4096
    62 
    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 
    66 /** Recommended size of fixed-size array for holding file names. */
    67 #define FILENAME_MAX 4096
    6846
    6947/** Forward declaration */
     
    7553        off64_t pos;
    7654} fpos_t;
     55
     56#ifndef _HELENOS_SOURCE
     57#define _IONBF 0
     58#define _IOLBF 1
     59#define _IOFBF 2
     60#endif
     61
     62/** Default size for stream I/O buffers */
     63#define BUFSIZ  4096
     64
     65#define EOF  (-1)
     66
     67/** Max number of files that is guaranteed to be able to open at the same time */
     68#define FOPEN_MAX VFS_MAX_OPEN_FILES
     69
     70/** Recommended size of fixed-size array for holding file names. */
     71#define FILENAME_MAX 4096
     72
     73/** Length of "/tmp/tmp.XXXXXX" + 1 */
     74#define L_tmpnam 16
     75
     76#ifndef SEEK_SET
     77#define SEEK_SET  0
     78#endif
     79
     80#ifndef SEEK_CUR
     81#define SEEK_CUR  1
     82#endif
     83
     84#ifndef SEEK_END
     85#define SEEK_END  2
     86#endif
     87
     88/** Minimum number of unique temporary file names */
     89#define TMP_MAX 1000000
    7790
    7891extern FILE *stdin;
     
    157170
    158171/* Misc file functions */
     172extern int remove(const char *);
    159173extern int rename(const char *, const char *);
    160 extern int remove(const char *);
    161 
    162 #ifndef _HELENOS_SOURCE
    163 #define _IONBF 0
    164 #define _IOLBF 1
    165 #define _IOFBF 2
    166 
    167 #endif
     174
     175extern FILE *tmpfile(void);
     176extern char *tmpnam(char *s) __attribute__((deprecated));
    168177
    169178#ifdef _HELENOS_SOURCE
  • uspace/lib/c/test/main.c

    rfb0ec570 r4e6a610  
    3838PCUT_IMPORT(scanf);
    3939PCUT_IMPORT(sprintf);
     40PCUT_IMPORT(stdio);
    4041PCUT_IMPORT(stdlib);
    4142PCUT_IMPORT(str);
  • uspace/lib/c/test/stdio.c

    rfb0ec570 r4e6a610  
    3838#include <pcut/pcut.h>
    3939#include <stdio.h>
     40#include <str.h>
     41#include <tmpfile.h>
     42#include <vfs/vfs.h>
    4043
    4144PCUT_INIT;
    4245
    4346PCUT_TEST_SUITE(stdio);
     47
     48/** remove function */
     49PCUT_TEST(remove)
     50{
     51        char buf[L_tmpnam];
     52        char *p;
     53        FILE *f;
     54        int rc;
     55
     56        /* Generate unique file name */
     57        p = tmpnam(buf);
     58        PCUT_ASSERT_NOT_NULL(p);
     59
     60        /* Removing it should fail */
     61        rc = remove(buf);
     62        PCUT_ASSERT_TRUE(rc != 0);
     63
     64        f = fopen(buf, "w");
     65        PCUT_ASSERT_NOT_NULL(f);
     66        fclose(f);
     67
     68        /* Remove for the first time */
     69        rc = remove(buf);
     70        PCUT_ASSERT_INT_EQUALS(0, rc);
     71
     72        /* Should fail the second time */
     73        rc = remove(buf);
     74        PCUT_ASSERT_TRUE(rc != 0);
     75}
     76
     77/** rename function */
     78PCUT_TEST(rename)
     79{
     80        char buf1[L_tmpnam];
     81        char buf2[L_tmpnam];
     82        char *p;
     83        FILE *f;
     84        int rc;
     85
     86        /* Generate first unique file name */
     87        p = tmpnam(buf1);
     88        PCUT_ASSERT_NOT_NULL(p);
     89
     90        /* Generate second unique file name */
     91        p = tmpnam(buf2);
     92        PCUT_ASSERT_NOT_NULL(p);
     93
     94        f = fopen(buf1, "w");
     95        PCUT_ASSERT_NOT_NULL(f);
     96        fclose(f);
     97
     98        /* Rename to the second name */
     99        rc = rename(buf1, buf2);
     100        PCUT_ASSERT_INT_EQUALS(0, rc);
     101
     102        /* First name should no longer exist */
     103        rc = remove(buf1);
     104        PCUT_ASSERT_TRUE(rc != 0);
     105
     106        /* Second can be removed */
     107        rc = remove(buf2);
     108        PCUT_ASSERT_INT_EQUALS(0, rc);
     109}
     110
     111/** tmpfile function */
     112PCUT_TEST(tmpfile)
     113{
     114        FILE *f;
     115        char c;
     116        size_t n;
     117
     118        f = tmpfile();
     119        PCUT_ASSERT_NOT_NULL(f);
     120
     121        c = 'x';
     122        n = fwrite(&c, sizeof(c), 1, f);
     123        PCUT_ASSERT_INT_EQUALS(1, n);
     124
     125        rewind(f);
     126        c = '\0';
     127        n = fread(&c, sizeof(c), 1, f);
     128        PCUT_ASSERT_INT_EQUALS(1, n);
     129        PCUT_ASSERT_INT_EQUALS('x', c);
     130
     131        fclose(f);
     132}
     133
     134/** tmpnam function with buffer argument */
     135PCUT_TEST(tmpnam_buf)
     136{
     137        char buf[L_tmpnam];
     138        char *p;
     139        FILE *f;
     140
     141        p = tmpnam(buf);
     142        PCUT_ASSERT_NOT_NULL(p);
     143
     144        f = fopen(p, "w+");
     145        PCUT_ASSERT_NOT_NULL(f);
     146        (void) remove(p);
     147        fclose(f);
     148}
     149
     150/** tmpnam function called twice */
     151PCUT_TEST(tmpnam_twice)
     152{
     153        char buf1[L_tmpnam];
     154        char buf2[L_tmpnam];
     155        char *p;
     156
     157        p = tmpnam(buf1);
     158        PCUT_ASSERT_NOT_NULL(p);
     159
     160        p = tmpnam(buf2);
     161        PCUT_ASSERT_NOT_NULL(p);
     162
     163        /* We must get two distinct names */
     164        PCUT_ASSERT_TRUE(str_cmp(buf1, buf2) != 0);
     165}
     166
     167/** tmpnam function with NULL argument */
     168PCUT_TEST(tmpnam_null)
     169{
     170        char *p;
     171        FILE *f;
     172
     173        p = tmpnam(NULL);
     174        PCUT_ASSERT_NOT_NULL(p);
     175
     176        f = fopen(p, "w+");
     177        PCUT_ASSERT_NOT_NULL(f);
     178        (void) remove(p);
     179        fclose(f);
     180}
    44181
    45182/** fgetpos and fsetpos functions */
     
    50187        FILE *f;
    51188
    52         // XXX Use tmpfile() when it is available
    53         f = fopen("/tmp/fgpsp.tmp", "w+");
    54         PCUT_ASSERT_NOT_NULL(f);
    55         rc = remove("/tmp/fgpsp.tmp");
    56         PCUT_ASSERT_INT_EQUALS(0, rc);
     189        f = tmpfile();
     190        PCUT_ASSERT_NOT_NULL(f);
    57191
    58192        rc = fputs("abc", f);
Note: See TracChangeset for help on using the changeset viewer.