Add support for tab-completion when selecting by rule
[alpine.git] / alpine / pine-use.c
blob75967d91be5abdef6743cbeaff1d58f1cf0ea406
1 /*
2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <pwd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
22 #ifndef MAILSPOOLPCTS
23 #define MAILSPOOLPCTS "/usr/spool/mail/%s"
24 /* #define MAILSPOOLPCTS "/usr/mail/%s" */
25 #endif
27 #define DAYSEC (60*60*24)
29 main(argc, argv)
30 int argc;
31 char **argv;
33 struct passwd *pw;
34 char filename[100], buf[100], *p;
35 struct stat statb;
36 long now, inbox_mess, inboxes, inbox_mess_max;
37 int core_files, c, core_id, count, sig_files;
38 int user_count[6], so_far;
39 FILE *f, *core;
41 user_count[0] = 0; /* Last week */
42 user_count[1] = 0; /* Last 2 weeks */
43 user_count[2] = 0; /* Last month */
44 user_count[3] = 0; /* Last year */
45 user_count[4] = 0; /* Ever */
46 sig_files = 0;
47 core_files = 0;
48 inboxes = 0;
49 inbox_mess = 0;
50 inbox_mess_max = 0;
52 now = time(0);
53 core = NULL;
55 if(argc > 1) {
56 core_id = atoi(argv[1]);
57 if(core_id == 0){
58 fprintf(stderr, "Bogus core starting number\n");
59 exit(-1);
60 } else {
61 printf("Core collect starting at %d\n", core_id);
62 core = fopen("pine-core-collect.sh", "w");
66 so_far = 0;
67 while((pw = getpwent()) != NULL) {
68 so_far++;
69 if((so_far % 200) == 0) {
70 printf("%5d users processed so far\n", so_far);
73 if(pw->pw_dir && *pw->pw_dir == '/' && pw->pw_dir[1] == '\0')
74 continue;
76 snprintf(filename, sizeof(filename), "%s/.pinerc", pw->pw_dir);
77 if(stat(filename, &statb) < 0)
78 continue;
79 if(statb.st_mtime + 7 * DAYSEC > now)
80 user_count[0]++;
81 else if(statb.st_mtime + 14 * DAYSEC > now)
82 user_count[1]++;
83 else if(statb.st_mtime + 30 * DAYSEC > now)
84 user_count[2]++;
85 else if(statb.st_mtime + 365 * DAYSEC > now)
86 user_count[3]++;
87 else
88 user_count[4]++;
91 if(statb.st_mtime + 30 * DAYSEC >= now) {
92 count = mail_file_size(pw->pw_name);
93 if(count >= 0){
94 inboxes++;
95 inbox_mess += count;
96 inbox_mess_max = inbox_mess_max > count ? inbox_mess_max:count;
100 snprintf(filename, sizeof(filename), "%s/.signature", pw->pw_dir);
101 if(access(filename, 0) == 0)
102 sig_files++;
104 snprintf(filename, sizeof(filename), "%s/core", pw->pw_dir);
105 if((f = fopen(filename, "r")) != NULL) {
106 fflush(stdout);
107 while((c = getc(f)) != EOF) {
108 if(c == 'P'){
109 p = buf;
110 *p++ = c;
111 while((c = getc(f)) != EOF) {
112 *p++ = c;
113 if(p > &buf[50]) {
114 break;
116 if(c == ')') {
117 break;
120 *p = '\0';
121 if(c == EOF)
122 break;
123 if(strncmp(&buf[strlen(buf) - 13], "(olivebranch)", 13) == 0) {
124 printf("%s\t%s\n", filename, buf + 14);
125 core_files++;
126 if(core != NULL) {
127 fprintf(core, "mv %s core%d.%s\n", filename,
128 core_id++,pw->pw_name);
130 break;
134 fclose(f);
135 } else {
136 /* printf("%s\n", pw->pw_name); */
141 printf("%5d: last week\n", user_count[0]);
142 printf("%5d: last two weeks (+%d)\n", user_count[1] + user_count[0],
143 user_count[1]);
144 printf("%5d: last month (+%d)\n", user_count[2] + user_count[1] + user_count[0], user_count[2]);
145 printf("%5d: last year\n", user_count[3]);
146 printf("%5d: more than a year\n", user_count[4]);
147 printf("%5d: core files\n", core_files);
148 printf("%5ld: Average messages in inbox (%ld/%ld)\n",
149 inbox_mess/(inboxes ? inboxes : 1), inbox_mess, inboxes);
150 printf("%5d: Largest inbox in messages\n", inbox_mess_max);
151 printf("%5d: Total users checked\n", so_far);
152 printf("%5d: signature files\n", sig_files);
156 mail_file_size(user)
157 char *user;
159 int count = 0;
160 FILE *f;
161 char buf[20480];
163 snprintf(buf, sizeof(buf), MAILSPOOLPCTS, user);
165 f = fopen(buf, "r");
166 if(f == NULL)
167 return(-1);
169 while(fgets(buf, sizeof(buf), f) != NULL) {
170 if(strncmp(buf, "From ", 5) == 0)
171 count++;
173 fclose(f);
174 /* printf("%s %d\n", user, count); */
175 return(count);