File: funstat.c

package info (click to toggle)
icmake 6.22-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,120 kB
  • ctags: 1,045
  • sloc: ansic: 9,241; makefile: 1,138; asm: 126; sh: 124
file content (72 lines) | stat: -rw-r--r-- 2,417 bytes parent folder | download | duplicates (3)
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
/*
\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 "icm-exec.h"

void fun_stat ()
{
    register char
        *fname;                                 /* file name */
    register int
        fileatt = 0,                            /* file attributes */
        mode;                                   /* P_CHECK wanted? */
    char
        buf [80];                               /* conversion buf */
    struct stat
        statbuf;                                /* file stat buffer */

    mode = stack [sp].vu.intval;                /* get mode arg */
    fname = stack [sp - 1].vu.i->ls.str;        /* get file name */
    reg = newvar (e_list);                      /* 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);
        else
            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;

    sprintf (buf, "%u", fileatt);               /* file attr --> string */
    reg = addtolist (reg, buf);                 /* = element #0 */

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