SECURE_CODING: Use snprintf/write_fmt_str instead of sprintf
[sxemacs] / lib-src / profile.c
1 /* profile.c --- generate periodic events for profiling of Emacs Lisp code.
2  Copyright (C) 1992, 1994 Free Software Foundation, Inc.
3
4  Author: Boaz Ben-Zvi <boaz@lcs.mit.edu>
5
6  This file is part of SXEmacs.
7
8  SXEmacs is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12
13  SXEmacs is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program.  If not, see <http://www.gnu.org/licenses/>. */
20
21 /* Synched up with: FSF 19.28. */
22 /* #### Not sure if this is needed for XEmacs. */
23
24 /**
25  **  To be run as an emacs process. Input string that starts with:
26  **    'z' -- resets the watch (to zero).
27  **    'p' -- return time (on stdout) as string with format <sec>.<micro-sec>
28  **    'q' -- exit.
29  **
30  **  abstraction : a stopwatch
31  **  operations: reset_watch, get_time
32  */
33 #include <config.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <assert.h>
37 #include "../src/systime.h"
38
39 static struct timeval TV1, TV2;
40 static int watch_not_started = 1;       /* flag */
41 static char time_string[64];
42
43 /* Reset the stopwatch to zero.  */
44
45 static void reset_watch(void)
46 {
47         EMACS_GET_TIME(TV1);
48         watch_not_started = 0;
49 }
50
51 /* This call returns the time since the last reset_watch call.  The time
52    is returned as a string with the format  <seconds>.<micro-seconds> 
53    If reset_watch was not called yet, exit.  */
54
55 static char *get_time(void)
56 {
57         int sz;
58         if (watch_not_started)
59                 exit(1);        /* call reset_watch first ! */
60         EMACS_GET_TIME(TV2);
61         if (TV1.tv_usec > TV2.tv_usec) {
62                 TV2.tv_usec += 1000000;
63                 TV2.tv_sec--;
64         }
65         sz = snprintf(time_string, sizeof(time_string), "%lu.%06lu",
66                       (unsigned long)TV2.tv_sec - TV1.tv_sec,
67                       (unsigned long)TV2.tv_usec - TV1.tv_usec);
68         assert(sz>=0 && sz<sizeof(time_string));
69         return time_string;
70 }
71
72 int main(int argc, char *argv[])
73 {
74         int c;
75         while ((c = getchar()) != EOF) {
76                 switch (c) {
77                 case 'z':
78                         reset_watch();
79                         break;
80                 case 'p':
81                         puts(get_time());
82                         break;
83                 case 'q':
84                         exit(0);
85                 default:
86                         break;
87                 }
88                 /* Anything remaining on the line is ignored.  */
89                 while (c != '\n' && c != EOF)
90                         c = getchar();
91         }
92         return 1;
93 }