File: stat.c

package info (click to toggle)
icmake 6.30-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,360 kB
  • ctags: 1,415
  • sloc: ansic: 7,727; makefile: 1,465; sh: 244; asm: 126; cpp: 39
file content (73 lines) | stat: -rw-r--r-- 2,209 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
\funcref{fun\_stat}{void fun\_stat ()}
    {}
    {}
    {}
    {fun\_exec()}
    {funsyste.c}
    {

        This function expects a filename as last pushed {\em e\_str}
        value. The file attributes are retrieved by a {\em stat ()} call and
        the return register is set to the type {\em e\_list}. This list
        returns the following information:

        \begin{itemize}

            \item The first element represents the file attributes.

            \item The second element represents the file size.

        \end{itemize}

    }
*/

#include "builtin.ih"

void fun_stat ()
{
    int fileatt = 0;;                           /* file attributes */
    struct stat statbuf;                        /* file stat buffer */

    int mode = intValue(top());                 /* get mode arg */
    char const *fname = stringStr(top() - 1);   /* get file name */

    reg = listConstructor();                    /* return result as list */

    if (stat (fname, &statbuf))                 /* do stat call */
    {                                           /* failure to stat? */
        if (P_CHECKMODE (mode))                 /* if mode indicates abort..*/
            error ("stat - unable to stat file %s", fname);

        return;                                 /* no checking: return */

    }                                           /* empty list */

    if (statbuf.st_mode & S_IREAD)              /* set file attribute int */
        fileatt |= IS_IREAD;
    if (statbuf.st_mode & S_IWRITE)
        fileatt |= IS_IWRITE;
    if (statbuf.st_mode & S_IEXEC)
        fileatt |= IS_IEXEC;
    if (statbuf.st_mode & S_IFDIR)
        fileatt |= IS_IFDIR;
    if (statbuf.st_mode & S_IFCHR)
        fileatt |= IS_IFCHR;
    if (statbuf.st_mode & S_IFREG)
        fileatt |= IS_IFREG;

    {
        char buf [80];                          /* conversion buf */
        sprintf (buf, "%u", fileatt);           /* file attr --> string */
        listAdd_cP(&reg, buf);                     /* = element #0 */

        sprintf(buf, "%ld",                     /* file size --> string */
                    (long)statbuf.st_size);     /* = element #1 */

        listAdd_cP(&reg, buf);
    }
}