-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt.diff
94 lines (84 loc) · 2.18 KB
/
getopt.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
From 1bc6fab60b08ae9f26df39ce135d4a0004675415 Mon Sep 17 00:00:00 2001
From: Cristian Canedo <[email protected]>
Date: Sat, 14 Oct 2023 12:06:02 -0500
Subject: [PATCH] getopt
---
uhm.c | 45 ++++++++++++++++++++++++++++++++++-----------
1 file changed, 34 insertions(+), 11 deletions(-)
diff --git a/uhm.c b/uhm.c
index 53413a3..9b31a6d 100644
--- a/uhm.c
+++ b/uhm.c
@@ -1,14 +1,13 @@
-#include <time.h>
-#include <unistd.h>
-#include <signal.h>
#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
-static char *mbox = "/var/mail/user";
-static char *aud = "/mnt/aud/sounds/mail.wav";
-
-static void play(void)
+static void play(char *aud)
{
char *cmd[] = { "aucat", "-i", aud, NULL };
@@ -21,28 +20,52 @@ static void play(void)
wait(NULL);
}
-int main(void)
+static void usage(void)
{
+ fprintf(stderr, "usage: uhm [-i delay] -f file -m mailbox\n");
+ exit(1);
+}
+
+int main(int argc, char *argv[])
+{
+ int opt, mflag = 0, fflag = 0, ms = 100;
+ char *mbox = NULL, *aud = NULL;
struct stat st1, st2;
- struct timespec delay = { 0, 100000000 };
+ struct timespec delay = { 0, ms * 1000000 };
+ while ((opt = getopt(argc, argv, "i:f:m:")) != -1) {
+ switch (opt) {
+ case 'i': ms = atoi(optarg); break;
+ case 'f': aud = optarg; fflag = 1; break;
+ case 'm': mbox = optarg; mflag = 1; break;
+ default: usage(); break;
+ }
+ }
+
+ if (!mflag || !fflag)
+ usage();
if (access(mbox, F_OK) == -1)
err(1, "%s", mbox);
if (access(aud, F_OK) == -1)
err(1, "%s", aud);
+ if (ms == 0)
+ ms = 100;
signal(SIGCHLD, SIG_IGN);
+ delay.tv_sec = ms / 1000;
+ delay.tv_nsec = (ms % 1000) * 1000000;
+
if (stat(mbox, &st1) == -1)
err(1, "%s", mbox);
if (st1.st_size > 0)
- play();
+ play(aud);
for (;;) {
if (stat(mbox, &st2) == -1)
err(1, "%s", mbox);
if (st2.st_size > st1.st_size)
- play();
+ play(aud);
st1 = st2;
nanosleep(&delay, NULL);
}
--
2.37.3