/
my_histogram.c
207 lines (180 loc) · 5.45 KB
/
my_histogram.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <stddef.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <dirent.h>
/* function type that is called for each filename */
typedef int Myfunc(const char *, const struct stat *, int);
static Myfunc myfunc;
static int myftw(char *, Myfunc *);
static int dopath(Myfunc *);
static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
int
main(int argc, char *argv[])
{
int ret;
fprintf(stdout, "%s\n\n", "Content-type: text/html");
//fprintf(stdout, "argv1=%s\n", argv[1]);
if (argc != 2) {
printf("usage: ftw <starting-pathname>");
exit(0);
}
char *argument = argv[1];
if (strstr(argument, "directory=") != NULL)
{
argument = strstr(argument, "=")+1;
}
if (argument[0] != '/')
{
char c_dir[500];
getcwd(c_dir, PATH_MAX);
sprintf(c_dir, "%s/", c_dir);
//fprintf(stdout, "c_dir: %s/%s\n", c_dir, argument);
strcpy(argument, c_dir);
}
//printf("argv[1]: %s", argv[1]);
//printf("ARG: %s", argument);
ret = myftw(argument, myfunc); /* does it all */
ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
if (ntot == 0)
ntot = 1; /* avoid divide by 0; print 0 for all counts */
FILE *pipe = popen("gnuplot -persist", "w");
//printf("set boxwidth 0.5\nset style fill solid\nset key off\nset yrange [0:*]\nset xlabel \"Files\"\nset ylabel \"Frequency\"\nset title \"Frequency of File Types\"\n");
//printf("set term gif\nset output 'testhist2.gif'\n");
fprintf(pipe, "set term gif\nset output 'testhist3.gif'\nset title 'File Type Counts'\nset xlabel 'File Types'\nset ylabel 'Count'\nset boxwidth 0.5\nset style fill solid\nset key off\nset yrange [0:*]\n");
fprintf(pipe, "plot '-' using (column(0)):2:xtic(1) ti col with boxes\n");
fprintf(pipe, "NULL 0\n"); // I don't know why but the first column cuts off
fprintf(pipe, "regular %ld\n", nreg);
//nreg*100.0/ntot); this is %f
fprintf(pipe, "directory %ld\n", ndir);
//ndir*100.0/ntot);
fprintf(pipe, "block %ld\n", nblk);
// nblk*100.0/ntot);
fprintf(pipe, "character %ld\n", nchr);
// nchr*100.0/ntot);
fprintf(pipe, "fifo %ld\n", nfifo);
// nfifo*100.0/ntot);
fprintf(pipe, "link %ld\n", nslink);
// nslink*100.0/ntot);
fprintf(pipe, "socket %ld\n", nsock);
// nsock*100.0/ntot);
fprintf(pipe, "e");
char str[999];
FILE * file;
file = fopen( "hist_template" , "r");
if (file) {
while (fscanf(file, "%s", str)!=EOF)
fprintf(stdout, "%s \n",str);
fclose(file);
}
exit(ret);
}
/*
* Descend through the hierarchy, starting at "pathname".
* The caller's func() is called for every file.
*/
#define FTW_F 1 /* file other than directory */
#define FTW_D 2 /* directory */
#define FTW_DNR 3 /* directory that can't be read */
#define FTW_NS 4 /* file that we can't stat */
static char *fullpath; /* contains full pathname for every file */
static size_t pathlen;
static int /* we return whatever func() returns */
myftw(char *pathname, Myfunc *func)
{
fullpath = malloc(PATH_MAX+1); /* malloc PATH_MAX+1 bytes */
/* ({Prog pathalloc}) */
if (pathlen <= strlen(pathname)) {
pathlen = strlen(pathname) * 2;
if ((fullpath = realloc(fullpath, pathlen)) == NULL)
exit(0);
}
strcpy(fullpath, pathname);
return(dopath(func));
}
/*
* Descend through the hierarchy, starting at "fullpath".
* If "fullpath" is anything other than a directory, we lstat() it,
* call func(), and return. For a directory, we call ourself
* recursively for each name in the directory.
*/
static int /* we return whatever func() returns */
dopath(Myfunc* func)
{
struct stat statbuf;
struct dirent *dirp;
DIR *dp;
int ret, n;
if (lstat(fullpath, &statbuf) < 0) /* stat error */
return(func(fullpath, &statbuf, FTW_NS));
if (S_ISDIR(statbuf.st_mode) == 0) /* not a directory */
return(func(fullpath, &statbuf, FTW_F));
/*
* It's a directory. First call func() for the directory,
* then process each filename in the directory.
*/
if ((ret = func(fullpath, &statbuf, FTW_D)) != 0)
return(ret);
n = strlen(fullpath);
if (n + NAME_MAX + 2 > pathlen) { /* expand path buffer */
pathlen *= 2;
if ((fullpath = realloc(fullpath, pathlen)) == NULL)
exit(0);
}
fullpath[n++] = '/';
fullpath[n] = 0;
if ((dp = opendir(fullpath)) == NULL) /* can't read directory */
return(func(fullpath, &statbuf, FTW_DNR));
while ((dirp = readdir(dp)) != NULL) {
if (strcmp(dirp->d_name, ".") == 0 ||
strcmp(dirp->d_name, "..") == 0)
continue; /* ignore dot and dot-dot */
strcpy(&fullpath[n], dirp->d_name); /* append name after "/" */
if ((ret = dopath(func)) != 0) /* recursive */
break; /* time to leave */
}
fullpath[n-1] = 0; /* erase everything from slash onward */
if (closedir(dp) < 0)
exit(0);
return(ret);
}
static int
myfunc(const char *pathname, const struct stat *statptr, int type)
{
switch (type) {
case FTW_F:
switch (statptr->st_mode & S_IFMT) {
case S_IFREG: nreg++; break;
case S_IFBLK: nblk++; break;
case S_IFCHR: nchr++; break;
case S_IFIFO: nfifo++; break;
case S_IFLNK: nslink++; break;
case S_IFSOCK: nsock++; break;
case S_IFDIR: /* directories should have type = FTW_D */
exit(0);
}
break;
case FTW_D:
ndir++;
break;
case FTW_DNR:
break;
case FTW_NS:
break;
default:
break;
}
return(0);
}