From 335e2ce30616f0f2a59b2ad908261053d26f7aad Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Wed, 23 Jan 2013 14:17:52 +0100 Subject: [PATCH 1/1] Initial commit --- Makefile | 54 ++++++++++++++++++++ mymail.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+) create mode 100644 Makefile create mode 100644 mymail.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6fdb732 --- /dev/null +++ b/Makefile @@ -0,0 +1,54 @@ + +# +# Copyright (c) 2013 Francois Fleuret +# Written by Francois Fleuret +# +# This file is part of mymail. +# +# mymail is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3 as +# published by the Free Software Foundation. +# +# mymail is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with mymail. If not, see . +# + +BINARY_PATH = $(DESTDIR)/usr/bin +MAN_PATH = $(DESTDIR)/usr/share/man/man1 + +LDFLAGS=-lcurses + +UNAME=\"$(shell uname -srmn)\" + +ifeq ($(DEBUG),yes) + OPTIMIZE_FLAG = -ggdb3 -DDEBUG -fno-omit-frame-pointer +else + OPTIMIZE_FLAG = -O2 +endif + +CFLAGS = -Wall -Wextra -ansi -pedantic -DUNAME="$(UNAME)" $(OPTIMIZE_FLAG) + +all: mymail + +mymail: mymail.o + $(CC) -o $@ $^ $(LDFLAGS) + +install: mymail + mkdir -p $(BINARY_PATH) $(MAN_PATH) + install -m 755 mymail $(BINARY_PATH) + install -m 755 bash-mymail.sh $(BINARY_PATH) + install -m 644 mymail.1 $(MAN_PATH) + gzip $(MAN_PATH)/mymail.1 + +uninstall: + rm $(BINARY_PATH)/mymail + rm $(BINARY_PATH)/bash-mymail.sh + rm $(MAN_PATH)/mymail.1.gz + +clean: + rm -f *.o mymail diff --git a/mymail.c b/mymail.c new file mode 100644 index 0000000..241d635 --- /dev/null +++ b/mymail.c @@ -0,0 +1,147 @@ + +/* + * Copyright (c) 2013 Francois Fleuret + * Written by Francois Fleuret + * + * This file is part of mymail. + * + * mymail is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * mymail is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with mymail. If not, see . + * + */ + +/* + + To use it as a super-history-search for bash: + mymail --bash <(history) + +*/ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define VERSION "1.1.7" + +#define BUFFER_SIZE 16384 + +/********************************************************************/ + +/* malloc with error checking. */ + +void *safe_malloc(size_t n) { + void *p = malloc(n); + if(!p && n != 0) { + fprintf(stderr, + "mymail: can not allocate memory: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + return p; +} + +/*********************************************************************/ + +void usage(FILE *out) { + + fprintf(out, "mymail version %s (%s)\n", VERSION, UNAME); + fprintf(out, "Written by Francois Fleuret .\n"); + fprintf(out, "\n"); + fprintf(out, "Usage: mymail [options] [ [ ...]]\n"); + fprintf(out, "\n"); +} + +void read_file(const char *input_filename, + int nb_lines_max, int *nb_lines) { + + char raw_line[BUFFER_SIZE]; + char *s; + FILE *file; + int l; + + file = fopen(input_filename, "r"); + + if(!file) { + fprintf(stderr, "mymail: Can not open `%s'.\n", input_filename); + exit(EXIT_FAILURE); + } + + while(*nb_lines < nb_lines_max && fgets(raw_line, BUFFER_SIZE, file)) { + l = strlen(raw_line); + fgets(raw_line + l, BUFFER_SIZE - l, file); + for(s = raw_line + strlen(raw_line) - 1; s > raw_line && *s == '\n'; s--) { + *s = '\0'; + } + /* store_line(hash_table, raw_line, nb_lines, lines); */ + } + + fclose(file); +} + +/*********************************************************************/ + +/* For long options that have no equivalent short option, use a + non-character as a pseudo short option, starting with CHAR_MAX + 1. */ +enum +{ + OPT_BASH_MODE = CHAR_MAX + 1 +}; + +static struct option long_options[] = { + { "help", no_argument, 0, 'h' }, + { 0, 0, 0, 0 } +}; + +int main(int argc, char **argv) { + int error = 0, show_help = 0; + char c; + + setlocale(LC_ALL, ""); + + while ((c = getopt_long(argc, argv, "o:s:x:vwmqf:ibzdeajyunt:r:l:c:-h", + long_options, NULL)) != -1) { + + switch(c) { + + case 'h': + show_help = 1; + break; + + default: + error = 1; + break; + } + } + + if(error) { + usage(stderr); + exit(EXIT_FAILURE); + } + + if(show_help) { + usage(stdout); + exit(EXIT_SUCCESS); + } + + exit(EXIT_SUCCESS); +} -- 2.39.5