示例#1
0
void __smakebuf(register FILE *fp)
{
    register size_t size, couldbetty;
    register void *p;
    struct stat st;

    if (fp->_flags & __SNBF)
    {
        fp->_bf._base = fp->_p = fp->_nbuf;
        fp->_bf._size = 1;
        return;
    }
    if (fp->_file < 0 || _fstat_r(fp->_data, fp->_file, &st) < 0)
    {
        couldbetty = 0;
        size = BUFSIZ;
        /* do not try to optimise fseek() */
        fp->_flags |= __SNPT;
    }
    else
    {
        couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
#ifdef HAVE_BLKSIZE
        size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize;
#else
        size = BUFSIZ;
#endif
        /*
         * Optimize fseek() only if it is a regular file.
         * (The test for __sseek is mainly paranoia.)
         */
        if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek)
        {
            fp->_flags |= __SOPT;
#ifdef HAVE_BLKSIZE
            fp->_blksize = st.st_blksize;
#else
            fp->_blksize = 1024;
#endif
        }
        else
            fp->_flags |= __SNPT;
    }
    if ((p = _malloc_r(fp->_data, size)) == NULL)
    {
        fp->_flags |= __SNBF;
        fp->_bf._base = fp->_p = fp->_nbuf;
        fp->_bf._size = 1;
    }
    else
    {
        fp->_data->__cleanup = _cleanup_r;
        fp->_flags |= __SMBF;
        fp->_bf._base = fp->_p = (unsigned char *)p;
        fp->_bf._size = size;
        if (couldbetty && isatty(fp->_file))
            fp->_flags |= __SLBF;
    }
}
示例#2
0
文件: esp_fs.c 项目: xzflin/smart.js
int _stat_r(struct _reent *r, const char *path, struct stat *s) {
    int ret, fd;

    /*
     * spiffs has no directories, simulating statting root directory;
     * required for mg_send_http_file.
     */
    if ((strcmp(path, "./") == 0) || (strcmp(path, "/") == 0)) {
        memset(s, 0, sizeof(*s));
        s->st_mode = S_IFDIR;
        return 0;
    }

    fd = _open_r(NULL, path, O_RDONLY, 0);
    if (fd == -1) return -1;
    ret = _fstat_r(NULL, fd, s);
    _close_r(NULL, fd);
    return ret;
}