3 * Copyright (c) 2013 Francois Fleuret
4 * Written by Francois Fleuret <francois@fleuret.org>
6 * This file is part of mymail.
8 * mymail is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3 as
10 * published by the Free Software Foundation.
12 * mymail is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with mymail. If not, see <http://www.gnu.org/licenses/>.
24 To use it as a super-history-search for bash:
25 mymail --bash <(history)
43 #define BUFFER_SIZE 16384
45 /********************************************************************/
47 /* malloc with error checking. */
49 void *safe_malloc(size_t n) {
53 "mymail: can not allocate memory: %s\n", strerror(errno));
59 /*********************************************************************/
61 void usage(FILE *out) {
62 fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME);
63 fprintf(out, "Written by Francois Fleuret <francois@fleuret.org>.\n");
65 fprintf(out, "Usage: mymail [options] [<filename1> [<filename2> ...]]\n");
69 void read_file(const char *input_filename) {
71 char raw_line[BUFFER_SIZE];
76 file = fopen(input_filename, "r");
79 fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename);
85 while(fgets(raw_line, BUFFER_SIZE, file)) {
86 if(strncmp(raw_line, "From ", 5) == 0) {
88 fprintf(stderr, "Got a 'From ' in the header.\n");
92 } else if(strncmp(raw_line, "\n", 1) == 0) {
93 if(in_header) { in_header = 0; }
97 printf("LINE.H %s", raw_line);
99 printf("LINE.B %s", raw_line);
106 int ignore_entry(const char *name) {
108 strcmp(name, ".") == 0 ||
109 strcmp(name, "..") == 0 ||
110 (name[0] == '.' && name[1] != '/');
113 void process_dir(const char *dir_name) {
115 struct dirent *dir_e;
117 char subname[PATH_MAX + 1];
119 if(lstat(dir_name, &sb) != 0) {
120 fprintf(stderr, "mymail: Can not stat \"%s\": %s\n", dir_name, strerror(errno));
125 if(S_ISLNK(sb.st_mode)) {
129 dir = opendir(dir_name);
132 printf("Processing directory '%s'.\n", dir_name);
133 while((dir_e = readdir(dir))) {
134 if(!ignore_entry(dir_e->d_name)) {
135 snprintf(subname, PATH_MAX, "%s/%s", dir_name, dir_e->d_name);
136 process_dir(subname);
141 if(S_ISREG(sb.st_mode)) {
142 printf("Processing regular file '%s'.\n", dir_name);
148 /*********************************************************************/
150 /* For long options that have no equivalent short option, use a
151 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
154 OPT_BASH_MODE = CHAR_MAX + 1
157 static struct option long_options[] = {
158 { "help", no_argument, 0, 'h' },
162 int main(int argc, char **argv) {
163 int error = 0, show_help = 0;
166 setlocale(LC_ALL, "");
168 while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeajyunt:r:l:c:-h",
169 long_options, NULL)) != -1) {
193 while(optind < argc) {
194 process_dir(argv[optind]);