File: tmp_full.c

package info (click to toggle)
lbcd 3.3.0-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 492 kB
  • ctags: 345
  • sloc: ansic: 2,907; sh: 242; makefile: 161; perl: 97
file content (78 lines) | stat: -rw-r--r-- 1,593 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
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
74
75
76
77
78
/*
 * tmp_full.c [las 27 Oct 1996]
 *
 * A simple routine to obtain the percentage of free space in /tmp.
 * Machines with a full tmp partition are often unusable.
 *
 * Note: Does not support Ultrix and hard-codes the minfree values.
 * Note: For unsupported systems, always reports 0% full.
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#elif HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif

#ifndef USER_FBLOCKS
/* Percentage of the fblocks on the partition which a user may use. */
#if defined(__sgi__) || defined(_AIX)
#define USER_FBLOCKS 1.0
#else
#define USER_FBLOCKS 0.9
#endif
#endif

int
tmp_full(const char *path)
{
  int percent = 0;

#if defined(HAVE_SYS_STATVFS_H) || defined(HAVE_SYS_VFS_H)
  if (chdir(path) == 0) {
#ifdef HAVE_SYS_STATVFS_H
  struct statvfs tmp;
#elif HAVE_SYS_VFS_H
  struct statfs tmp;
#endif

#ifdef HAVE_SYS_STATVFS_H
    if (statvfs(".",&tmp) == 0) {
#else
    if (statfs(".",&tmp) == 0) {
#endif
      float pct = ((tmp.f_blocks * USER_FBLOCKS) - tmp.f_bavail) * 100 /
	(tmp.f_blocks * USER_FBLOCKS);
      percent = pct+0.5;	/* round result */
    }

    /* Sanity check */
    if (percent < 0) {
      percent = 0;
    } else if (percent > 100) {
      percent = 100;
    }
  }
#endif

  return percent;
}

#ifdef MAIN
int
main(char argc, char **argv)
{
  printf("%%%d percent tmp full.\n",tmp_full("/tmp"));
#ifdef P_tmpdir
  printf("%%%d percent P_tmpdir full.\n",tmp_full(P_tmpdir));
#endif
  return 0;
}
#endif