1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * filestats.c -- Functions for keeping stats on file accesses
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | **************************************************************************/
|
---|
21 |
|
---|
22 | #include "sysfuncs.h"
|
---|
23 |
|
---|
24 | #include "memlib.h"
|
---|
25 | #include "filestats.h"
|
---|
26 | #include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
|
---|
27 |
|
---|
28 |
|
---|
29 | File *
|
---|
30 | Fopen (char *name, char *mode, unsigned long magic)
|
---|
31 | {
|
---|
32 | FILE *f;
|
---|
33 | File *F;
|
---|
34 | unsigned long m;
|
---|
35 | if (!(f = fopen (name, mode)))
|
---|
36 | return (NULL);
|
---|
37 | if (magic)
|
---|
38 | switch (*mode)
|
---|
39 | {
|
---|
40 | case 'r':
|
---|
41 | fread ((char *) &m, sizeof (m), 1, f);
|
---|
42 | NTOHUL(m); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
43 | if (m != magic)
|
---|
44 | {
|
---|
45 | fclose (f);
|
---|
46 | return (NULL);
|
---|
47 | }
|
---|
48 | break;
|
---|
49 | case 'w':
|
---|
50 | HTONUL(magic); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
51 | fwrite ((char *) &magic, sizeof (magic), 1, f);
|
---|
52 | }
|
---|
53 | if (!(F = Xmalloc (sizeof (File))))
|
---|
54 | {
|
---|
55 | fclose (f);
|
---|
56 | return (NULL);
|
---|
57 | }
|
---|
58 | F->pathname = Xstrdup (name);
|
---|
59 | F->name = strrchr (F->pathname, '/');
|
---|
60 | F->name = F->name ? F->name + 1 : F->pathname;
|
---|
61 | F->f = f;
|
---|
62 | F->Current.NumSeeks = F->Current.NumReads = F->Current.NumBytes = 0;
|
---|
63 | F->Cumulative = F->Current;
|
---|
64 | return (F);
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | int
|
---|
69 | Fclose (File * F)
|
---|
70 | {
|
---|
71 | int num;
|
---|
72 | if (!F)
|
---|
73 | return (0);
|
---|
74 | num = fclose (F->f);
|
---|
75 | if (F->pathname)
|
---|
76 | Xfree (F->pathname);
|
---|
77 | Xfree (F);
|
---|
78 | return (num);
|
---|
79 | }
|
---|
80 |
|
---|
81 | size_t
|
---|
82 | Fread (void *ptr, size_t size, size_t nitems, File * F)
|
---|
83 | {
|
---|
84 | int num;
|
---|
85 | num = fread ((char *) ptr, size, nitems, F->f);
|
---|
86 | F->Current.NumReads++;
|
---|
87 | F->Current.NumBytes += num * size;
|
---|
88 | return (num);
|
---|
89 | }
|
---|
90 |
|
---|
91 | int
|
---|
92 | Fseek (File * F, long offset, int ptrname)
|
---|
93 | {
|
---|
94 | int num;
|
---|
95 | num = fseek (F->f, offset, ptrname);
|
---|
96 | F->Current.NumSeeks++;
|
---|
97 | return (num);
|
---|
98 | }
|
---|
99 |
|
---|
100 | void
|
---|
101 | Rewind (File * F)
|
---|
102 | {
|
---|
103 | rewind (F->f);
|
---|
104 | F->Current.NumSeeks++;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | void
|
---|
109 | ZeroFileStats (File * F)
|
---|
110 | {
|
---|
111 | F->Cumulative.NumSeeks += F->Current.NumSeeks;
|
---|
112 | F->Cumulative.NumReads += F->Current.NumReads;
|
---|
113 | F->Cumulative.NumBytes += F->Current.NumBytes;
|
---|
114 | F->Current.NumSeeks = F->Current.NumReads = F->Current.NumBytes = 0;
|
---|
115 | }
|
---|