]> Savannah Git Hosting - gnulib.git/commitdiff
boot-time: New module.
authorBruno Haible <bruno@clisp.org>
Thu, 10 Aug 2023 11:08:19 +0000 (13:08 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 10 Aug 2023 11:08:19 +0000 (13:08 +0200)
* lib/boot-time.h: New file.
* lib/boot-time.c: New file.
* modules/boot-time: New file.

ChangeLog
lib/boot-time.c [new file with mode: 0644]
lib/boot-time.h [new file with mode: 0644]
modules/boot-time [new file with mode: 0644]

index b167189c03d018200c1f4707ad758f7599742cd2..cb3c9377974c7b443057ffe9062b3e499dec47f6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2023-08-10  Bruno Haible  <bruno@clisp.org>
+
+       boot-time: New module.
+       * lib/boot-time.h: New file.
+       * lib/boot-time.c: New file.
+       * modules/boot-time: New file.
+
 2023-08-10  Bruno Haible  <bruno@clisp.org>
 
        readutmp: Return a boot time also on Android.
diff --git a/lib/boot-time.c b/lib/boot-time.c
new file mode 100644 (file)
index 0000000..fe1e13e
--- /dev/null
@@ -0,0 +1,43 @@
+/* Determine the time when the machine last booted.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file 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, either version 3 of the License,
+   or (at your option) any later version.
+
+   This file 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.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "boot-time.h"
+
+#include <stddef.h>
+
+#include "idx.h"
+#include "readutmp.h"
+
+int
+get_boot_time (struct timespec *p_boot_time)
+{
+  idx_t n_entries = 0;
+  STRUCT_UTMP *utmp = NULL;
+  read_utmp (UTMP_FILE, &n_entries, &utmp, READ_UTMP_BOOT_TIME);
+  if (n_entries > 0)
+    {
+      *p_boot_time = utmp[0].ut_ts;
+      free (utmp);
+      return 0;
+    }
+  free (utmp);
+  return -1;
+}
diff --git a/lib/boot-time.h b/lib/boot-time.h
new file mode 100644 (file)
index 0000000..159a005
--- /dev/null
@@ -0,0 +1,38 @@
+/* Determine the time when the machine last booted.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+   This file 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, either version 3 of the License,
+   or (at your option) any later version.
+
+   This file 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.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>.  */
+
+#ifndef _BOOT_TIME_H
+#define _BOOT_TIME_H
+
+#include <time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* Store the approximate time when the machine last booted in *P_BOOT_TIME,
+   and return 0.  If it cannot be determined, return -1.  */
+extern int get_boot_time (struct timespec *p_boot_time);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _BOOT_TIME_H */
diff --git a/modules/boot-time b/modules/boot-time
new file mode 100644 (file)
index 0000000..99ae5c9
--- /dev/null
@@ -0,0 +1,27 @@
+Description:
+Determine the time when the machine last booted.
+
+Files:
+lib/boot-time.h
+lib/boot-time.c
+
+Depends-on:
+idx
+readutmp
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += boot-time.c
+
+Include:
+"boot-time.h"
+
+Link:
+$(READUTMP_LIB)
+
+License:
+GPL
+
+Maintainer:
+all