(struct composition_it): New member lookback.
[bpt/emacs.git] / src / getloadavg.c
index 635e5ac..a174fc0 100644 (file)
@@ -1,6 +1,10 @@
 /* Get the system load averages.
-   Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995
-       Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995,
+                 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+                 Free Software Foundation, Inc.
+
+   NOTE: The canonical source of this file is maintained with gnulib.
+   Bugs can be reported to bug-gnulib@gnu.org.
 
    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
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    USA.  */
 
 /* Compile-time symbols that this file uses:
 
+   HAVE_PSTAT_GETDYNAMIC       Define this if your system has the
+                                pstat_getdynamic function.  I think it
+                               is unique to HPUX9.  The best way to get the
+                               definition is through the AC_FUNC_GETLOADAVG
+                               macro that comes with autoconf 2.13 or newer.
+                               If that isn't an option, then just put
+                               AC_CHECK_FUNCS(pstat_getdynamic) in your
+                               configure.in file.
    FIXUP_KERNEL_SYMBOL_ADDR()  Adjust address in returned struct nlist.
    KERNEL_FILE                 Pathname of the kernel to nlist.
    LDAV_CVT()                  Scale the load average from the kernel.
    LOAD_AVE_TYPE               Type of the load average array in the kernel.
                                Must be defined unless one of
                                apollo, DGUX, NeXT, or UMAX is defined;
+                                or we have libkstat;
                                otherwise, no load average is available.
+   HAVE_NLIST_H                 nlist.h is available.  NLIST_STRUCT defaults
+                                to this.
    NLIST_STRUCT                        Include nlist.h, not a.out.h, and
                                the nlist n_name element is a pointer,
                                not an array.
-   NLIST_NAME_UNION            struct nlist has an n_un member, not n_name.
+   HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'.
    LINUX_LDAV_FILE             [__linux__]: File containing load averages.
+   HAVE_LOCALE_H                locale.h is available.
+   HAVE_SETLOCALE               The `setlocale' function is available.
 
    Specific system predefines this file uses, aside from setting
    default values if not emacs:
@@ -43,7 +60,7 @@
    DGUX
    eunice                      UNIX emulator under VMS.
    hpux
-   MSDOS                       No-op for MSDOS.
+   __MSDOS__                   No-op for MSDOS.
    NeXT
    sgi
    sequent                     Sequent Dynix 3.x.x (BSD)
@@ -52,7 +69,7 @@
    UMAX
    UMAX4_3
    VMS
-   WIN32                       No-op for Windows95/NT.
+   WINDOWS32                   No-op for Windows95/NT.
    __linux__                   Linux: assumes /proc filesystem mounted.
                                Support from Michael K. Johnson.
    __NetBSD__                  NetBSD: assumes /kern filesystem mounted.
@@ -73,7 +90,7 @@
 /* Both the Emacs and non-Emacs sections want this.  Some
    configuration files' definitions for the LOAD_AVE_CVT macro (like
    sparc.h's) use macros like FSCALE, defined here.  */
-#ifdef unix
+#if defined (unix) || defined (__unix)
 # include <sys/param.h>
 #endif
 
 extern int errno;
 #endif
 
-#ifndef HAVE_GETLOADAVG
+#ifdef HAVE_LOCALE_H
+# include <locale.h>
+#endif
+#ifndef HAVE_SETLOCALE
+# define setlocale(Category, Locale) /* empty */
+#endif
 
+#ifndef HAVE_GETLOADAVG
 
 /* The existing Emacs configuration files define a macro called
    LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
@@ -123,6 +146,12 @@ extern int errno;
 #  undef FSCALE
 # endif
 
+/* Same issues as for NeXT apply to the HURD-based GNU system.  */
+# ifdef __GNU__
+#  undef BSD
+#  undef FSCALE
+# endif /* __GNU__ */
+
 /* Set values that are different from the defaults, which are
    set a little farther down with #ifndef.  */
 
@@ -159,6 +188,9 @@ extern int errno;
 
 # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
 #  define OSF_ALPHA
+#  include <sys/mbuf.h>
+#  include <sys/socket.h>
+#  include <net/route.h>
 #  include <sys/table.h>
 # endif
 
