• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

test


Commit MetaInfo

修订版ecd57f01e73ff374cda38b1627c34b345c2bb010 (tree)
时间2009-09-02 01:40:30
作者TATEISHI Katsuyuki <kt@whee...>
CommiterTATEISHI Katsuyuki

Log Message

Add new program 'from'.

The from is a program to print your mailbox summary.

更改概述

差异

--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,7 +1,9 @@
11 INCLUDES = -I$(top_srcdir)/lib
22 LDADD = $(top_builddir)/lib/libsubr.a
33
4-bin_PROGRAMS = hello localtime
4+bin_PROGRAMS = from hello localtime
5+
6+from_SOURCES = from.c
57
68 hello_SOURCES = hello.c
79
--- /dev/null
+++ b/src/from.c
@@ -0,0 +1,104 @@
1+#include <stdio.h>
2+#include <stdlib.h>
3+#include <string.h>
4+#include <unistd.h>
5+#include <err.h>
6+
7+#include "subr.h"
8+#include "config.h"
9+
10+const char *from_mark = "From ";
11+
12+void search_mail(FILE *mbox, const char *sender, int iscount);
13+void usage();
14+
15+int
16+main(int argc, char **argv) {
17+ char *path_mbox, *sender;
18+ FILE *mbox;
19+ char ch;
20+ int count_mode = 0;
21+
22+ path_mbox = sender = NULL;
23+
24+ setprogname(argv[0]);
25+
26+ while ((ch = getopt(argc, argv, "cf:s:")) != -1) {
27+ switch (ch) {
28+ case 'c':
29+ count_mode = 1;
30+ break;
31+ case 'f':
32+ path_mbox = optarg;
33+ break;
34+ case 's':
35+ sender = optarg;
36+ break;
37+ default:
38+ usage();
39+ return 1;
40+ }
41+ }
42+ argc -= optind;
43+ argv += optind;
44+
45+ if (path_mbox == NULL) {
46+ path_mbox = getenv("MAIL");
47+ mbox = fopen(path_mbox, "r");
48+ } else if (strcmp(path_mbox, "-") == 0) {
49+ mbox = stdin;
50+ } else {
51+ mbox = fopen(path_mbox, "r");
52+ }
53+
54+ if (mbox == NULL)
55+ err(1, "fopen():", getprogname());
56+
57+ search_mail(mbox, sender, count_mode);
58+
59+ return 0;
60+}
61+
62+void search_mail(FILE *mbox, const char *sender, int iscount)
63+{
64+ char str[BUFSIZ], *ret;
65+ int flag = 1;
66+ int nmails = 0;
67+
68+ while((ret = fgets(str, BUFSIZ, mbox)) != NULL) {
69+ if (strcmp(str, "\n") == 0) {
70+ flag = 1;
71+ continue;
72+ }
73+
74+ if (flag && strncmp(str, from_mark, strlen(from_mark)) == 0) {
75+ flag = 0;
76+ if (sender && (strpbrk(sender, str) == NULL)) {
77+ continue;
78+ }
79+
80+ if (iscount)
81+ nmails++;
82+ else
83+ printf("%s", str);
84+ }
85+ }
86+
87+ if (feof(mbox)) {
88+ if (iscount) {
89+ printf("There %s %d message%s in your"
90+ " incoming mailbox.\n",
91+ (nmails == 1) ? "is" : "are",
92+ nmails,
93+ (nmails == 1) ? "" : "s");
94+ }
95+ } else {
96+ err(1, "fgets()");
97+ }
98+
99+}
100+
101+void
102+usage() {
103+ fprintf(stderr, "Usage: %s [-c] [-f file] [-s sender] [user]\n", getprogname());
104+}