---
name: "libdi"
section: "3"
platform: "linux"
package: "libdi-dev"
version: "6.0.0-1+b1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/libdi"
---

# libdi(3)

disk information library

## Name

**libdi** - disk information library

## Synopsis

### Initialization

\#include \<di.h\>

void \* **di\_initialize** (void);\
 int **di\_process\_options** (void \**di\_data*, int *targc*, const char \**targv* \[\], int *offset*);\
 void **di\_cleanup** (void \**di\_data*);\
 const char \***di\_version** (void);

### Options

int **di\_check\_option** (void \**di\_data*, int *option*);\
 void **di\_format\_iter\_init** (void \**di\_data*);\
 int **di\_format\_iterate** (void \**di\_data*);\

### Getting data

void **di\_get\_all\_disk\_info** (void \**di\_data*);\
 int **di\_iterate\_init** (void \**di\_data*, int *iteroption*);\

```
  typedef struct {
    const char   *strdata [DI_DISP_MAX];      /* string data */
    int          index;             /* internal index value */
    int          doPrint;           /* printable flag based on options */
    int          printFlag;         /* print result flag */
    int          isLocal;           /* filesystem is local */
    int          isReadOnly;        /* filesystem is read-only */
    int          isLoopback;        /* filesystme is a loopback filesystem */
  } di_pub_disk_info_t;
```

const di\_pub\_disk\_info\_t \* **di\_iterate** (void \**di\_data*);\
 int **di\_get\_scale\_max** (void \**di\_data*, int *index*,\
\
     int *valueidxA*, int *valueidxB*, int *valueidxC*);\
 double **di\_get\_scaled** (void \**di\_data*, int *index*,\
\
     int *scaleidx*, int *valueidxA*, int *valueidxB*, int *valueidxC*);\
 void **di\_disp\_scaled** (void \**di\_data*, char \**buff*, long *buffsz*,\
\
     int *index*, int *scaleidx*,\
\
     int *valueidxA*, int *valueidxB*, int *valueidxC*);\
 double **di\_get\_perc** (void \**di\_data*, int *index*,\
\
     int *valueidxA*, int *valueidxB*,\
\
     int *valueidxC*, int *valueidxB*, int *valueidxE*);\
 void **di\_disp\_perc** (void \**di\_data*, char \**buff*, long *buffsz*,\
\
     int *index*,\
\
     int *valueidxA*, int *valueidxB*,\
\
     int *valueidxC*, int *valueidxB*, int *valueidxE*);

## Overview

### Initialization

**di\_initialize** returns a pointer to a *di\_data* structure, used in all of the other library calls. The returned pointer must be freed with **di\_cleanup**.

**di\_process\_options** processes the arguments to *di*. These are passed as strings via an *argv* structure as if on the command line.  If no call to **di\_process\_options** is made, *di* will use the defaults and the *DI\_ARGS* environment variable will not be processed.

**di\_cleanup** frees all allocated data.

### Options

**di\_check\_option** returns the value of the specified option. All options will return a boolean value, excepting: DI\_OPT\_DEBUG, DI\_OPT\_FMT\_STR\_LEN, DI\_OPT\_SCALE and DI\_OPT\_BLOCK\_SZ.

*option* may be one of:

-

  DI\_OPT\_POSIX\_COMPAT\
   DI\_OPT\_QUOTA\_CHECK\
   DI\_OPT\_DISP\_CSV\
   DI\_OPT\_DISP\_CSV\_TAB\
   DI\_OPT\_EXCL\_LOOPBACK\
   DI\_OPT\_DISP\_JSON\
   DI\_OPT\_DISP\_TOTALS\
   DI\_OPT\_DISP\_HEADER\
   DI\_OPT\_DISP\_ALL\
   DI\_OPT\_LOCAL\_ONLY\
   DI\_OPT\_NO\_SYMLINK\
   DI\_OPT\_DEBUG\
   DI\_OPT\_FMT\_STR\_LEN\
   DI\_OPT\_SCALE\
   DI\_OPT\_BLOCK\_SZ

**di\_format\_iter\_init** initializes the format string iterator.

**di\_format\_iterate** iterates through each format character in the format string.  Note that *di* allows unrecognized format characters.  In the main *di* program, these are printed as-is.

### Getting Data

