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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use ;
static INIT: Once = new;
/// Initializes the SIGINT (Ctrl-C) signal handler.
///
/// This function installs a minimal, signal-safe handler for `SIGINT`.
/// Once installed, any incoming Ctrl-C will set an internal flag,
/// which can later be queried via [`is_ctrlc_received()`].
///
/// This function may be called multiple times;
/// the signal handler will only be installed once.
/// Repeated calls are safe and have no additional effect.
///
/// # Note
///
/// Use either this function OR [`init_ctrlc_with_print()`], not both.
///
/// # Errors
///
/// Returns an `Err` if the underlying system call (`sigaction`)
/// fails during handler installation. This typically indicates a
/// low-level OS error or permission issue.
///
/// # Examples
///
/// ```rust,no_run
/// ctrlc_tiny::init_ctrlc()?;
/// while !ctrlc_tiny::is_ctrlc_received() {
/// // Do work here
/// }
/// # Ok::<_, std::io::Error>(())
/// ```
/// Initializes the SIGINT (Ctrl-C) signal handler with a custom message.
///
/// This function installs a minimal, signal-safe handler for `SIGINT`.
/// Once installed, any incoming Ctrl-C will set an internal flag and
/// print the specified message to stderr.
/// The flag can later be queried via [`is_ctrlc_received()`].
///
/// This function may be called multiple times;
/// the signal handler will only be installed once.
/// Repeated calls are safe and have no additional effect.
///
/// # Note
///
/// Use either this function OR [`init_ctrlc()`], not both.
///
/// # Arguments
///
/// * `message` - The message to print to stderr when Ctrl-C is pressed
///
/// # Errors
///
/// Returns an `Err` if the underlying system call (`sigaction`)
/// fails during handler installation. This typically indicates a
/// low-level OS error or permission issue.
///
/// # Examples
///
/// ```rust,no_run
/// ctrlc_tiny::init_ctrlc_with_print("Ctrl+C pressed\n")?;
/// while !ctrlc_tiny::is_ctrlc_received() {
/// // Do work here
/// }
/// # Ok::<_, std::io::Error>(())
/// ```
/// Checks whether Ctrl-C (SIGINT) has been received.
///
/// Returns `true` if a `SIGINT` signal (typically from Ctrl-C)
/// has been delivered since [`init_ctrlc()`] was called.
///
/// Once set, the flag remains `true` for the lifetime of the process.
///
/// This function is safe to call from any thread at any time
/// after initialization.
///
/// # Examples
///
/// ```rust,no_run
/// ctrlc_tiny::init_ctrlc_with_print("Ctrl+C pressed\n")?;
/// while !ctrlc_tiny::is_ctrlc_received() {
/// // Do work here
/// }
/// # Ok::<_, std::io::Error>(())
/// ```
/// Resets the internal Ctrl-C received flag to `false`.
///
/// This can be useful if you want to detect multiple Ctrl-C presses
/// independently (e.g. "exit on second Ctrl-C").
///
/// # Safety
///
/// Internally, this clears a `sig_atomic_t` flag that may be concurrently
/// modified by the signal handler. This is safe but may cause a signal
/// received during the reset to be missed.
///
/// # Examples
///
/// ```rust,no_run
/// ctrlc_tiny::init_ctrlc_with_print("Ctrl+C pressed\n")?;
/// let mut count = 0;
/// loop {
/// if ctrlc_tiny::is_ctrlc_received() {
/// ctrlc_tiny::reset_ctrlc_received();
/// count += 1;
/// if count == 2 {
/// break;
/// }
/// }
/// }
/// # Ok::<_, std::io::Error>(())
/// ```