@@ -270,7 +302,7 @@ extern int errno;
 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
    above under #ifdef MIPS.  But we want the sgi value.  */
 #   undef FSCALE
-#   define     FSCALE 1000.0
+#   define FSCALE 1000.0
 #  endif
 
 #  if defined (ardent) && defined (titan)
@@ -288,70 +320,14 @@ extern int errno;
 # endif        /* Not FSCALE.  */
 
 # if !defined (LDAV_CVT) && defined (FSCALE)
-#  define      LDAV_CVT(n) (((double) (n)) / FSCALE)
+#  define LDAV_CVT(n) (((double) (n)) / FSCALE)
 # endif
 
-/* VAX C can't handle multi-line #ifs, or lines longer that 256 characters.  */
 # ifndef NLIST_STRUCT
-
-#  ifdef MORE_BSD
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef sun
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef decstation
+#  if HAVE_NLIST_H
 #   define NLIST_STRUCT
 #  endif
-
-#  ifdef hpux
-#   define NLIST_STRUCT
-#  endif
-
-#  if defined (_SEQUENT_) || defined (sequent)
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef sgi
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef SVR4
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef sony_news
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef OSF_ALPHA
-#   define NLIST_STRUCT
-#  endif
-
-#  if defined (ardent) && defined (titan)
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef tek4300
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef butterfly
-#   define NLIST_STRUCT
-#  endif
-
-#  if defined(alliant) && defined(i860) /* Alliant FX/2800 */
-#   define NLIST_STRUCT
-#  endif
-
-#  ifdef _AIX
-#   define NLIST_STRUCT
-#  endif
-
-# endif /* defined (NLIST_STRUCT) */
-
+# endif
 
 # if defined(sgi) || (defined(mips) && !defined(BSD))
 #  define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
@@ -436,6 +412,14 @@ extern int errno;
 
 # endif /* LOAD_AVE_TYPE */
 
+# if defined(__GNU__) && !defined (NeXT)
+/* Note that NeXT Openstep defines __GNU__ even though it should not.  */
+/* GNU system acts much like NeXT, for load average purposes,
+   but not exactly.  */
+#  define NeXT
+#  define host_self mach_host_self
+# endif
+
 # ifdef NeXT
 #  ifdef HAVE_MACH_MACH_H
 #   include <mach/mach.h>
@@ -497,23 +481,23 @@ static unsigned int samples;
 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
 # endif /* DGUX */
 
-# ifdef LOAD_AVE_TYPE
+#if !defined(HAVE_LIBKSTAT) && defined(LOAD_AVE_TYPE)
 /* File descriptor open to /dev/kmem or VMS load ave driver.  */
 static int channel;
-/* Nonzero iff channel is valid.  */
+/* Nonzero if channel is valid.  */
 static int getloadavg_initialized;
 /* Offset in kmem to seek to read load average, or 0 means invalid.  */
 static long offset;
 
 #  if !defined(VMS) && !defined(sgi) && !defined(__linux__)
-static struct nlist nl[2];
+static struct nlist name_list[2];
 #  endif /* Not VMS or sgi */
 
 #  ifdef SUNOS_5
 static kvm_t *kd;
 #  endif /* SUNOS_5 */
 
-# endif /* LOAD_AVE_TYPE */
+#endif /* LOAD_AVE_TYPE && !HAVE_LIBKSTAT */
 \f
 /* Put the 1 minute, 5 minute and 15 minute load averages
    into the first NELEM elements of LOADAVG.
@@ -535,7 +519,7 @@ getloadavg (loadavg, nelem)
   elem = -1;
 # endif
 
-# if !defined (LDAV_DONE) && defined (SUNOS_5)
+# if !defined (LDAV_DONE) && defined (HAVE_LIBKSTAT)
 /* Use libkstat because we don't have to be root.  */
 #  define LDAV_DONE
   kstat_ctl_t *kc;
@@ -580,7 +564,7 @@ getloadavg (loadavg, nelem)
     }
 
   kstat_close (kc);
-# endif /* SUNOS_5 */
+# endif /* HAVE_LIBKSTAT */
 
 # if !defined (LDAV_DONE) && defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
 /* Use pstat_getdynamic() because we don't have to be root.  */
