File: filesystem.c

package info (click to toggle)
moodss 9.0-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,540 kB
  • ctags: 609
  • sloc: sh: 8,869; tcl: 6,909; ansic: 113; makefile: 44
file content (42 lines) | stat: -rw-r--r-- 2,091 bytes parent folder | download | duplicates (2)
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
/* $Id: filesystem.c,v 1.3 1998/12/12 20:37:11 jfontain Exp $ */

/* to create the loadable library, use: cc -shared -o libfilesystem.so.1.0 -O2 -fPIC -Wall filesystem.c */
/* pkgIndex.tcl: package ifneeded filesystem 1.0 "load [file join $dir libfilesystem.so.1.0]" */

#include <tcl.h>
#include <sys/vfs.h>
#include <errno.h>
#include <string.h>

static int fileSystemStatistics(ClientData clientData, Tcl_Interp *interpreter, int numberOfArguments, Tcl_Obj * CONST arguments[])
{
    struct statfs statistics;
    Tcl_Obj *object;

    if(numberOfArguments != 2){
        Tcl_WrongNumArgs(interpreter, 1, arguments, "device");
        return TCL_ERROR;
    }
    if(statfs(Tcl_GetStringFromObj(arguments[1], 0), &statistics)<0){
        Tcl_SetObjResult(interpreter, Tcl_NewStringObj(strerror(errno), -1));                /* core takes care of freeing object */
        return TCL_ERROR;
    }
    object = Tcl_NewObj();
    Tcl_ListObjAppendElement(interpreter, object, Tcl_NewLongObj(statistics.f_bsize));             /* optimal transfer block size */
    Tcl_ListObjAppendElement(interpreter, object, Tcl_NewLongObj(statistics.f_blocks));                      /* total data blocks */
    Tcl_ListObjAppendElement(interpreter, object, Tcl_NewLongObj(statistics.f_bavail));  /* free blocks available to normal users */
    Tcl_ListObjAppendElement(interpreter, object, Tcl_NewLongObj(statistics.f_blocks - statistics.f_bfree));       /* used blocks */
    Tcl_SetObjResult(interpreter, object);                                                   /* core takes care of freeing object */
    return TCL_OK;
}

int Filesystem_Init(Tcl_Interp *interpreter)                                       /* create all commands in filesystem namespace */
{
    Tcl_CreateObjCommand(interpreter, "::filesystem::statistics", fileSystemStatistics, 0, 0);
    return Tcl_PkgProvide(interpreter, "filesystem", "1.0");
}

int Filesystem_SafeInit(Tcl_Interp *interpreter)
{
    return Filesystem_Init(interpreter);                                                                 /* all commands are safe */
}