* Fix an error in compilation when Alpine is not built with S/MIME
[alpine.git] / alpine / pine-use.c
blobab6ee2e602a70850b467b5507b60f93579a191ff
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: pine-use.c 761 2007-10-23 22:35:18Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2006 University of Washington
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * ========================================================================
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <pwd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
25 #ifndef MAILSPOOLPCTS
26 #define MAILSPOOLPCTS "/usr/spool/mail/%s"
27 /* #define MAILSPOOLPCTS "/usr/mail/%s" */
28 #endif
30 #define DAYSEC (60*60*24)
32 main(argc, argv)
33 int argc;
34 char **argv;
36 struct passwd *pw;
37 char filename[100], buf[100], *p;
38 struct stat statb;
39 long now, inbox_mess, inboxes, inbox_mess_max;
40 int core_files, c, core_id, count, sig_files;
41 int user_count[6], so_far;
42 FILE *f, *core;
44 user_count[0] = 0; /* Last week */
45 user_count[1] = 0; /* Last 2 weeks */
46 user_count[2] = 0; /* Last month */
47 user_count[3] = 0; /* Last year */
48 user_count[4] = 0; /* Ever */
49 sig_files = 0;
50 core_files = 0;
51 inboxes = 0;
52 inbox_mess = 0;
53 inbox_mess_max = 0;
55 now = time(0);
56 core = NULL;
58 if(argc > 1) {
59 core_id = atoi(argv[1]);
60 if(core_id == 0){
61 fprintf(stderr, "Bogus core starting number\n");
62 exit(-1);
63 } else {
64 printf("Core collect starting at %d\n", core_id);
65 core = fopen("pine-core-collect.sh", "w");
69 so_far = 0;
70 while((pw = getpwent()) != NULL) {
71 so_far++;
72 if((so_far % 200) == 0) {
73 printf("%5d users processed so far\n", so_far);
76 if(pw->pw_dir && *pw->pw_dir == '/' && pw->pw_dir[1] == '\0')
77 continue;
79 snprintf(filename, sizeof(filename), "%s/.pinerc", pw->pw_dir);
80 if(stat(filename, &statb) < 0)
81 continue;
82 if(statb.st_mtime + 7 * DAYSEC > now)
83 user_count[0]++;
84 else if(statb.st_mtime + 14 * DAYSEC > now)
85 user_count[1]++;
86 else if(statb.st_mtime + 30 * DAYSEC > now)
87 user_count[2]++;
88 else if(statb.st_mtime + 365 * DAYSEC > now)
89 user_count[3]++;
90 else
91 user_count[4]++;
94 if(statb.st_mtime + 30 * DAYSEC >= now) {
95 count = mail_file_size(pw->pw_name);
96 if(count >= 0){
97 inboxes++;
98 inbox_mess += count;
99 inbox_mess_max = inbox_mess_max > count ? inbox_mess_max:count;
103 snprintf(filename, sizeof(filename), "%s/.signature", pw->pw_dir);
104 if(access(filename, 0) == 0)
105 sig_files++;
107 snprintf(filename, sizeof(filename), "%s/core", pw->pw_dir);
108 if((f = fopen(filename, "r")) != NULL) {
109 fflush(stdout);
110 while((c = getc(f)) != EOF) {
111 if(c == 'P'){
112 p = buf;
113 *p++ = c;
114 while((c = getc(f)) != EOF) {
115 *p++ = c;
116 if(p > &buf[50]) {
117 break;
119 if(c == ')') {
120 break;
123 *p = '\0';
124 if(c == EOF)
125 break;
126 if(strncmp(&buf[strlen(buf) - 13], "(olivebranch)", 13) == 0) {
127 printf("%s\t%s\n", filename, buf + 14);
128 core_files++;
129 if(core != NULL) {
130 fprintf(core, "mv %s core%d.%s\n", filename,
131 core_id++,pw->pw_name);
133 break;
137 fclose(f);
138 } else {
139 /* printf("%s\n", pw->pw_name); */
144 printf("%5d: last week\n", user_count[0]);
145 printf("%5d: last two weeks (+%d)\n", user_count[1] + user_count[0],
146 user_count[1]);
147 printf("%5d: last month (+%d)\n", user_count[2] + user_count[1] + user_count[0], user_count[2]);
148 printf("%5d: last year\n", user_count[3]);
149 printf("%5d: more than a year\n", user_count[4]);
150 printf("%5d: core files\n", core_files);
151 printf("%5ld: Average messages in inbox (%ld/%ld)\n",
152 inbox_mess/(inboxes ? inboxes : 1), inbox_mess, inboxes);
153 printf("%5d: Largest inbox in messages\n", inbox_mess_max);
154 printf("%5d: Total users checked\n", so_far);
155 printf("%5d: signature files\n", sig_files);
159 mail_file_size(user)
160 char *user;
162 int count = 0;
163 FILE *f;
164 char buf[20480];
166 snprintf(buf, sizeof(buf), MAILSPOOLPCTS, user);
168 f = fopen(buf, "r");
169 if(f == NULL)
170 return(-1);
172 while(fgets(buf, sizeof(buf), f) != NULL) {
173 if(strncmp(buf, "From ", 5) == 0)
174 count++;
176 fclose(f);
177 /* printf("%s %d\n", user, count); */
178 return(count);