**di\_get\_all\_disk\_info** retrieves and processes all of the disk information from the operating system.

**di\_iterate\_init** initializes the disk information iterator. *iteroption* is one of DI\_ITER\_PRINTABLE or DI\_ITER\_ALL. If DI\_ITER\_PRINTABLE is specified, only the partitions with the *doPrint* flag set will be returned by the iterator.

**di\_iterate** iterates through the filesystems, returning a *di\_pub\_disk\_info\_t* structure for each filesystem.  If the *--totals* flag was specified as an option, the totals will be the last item returned by the iterator.

**di\_get\_scale\_max** gets the maximum scaling unit for the space value.

*index* is the index from the *di\_pub\_disk\_info\_t* structure.

The *valueidx* arguments indicate which disk space values to use to calculate the the space value.  The formula is: **A - (B - C)**. Valid combinations are:

-

  DI\_SPACE\_TOTAL, DI\_VALUE\_NONE, DI\_VALUE\_NONE    // total\
   DI\_SPACE\_TOTAL, DI\_SPACE\_FREE, DI\_SPACE\_AVAIL   // used\
   DI\_SPACE\_TOTAL, DI\_SPACE\_FREE, DI\_VALUE\_NONE    // used\
   DI\_SPACE\_TOTAL, DI\_SPACE\_AVAIL, DI\_VALUE\_NONE   // used\
   DI\_SPACE\_FREE, DI\_VALUE\_NONE, DI\_VALUE\_NONE     // free\
   DI\_SPACE\_AVAIL, DI\_VALUE\_NONE, DI\_VALUE\_NONE    // free\
   DI\_INODE\_TOTAL, DI\_VALUE\_NONE, DI\_VALUE\_NONE    // inode total\
   DI\_INODE\_TOTAL, DI\_INODE\_FREE, DI\_VALUE\_NONE    // inodes used\
   DI\_INODE\_FREE, DI\_VALUE\_NONE, DI\_VALUE\_NONE     // inodes free

**di\_get\_scaled** gets a space value scaled according to the scaling unit.

*index* is the index from the *di\_pub\_disk\_info\_t* structure.

*scaleidx* is the scaling index to use.  Use a scaling index returned from **di\_get\_scale\_max** or use one of the following values:

-

  DI\_SCALE\_BYTE\
   DI\_SCALE\_KILO\
   DI\_SCALE\_MEGA\
   DI\_SCALE\_GIGA\
   DI\_SCALE\_TERA\
   DI\_SCALE\_PETA\
   DI\_SCALE\_EXA\
   DI\_SCALE\_ZETTA\
   DI\_SCALE\_YOTTA\
   DI\_SCALE\_RONNA\
   DI\_SCALE\_QUETTA

The *valueidx* arguments indicate which disk space values to use to calculate the the space value.  The formula is: **A - (B - C)**. See **di\_get\_scale\_max** for a list of valid combinations.

**di\_disp\_scaled** gets a space value scaled according to the scaling unit, and creates a string that can be printed.

**buff** is the character string where the string will be stored.

**buffsz** is the size of the character string.

*index* is the index from the *di\_pub\_disk\_info\_t* structure.

*scaleidx* is the scaling index to use.  Use a scaling index returned from **di\_get\_scale\_max** or one of the values as listed in **di\_get\_scaled**.

The *valueidx* arguments indicate which disk space values to use to calculate the the space value.  The formula is: **A - (B - C)**. See **di\_get\_scale\_max** for a list of valid combinations.

**di\_get\_perc** retrieves the disk space value as a percentage.

*index* is the index from the *di\_pub\_disk\_info\_t* structure.

The *valueidx* arguments indicate which disk space values to use to calculate the percentage.  The formula is: **(A - B) / (C - (D - E))**.

Valid combinations are:

