File: uptime.c

package info (click to toggle)
ud 0.7.1-18
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 380 kB
  • ctags: 45
  • sloc: sh: 2,865; ansic: 784; makefile: 90
file content (46 lines) | stat: -rw-r--r-- 1,250 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
/*
   Uptime Daemon
   Copyright (C) 1998 Matthew Trent <root@piguy.dyn.ml.org>
   Changes by Johnny Teve_en <j.tevessen@line.org>
   Changes by Andreas Muck <andi@koala.rhein-neckar.de>
   Changes by John Campbell <jcampbel@lynn.ci-n.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 */

#include <stdio.h>
#include <stdlib.h>

float
get_uptime (void)
{
  static const char proc_uptime_path[] = "/proc/uptime";
  FILE *proc_uptime;
  float uptime;
  int err = 0;
  
  if ((proc_uptime = fopen (proc_uptime_path, "r")) == NULL)
    {
      fprintf (stderr, "Can't open `%s'.\n", proc_uptime_path);
      err = 1;
    }
  else
    {
      if (1 != fscanf (proc_uptime, "%f", &uptime))
	{
	  fprintf (stderr, "Error parsing `%s'.\n", proc_uptime_path);
	  err = 1;
	}
      (void) fclose (proc_uptime);
    }
  if (0 != err)
    exit (EXIT_FAILURE);
  return (uptime);
}