Changeset 97b1dfc2 in mainline


Ignore:
Timestamp:
2018-09-24T15:36:21Z (6 years ago)
Author:
Jiří Zárevúcky <jiri.zarevucky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
34c76e2f
Parents:
631281d
git-author:
Jiří Zárevúcky <jiri.zarevucky@…> (2018-09-24 15:02:32)
git-committer:
Jiří Zárevúcky <jiri.zarevucky@…> (2018-09-24 15:36:21)
Message:

Make libposix headers understand _FILE_OFFSET_BITS and _LARGEFILE64_SOURCE

Location:
uspace/lib/posix
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/include/posix/sys/types.h

    r631281d r97b1dfc2  
    4343typedef unsigned int uid_t;
    4444typedef unsigned int gid_t;
    45 typedef int64_t off_t;
    4645typedef long blksize_t;
    4746typedef long blkcnt_t;
     
    4948typedef sysarg_t dev_t;
    5049typedef unsigned int mode_t;
     50
     51#if _FILE_OFFSET_BITS == 64
     52typedef int64_t off_t;
     53#else
     54typedef long off_t;
     55#endif
     56
     57#ifdef _LARGEFILE64_SOURCE
     58typedef int64_t off64_t;
     59#endif
    5160
    5261/* PThread Types */
  • uspace/lib/posix/include/posix/unistd.h

    r631281d r97b1dfc2  
    8080extern ssize_t read(int fildes, void *buf, size_t nbyte);
    8181extern ssize_t write(int fildes, const void *buf, size_t nbyte);
    82 extern off_t lseek(int fildes,
    83     off_t offset, int whence);
    8482extern int fsync(int fildes);
    8583extern int ftruncate(int fildes, off_t length);
     
    8886extern int dup(int fildes);
    8987extern int dup2(int fildes, int fildes2);
     88
     89#ifdef _LARGEFILE64_SOURCE
     90extern off64_t lseek64(int fildes, off64_t offset, int whence);
     91#endif
     92
     93#if _FILE_OFFSET_BITS == 64
     94static inline off_t lseek(int fildes, off_t offset, int whence)
     95{
     96        /* Declarations visible in this function body only. */
     97        typedef int64_t off64_t;
     98        extern off64_t lseek64(int fildes, off64_t offset, int whence);
     99
     100        /* With _FILE_OFFSET_BITS == 64, lseek is actually lseek64. */
     101        return lseek64(fildes, offset, whence);
     102}
     103#else
     104extern off_t lseek(int fildes, off_t offset, int whence);
     105#endif
    90106
    91107/* Standard Streams */
  • uspace/lib/posix/src/unistd.c

    r631281d r97b1dfc2  
    3434 */
    3535
     36#define _LARGEFILE64_SOURCE
     37#undef _FILE_OFFSET_BITS
     38
    3639#include "internal/common.h"
    3740#include <unistd.h>
     
    234237}
    235238
     239static off64_t _lseek(int fildes, off64_t offset, off64_t max_pos, int whence)
     240{
     241        vfs_stat_t st;
     242        off64_t new_pos;
     243
     244        switch (whence) {
     245        case SEEK_SET:
     246                new_pos = offset;
     247                break;
     248        case SEEK_CUR:
     249                if (__builtin_add_overflow(posix_pos[fildes], offset, &new_pos)) {
     250                        errno = EOVERFLOW;
     251                        return -1;
     252                }
     253                break;
     254        case SEEK_END:
     255                if (failed(vfs_stat(fildes, &st)))
     256                        return -1;
     257
     258                if (__builtin_add_overflow(st.size, offset, &new_pos)) {
     259                        errno = EOVERFLOW;
     260                        return -1;
     261                }
     262                break;
     263        default:
     264                errno = EINVAL;
     265                return -1;
     266        }
     267
     268        if (new_pos < 0) {
     269                errno = EINVAL;
     270                return -1;
     271        }
     272
     273        if (new_pos > max_pos) {
     274                /* The resulting file offset is too large for the interface. */
     275                errno = EOVERFLOW;
     276                return -1;
     277        }
     278
     279        posix_pos[fildes] = new_pos;
     280        return new_pos;
     281}
     282
     283static off64_t _lseek64(int fildes, off64_t offset, int whence)
     284{
     285        return _lseek(fildes, offset, INT64_MAX, whence);
     286}
     287
     288off64_t lseek64(int fildes, off64_t offset, int whence)
     289{
     290        return _lseek64(fildes, offset, whence);
     291}
     292
    236293/**
    237294 * Reposition read/write file offset
     
    245302off_t lseek(int fildes, off_t offset, int whence)
    246303{
    247         vfs_stat_t st;
    248 
    249         switch (whence) {
    250         case SEEK_SET:
    251                 posix_pos[fildes] = offset;
    252                 break;
    253         case SEEK_CUR:
    254                 posix_pos[fildes] += offset;
    255                 break;
    256         case SEEK_END:
    257                 if (failed(vfs_stat(fildes, &st)))
    258                         return -1;
    259                 posix_pos[fildes] = st.size + offset;
    260                 break;
    261         }
    262         if (posix_pos[fildes] > INT64_MAX) {
    263                 /* The native width is too large for the POSIX interface. */
    264                 errno = ERANGE;
    265                 return -1;
    266         }
    267         return posix_pos[fildes];
     304#if LONG_MAX == INT64_MAX
     305        return _lseek64(fildes, offset, whence);
     306#else
     307        return _lseek(fildes, offset, LONG_MAX, whence);
     308#endif
    268309}
    269310
Note: See TracChangeset for help on using the changeset viewer.