-

  /\* percent used \*/\
   DI\_SPACE\_TOTAL, DI\_SPACE\_AVAIL,\
   DI\_SPACE\_TOTAL, DI\_VALUE\_NONE, DI\_VALUE\_NONE\
   /\* percent used \*/\
   DI\_SPACE\_TOTAL, DI\_SPACE\_FREE,\
   DI\_SPACE\_TOTAL, DI\_VALUE\_NONE, DI\_VALUE\_NONE\
   /\* percent used, BSD style \*/\
   DI\_SPACE\_TOTAL, DI\_SPACE\_FREE,\
   DI\_SPACE\_TOTAL, DI\_VALUE\_FREE, DI\_VALUE\_AVAIL\
   /\* percent free \*/\
   DI\_SPACE\_AVAIL, DI\_VALUE\_NONE,\
   DI\_SPACE\_TOTAL, DI\_VALUE\_NONE, DI\_VALUE\_NONE,\
   /\* percent free \*/\
   DI\_SPACE\_FREE, DI\_VALUE\_NONE,\
   DI\_SPACE\_TOTAL, DI\_VALUE\_NONE, DI\_VALUE\_NONE,\
   /\* inodes used \*/\
   DI\_INODE\_TOTAL, DI\_INODE\_AVAIL,\
   DI\_INODE\_TOTAL, DI\_VALUE\_NONE, DI\_VALUE\_NONE\

**di\_disp\_perc** retrieves the disk space value as a percentage and creates a string that can be printed.

**buff** is the character string where the string will be stored.

**buffsz** is the size of the character string.

*index* is the index from the *di\_pub\_disk\_info\_t* structure.

The *valueidx* arguments indicate which disk space values to use to calculate the percentage.  The formula is: **(A - B) / (C - (D - E))**.  See **di\_get\_perc** for valid combinations.

## Example

```
    /* this code is in the public domain */
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <math.h>
    #include <di.h>
    enum {
      HAVE_SPACE = 0,
      NO_SPACE = 1,
    };
    int
    check_space (const char *fn, double space_wanted) {
      void        *di_data;
      int         targc;
      const char  *targv [10];
      int         exitflag;
      const di_pub_disk_info_t  *pub;
      int         rval = NO_SPACE;
      int         count;
      targc = 1;
      targv [0] = fn;
      targv [1] = NULL;
      di_data = di_initialize ();
      exitflag = di_process_options (di_data, targc, targv, 0);
      if (exitflag != DI_EXIT_NORM) {
        di_cleanup (di_data);
        exit (exitflag);
      }
      di_get_all_disk_info (di_data);
      count = di_iterate_init (di_data, DI_ITER_PRINTABLE);
      while ((pub = di_iterate (di_data)) != NULL) {
        double    dval;
        /* compare terabytes available */
        dval = di_get_scaled (di_data, pub->index, DI_SCALE_TERA,
            DI_SPACE_AVAIL, DI_VALUE_NONE, DI_VALUE_NONE);
        if (dval >= space_wanted) {
          rval = HAVE_SPACE;
        }
      }
      di_cleanup (di_data);
      return rval;
    }
    int
    main (int argc, char *argv []) {
      const char  *fn = "/home/bll/s/di";
      double      spwant = 0.2;   /* terabytes */
      bool        rval;
      if (argc > 1) {
        fn = argv [1];
      }
      if (argc > 2) {
        spwant = atof (argv [2]);
      }
      rval = check_space (fn, spwant);
      if (rval == NO_SPACE) {
        fprintf (stdout, "Not enough disk space0);
      }
      if (rval == HAVE_SPACE) {
        fprintf (stdout, "Enough disk space0);
      }
      return rval;
    }
```

### Compile and Link

```
    dilibd=$HOME/local/lib
    diinc=$(pkg-config --with-path ${dilibd}/pkgconfig --cflags di)
    dilibs=$(pkg-config --with-path ${dilibd}/pkgconfig --libs di)
    cc -o tt ${diinc} tt.c -Wl,-R${dilibd} ${dilibs})
```

### Example Run

```
    bll-g7:bll$ ./tt /home/bll 0.5
    Not enough disk space
    bll-g7:bll$ ./tt /home/bll 0.1
    Enough disk space
```

## See Also

[df(1)](/df/1.md), [di(1)](/di/1.md)

## Bugs

Open a ticket at https://sourceforge.net/p/diskinfo-di/tickets/\
 Send bug reports to: brad.lanam.di @ gmail.com

## Links

Home Page: https://diskinfo-di.sourceforge.io/\
 Wiki: https://sourceforge.net/p/diskinfo-di/wiki/Home/\
 Change Log: https://sourceforge.net/p/diskinfo-di/wiki/ChangeLog/

## Author

Copyright 1994-2025 by Brad Lanam Pleasant Hill, CA