@@ -619,8 +603,11 @@ getloadavg (loadavg, nelem)
   if (count <= 0)
     return -1;
 
+  /* The following sscanf must use the C locale.  */
+  setlocale (LC_NUMERIC, "C");
   count = sscanf (ldavgbuf, "%lf %lf %lf",
                  &load_ave[0], &load_ave[1], &load_ave[2]);
+  setlocale (LC_NUMERIC, "");
   if (count < 1)
     return -1;
 
@@ -818,7 +805,7 @@ getloadavg (loadavg, nelem)
        : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
 # endif        /* OSF_MIPS */
 
-# if !defined (LDAV_DONE) && (defined (MSDOS) || defined (WIN32))
+# if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
 #  define LDAV_DONE
 
   /* A faithful emulation is going to have to be saved for a rainy day.  */
@@ -826,7 +813,7 @@ getloadavg (loadavg, nelem)
     {
       loadavg[elem] = 0.0;
     }
-# endif  /* MSDOS */
+# endif  /* __MSDOS__ || WINDOWS32 */
 
 # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
 #  define LDAV_DONE
@@ -893,32 +880,32 @@ getloadavg (loadavg, nelem)
     {
 #  ifndef sgi
 #   ifndef NLIST_STRUCT
-      strcpy (nl[0].n_name, LDAV_SYMBOL);
-      strcpy (nl[1].n_name, "");
+      strcpy (name_list[0].n_name, LDAV_SYMBOL);
+      strcpy (name_list[1].n_name, "");
 #   else /* NLIST_STRUCT */
-#    ifdef NLIST_NAME_UNION
-      nl[0].n_un.n_name = LDAV_SYMBOL;
-      nl[1].n_un.n_name = 0;
-#    else /* not NLIST_NAME_UNION */
-      nl[0].n_name = LDAV_SYMBOL;
-      nl[1].n_name = 0;
-#    endif /* not NLIST_NAME_UNION */
+#    ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
+      name_list[0].n_un.n_name = LDAV_SYMBOL;
+      name_list[1].n_un.n_name = 0;
+#    else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
+      name_list[0].n_name = LDAV_SYMBOL;
+      name_list[1].n_name = 0;
+#    endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
 #   endif /* NLIST_STRUCT */
 
 #   ifndef SUNOS_5
       if (
 #    if !(defined (_AIX) && !defined (ps2))
-         nlist (KERNEL_FILE, nl)
+         nlist (KERNEL_FILE, name_list)
 #    else  /* _AIX */
-         knlist (nl, 1, sizeof (nl[0]))
+         knlist (name_list, 1, sizeof (name_list[0]))
 #    endif
          >= 0)
-         /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i.  */
+         /* Omit "&& name_list[0].n_type != 0 " -- it breaks on Sun386i.  */
          {
 #    ifdef FIXUP_KERNEL_SYMBOL_ADDR
-           FIXUP_KERNEL_SYMBOL_ADDR (nl);
+           FIXUP_KERNEL_SYMBOL_ADDR (name_list);
 #    endif
-           offset = nl[0].n_value;
+           offset = name_list[0].n_value;
          }
 #   endif /* !SUNOS_5 */
 #  else  /* sgi */
@@ -939,7 +926,7 @@ getloadavg (loadavg, nelem)
        {
          /* Set the channel to close on exec, so it does not
             litter any child's descriptor table.  */
-#   ifdef FD_SETFD
+#   ifdef F_SETFD
 #    ifndef FD_CLOEXEC
 #     define FD_CLOEXEC 1
 #    endif
@@ -954,8 +941,8 @@ getloadavg (loadavg, nelem)
       if (kd != 0)
        {
          /* nlist the currently running kernel.  */
-         kvm_nlist (kd, nl);
-         offset = nl[0].n_value;
+         kvm_nlist (kd, name_list);
+         offset = name_list[0].n_value;
          getloadavg_initialized = 1;
        }
 #  endif /* SUNOS_5 */
@@ -1050,3 +1037,6 @@ main (argc, argv)
   exit (0);
 }
 #endif /* TEST */
+
+/* arch-tag: 2b37a242-6289-41f4-8cd5-0e73fd615db1
+   (do not change this comment) */