Subversion Repositories Kolibri OS

Rev

Rev 5215 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4921 Serge 1
/* fstat.c -- get status of a file.
2
 *
3
 * Copyright (c) 1995 Cygnus Support
4
 *
5
 * The authors hereby grant permission to use, copy, modify, distribute,
6
 * and license this software and its documentation for any purpose, provided
7
 * that existing copyright notices are retained in all copies and that this
8
 * notice is included verbatim in any distributions. No written agreement,
9
 * license, or royalty fee is required for any of the authorized uses.
10
 * Modifications to this software may be copyrighted by their authors
11
 * and need not follow the licensing terms described here, provided that
12
 * the new terms are clearly indicated on the first page of each file where
13
 * they apply.
14
 */
15
#include 
16
#include 
17
#include 
18
#include 
19
#include 
20
#include "glue.h"
21
#include "io.h"
22
 
23
int
24
_DEFUN (fstat, (fd, buf),
25
       int fd _AND
26
       struct stat *buf)
27
{
28
    fileinfo_t info;
5215 serge 29
    struct tm time;
30
 
4921 Serge 31
    __io_handle *ioh;
32
 
33
    if( (fd < 0) || (fd >=64) )
34
    {
35
        errno = EBADF;
36
        return (-1);
37
    };
38
 
39
    memset (buf, 0, sizeof (* buf));
40
 
41
    if (fd <= STDERR_FILENO)
42
    {
43
        buf->st_mode = S_IFCHR;
44
        buf->st_blksize = 0;
45
    }
46
    else
47
    {
48
 
49
        ioh = &__io_tab[fd];
50
        get_fileinfo(ioh->name, &info);
51
 
5215 serge 52
        if (info.attr & 0x10)
53
            buf->st_mode = S_IFDIR;
54
        else
55
        {
56
            if (info.attr & 0x07)
57
                buf->st_mode = S_IFREG|S_IRUSR|S_IXUSR;
58
            else
59
                buf->st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IXUSR;
60
        }
4921 Serge 61
        buf->st_blksize = 4096;
5369 serge 62
        buf->st_size  = info.size;
5215 serge 63
 
64
        time.tm_sec   = info.atime.sec;
65
        time.tm_min   = info.atime.min;
66
        time.tm_hour  = info.atime.hour;
67
        time.tm_mday  = info.adate.day;
68
        time.tm_mon   = info.adate.month;
69
        time.tm_year  = info.adate.year - 1900;
70
        time.tm_isdst = -1;
71
        buf->st_atime = mktime(&time);
72
 
73
        time.tm_sec   = info.ctime.sec;
74
        time.tm_min   = info.ctime.min;
75
        time.tm_hour  = info.ctime.hour;
76
        time.tm_mday  = info.cdate.day;
77
        time.tm_mon   = info.cdate.month;
78
        time.tm_year  = info.cdate.year - 1900;
79
        time.tm_isdst = -1;
80
        buf->st_ctime = mktime(&time);
81
 
82
        time.tm_sec   = info.mtime.sec;
83
        time.tm_min   = info.mtime.min;
84
        time.tm_hour  = info.mtime.hour;
85
        time.tm_mday  = info.mdate.day;
86
        time.tm_mon   = info.mdate.month;
87
        time.tm_year  = info.mdate.year - 1900;
88
        time.tm_isdst = -1;
89
        buf->st_mtime = mktime(&time);
90
 
4921 Serge 91
    };
92
 
93
    return (0);
94
}
95