Flush out the last dregs in the terminal before quitting when
[unix-history] / usr / src / usr.bin / gprof / hertz.c
index c465ecc..73a30fc 100644 (file)
@@ -1,54 +1,43 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-    static     char *sccsid = "@(#)hertz.c     1.1 (Berkeley) %G%";
-#endif lint
+static char sccsid[] = "@(#)hertz.c    5.3 (Berkeley) %G%";
+#endif /* not lint */
+
+#include <sys/time.h>
 
     /*
      * discover the tick frequency of the machine
 
     /*
      * discover the tick frequency of the machine
+     * if something goes wrong, we return 0, an impossible hertz.
      */
      */
-#include <nlist.h>
-#include <stdio.h>
-
-struct nlist   nl[] =  {{"_hz"},       /* clock ticks per second */
-                        {0}};
+#define        HZ_WRONG        0
 
 hertz()
 {
 
 hertz()
 {
-    int                kmem;                   /* file descriptor for /dev/kmem */
-    long       lseek();
-    long       seeked;                 /* return value from lseek() */
-    long       hz;                     /* buffer for reading from system */
-    int                red;                    /* return value from read() */
-    int                closed;                 /* return value from close() */
+       struct itimerval tim;
 
 
-#   define     VMUNIX  "/vmunix"       /* location of the system namelist */
-    nlist(VMUNIX, nl);
-    if (nl[0].n_type == 0) {
-       fprintf(stderr, "no %s namelist entry for _hz\n", VMUNIX);
-       return 0;
-    }
-#   define     KMEM    "/dev/kmem"     /* location of the system data space */
-    kmem = open(KMEM, 0);
-    if (kmem == -1) {
-       perror("hertz()");
-       fprintf(stderr, "open(\"%s\", 0)", KMEM);
-       return 0;
-    }
-    seeked = lseek(kmem, nl[0].n_value, 0);
-    if (seeked == -1) {
-       fprintf(stderr, "can't lseek(kmem, 0x%x, 0)\n", nl[0].n_value);
-       return 0;
-    }
-    red = read(kmem, &hz, sizeof hz);
-    if (red != sizeof hz) {
-       fprintf(stderr, "read(kmem, 0x%x, %d) returned %d\n",
-               &hz, sizeof hz, red);
-       return 0;
-    }
-    closed = close(kmem);
-    if (closed != 0) {
-       perror("hertz()");
-       fprintf(stderr, "close(\"%s\")", KMEM);
-       return 0;
-    }
-    return hz;
+       tim.it_interval.tv_sec = 0;
+       tim.it_interval.tv_usec = 1;
+       tim.it_value.tv_sec = 0;
+       tim.it_value.tv_usec = 0;
+       setitimer(ITIMER_REAL, &tim, 0);
+       setitimer(ITIMER_REAL, 0, &tim);
+       if (tim.it_interval.tv_usec < 2)
+               return(HZ_WRONG);
+       return (1000000 / tim.it_interval.tv_usec);
 }
 }