ReactOS  0.4.15-dev-1177-g6cb3b62
tickcount.c
Go to the documentation of this file.
1 /*
2  tickcount -- Display the kernel tick count (or any tick count passed as an
3  argument or as input) in human-readable format
4 
5  This is public domain software
6 */
7 
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <tchar.h>
12 #include <windows.h>
13 
14 typedef __int64 int64_;
15 typedef unsigned __int64 uint64_;
16 
17 #define TICKS_YEAR (TICKS_DAY * ((uint64_)365))
18 #define TICKS_MONTH (TICKS_DAY * ((uint64_)30))
19 #define TICKS_WEEK (TICKS_DAY * ((uint64_)7))
20 #define TICKS_DAY (TICKS_HOUR * ((uint64_)24))
21 #define TICKS_HOUR (TICKS_MINUTE * ((uint64_)60))
22 #define TICKS_MINUTE (TICKS_SECOND * ((uint64_)60))
23 #define TICKS_SECOND ((uint64_)1000)
24 
25 #define SLICES_COUNT (sizeof(ticks_per_slice) / sizeof(ticks_per_slice[0]))
26 
28 {
29  TICKS_YEAR,
31  TICKS_WEEK,
32  TICKS_DAY,
33  TICKS_HOUR,
36  1
37 };
38 
40 {
41  _T("year"),
42  _T("month"),
43  _T("week"),
44  _T("day"),
45  _T("hour"),
46  _T("minute"),
47  _T("second"),
48  _T("millisecond")
49 };
50 
52 {
53  _T("years"),
54  _T("months"),
55  _T("weeks"),
56  _T("days"),
57  _T("hours"),
58  _T("minutes"),
59  _T("seconds"),
60  _T("milliseconds")
61 };
62 
63 void print_uptime
64 (
65  uint64_ tickcount,
66  uint64_ prevsliceval,
67  _TCHAR * prevsliceunit,
68  int curslice
69 )
70 {
71  uint64_ tick_cur = tickcount / ticks_per_slice[curslice];
72  uint64_ tick_residual = tickcount % ticks_per_slice[curslice];
73 
74  assert(tick_cur <= (~((uint64_)0)));
75 
76  if(tick_residual == 0)
77  {
78  /* the current slice is the last */
79 
80  if(prevsliceval == 0)
81  {
82  /* the current slice is the only */
83  _tprintf
84  (
85  _T("%u %s"),
86  (unsigned)tick_cur,
87  (tick_cur == 1 ? slice_names_singular : slice_names_plural)[curslice]
88  );
89  }
90  else
91  {
92  /* the current slice is the last, and there's a previous slice */
93  assert(prevsliceunit);
94 
95  /* print the previous and the current slice, and terminate */
96  _tprintf
97  (
98  _T("%u %s %s %u %s"),
99  (unsigned)prevsliceval,
100  prevsliceunit,
101  _T("and"),
102  (unsigned)tick_cur,
103  (tick_cur == 1 ? slice_names_singular : slice_names_plural)[curslice]
104  );
105  }
106  }
107  else if(tick_cur != 0)
108  {
109  /* the current slice is not the last, and non-zero */
110 
111  if(prevsliceval != 0)
112  {
113  /* there's a previous slice: print it */
114  assert(prevsliceunit);
115  _tprintf(_T("%u %s, "), (unsigned)prevsliceval, prevsliceunit);
116  }
117 
118  /* recursion on the next slice size, storing the current slice */
120  (
121  tick_residual,
122  tick_cur,
123  (tick_cur == 1 ? slice_names_singular : slice_names_plural)[curslice],
124  curslice + 1
125  );
126  }
127  else
128  {
129  /*
130  the current slice is not the last, and zero: recursion, remembering the
131  previous non-zero slice
132  */
133  print_uptime(tick_residual, prevsliceval, prevsliceunit, curslice + 1);
134  }
135 }
136 
137 int parse_print(const _TCHAR * str)
138 {
139  int64_ tickcount;
140 
141  tickcount = _ttoi64(str);
142 
143  if(tickcount < 0)
144  tickcount = - tickcount;
145  else if(tickcount == 0)
146  return 1;
147 
148  print_uptime(tickcount, 0, NULL, 0);
149  _puttc(_T('\n'), stdout);
150 
151  return 0;
152 }
153 
154 int _tmain(int argc, _TCHAR * argv[])
155 {
156  int r;
157 
158  if(argc <= 1)
159  {
161  _puttc(_T('\n'), stdout);
162  }
163  else if(argc == 2 && argv[1][0] == _T('-') && argv[1][1] == 0)
164  {
165  while(!feof(stdin))
166  {
167  _TCHAR buf[23];
168  _TCHAR * str;
169 
170  str = _fgetts(buf, 22, stdin);
171 
172  if(str == NULL)
173  return 0;
174 
175  if((r = parse_print(str)) != 0)
176  return r;
177  }
178  }
179  else
180  {
181  int i;
182 
183  for(i = 1; i < argc; ++ i)
184  if((r = parse_print(argv[i])) != 0)
185  return r;
186  }
187 
188  return 0;
189 }
static int argc
Definition: ServiceArgs.c:12
#define _tprintf
Definition: tchar.h:506
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define TICKS_DAY
Definition: tickcount.c:20
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define _puttc
Definition: tchar.h:581
#define _fgetts
Definition: tchar.h:565
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
#define assert(x)
Definition: debug.h:53
_TCHAR * slice_names_plural[SLICES_COUNT]
Definition: tickcount.c:51
FILE * stdin
void print_uptime(uint64_ tickcount, uint64_ prevsliceval, _TCHAR *prevsliceunit, int curslice)
Definition: tickcount.c:64
#define argv
Definition: mplay32.c:18
#define TICKS_MINUTE
Definition: tickcount.c:22
FILE * stdout
#define TICKS_SECOND
Definition: tickcount.c:23
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define SLICES_COUNT
Definition: tickcount.c:25
#define TICKS_MONTH
Definition: tickcount.c:18
const WCHAR * str
smooth NULL
Definition: ftsmooth.c:416
unsigned __int64 uint64_
Definition: tickcount.c:15
#define TICKS_WEEK
Definition: tickcount.c:19
char _TCHAR
Definition: tchar.h:1392
#define _ttoi64
Definition: tchar.h:614
_Check_return_ _CRTIMP int __cdecl feof(_In_ FILE *_File)
#define _T(x)
Definition: vfdio.h:22
__int64 int64_
Definition: tickcount.c:14
int _tmain(int argc, _TCHAR *argv[])
Definition: tickcount.c:154
#define TICKS_YEAR
Definition: tickcount.c:17
int parse_print(const _TCHAR *str)
Definition: tickcount.c:137
uint64_ ticks_per_slice[]
Definition: tickcount.c:27
_TCHAR * slice_names_singular[SLICES_COUNT]
Definition: tickcount.c:39
#define TICKS_HOUR
Definition: tickcount.c:21
#define __int64
Definition: basetyps.h:16