30
30
#include <signal.h>
31
31
#include <stdio.h>
32
32
33
- #define error (status , errno , fmt , ...) do { \
34
- if (!errno) \
35
- fprintf (stderr, "crun: " fmt, ##__VA_ARGS__); \
36
- else \
37
- fprintf (stderr, "crun: %s:" fmt, strerror (errno), ##__VA_ARGS__); \
38
- if (status) \
39
- exit (status); \
40
- } while(0)
33
+ #define error (status , errno , fmt , ...) \
34
+ do \
35
+ { \
36
+ if (! errno) \
37
+ fprintf (stderr, "crun: " fmt, ##__VA_ARGS__); \
38
+ else \
39
+ fprintf (stderr, "crun: %s:" fmt, strerror (errno), ##__VA_ARGS__); \
40
+ if (status) \
41
+ exit (status); \
42
+ } while (0)
41
43
42
44
struct termios tset ;
43
45
int fd ;
@@ -52,7 +54,7 @@ open_unix_domain_socket (const char *path)
52
54
error (EXIT_FAILURE , errno , "error creating UNIX socket" );
53
55
if (strlen (path ) >= sizeof (addr .sun_path ))
54
56
error (EXIT_FAILURE , 0 , "invalid path" );
55
-
57
+
56
58
strcpy (addr .sun_path , path );
57
59
addr .sun_family = AF_UNIX ;
58
60
ret = bind (fd , (struct sockaddr * ) & addr , sizeof (addr ));
@@ -112,16 +114,16 @@ void
112
114
sigint_handler (int s )
113
115
{
114
116
const char ctrlc = 3 ;
115
- write (fd , & ctrlc , 1 );
117
+ write (fd , & ctrlc , 1 );
116
118
}
117
119
118
120
void
119
- register_handler (struct sigaction * handler )
121
+ register_handler (struct sigaction * handler )
120
122
{
121
123
handler -> sa_handler = sigint_handler ;
122
- sigemptyset (& handler -> sa_mask );
124
+ sigemptyset (& handler -> sa_mask );
123
125
handler -> sa_flags = 0 ;
124
- sigaction (SIGINT , handler , NULL );
126
+ sigaction (SIGINT , handler , NULL );
125
127
}
126
128
127
129
int
@@ -135,7 +137,7 @@ main (int argc, char **argv)
135
137
136
138
unlink (argv [1 ]);
137
139
138
- register_handler (& ctrl_c_handler );
140
+ register_handler (& ctrl_c_handler );
139
141
140
142
socket = open_unix_domain_socket (argv [1 ]);
141
143
while (1 )
@@ -144,7 +146,7 @@ main (int argc, char **argv)
144
146
int stdin_flags , term_flags ;
145
147
int data ;
146
148
147
- printf ("Press 'Ctrl \\' to exit.\nWaiting for connection ...\n" );
149
+ printf ("Press 'Ctrl \\' to exit.\nWaiting for connection ...\n" );
148
150
do
149
151
conn = accept (socket , NULL , NULL );
150
152
while (conn < 0 && errno == EINTR );
@@ -158,7 +160,7 @@ main (int argc, char **argv)
158
160
continue ;
159
161
}
160
162
161
- if (tcgetattr (fd , & tset ) == -1 )
163
+ if (tcgetattr (fd , & tset ) == -1 )
162
164
error (0 , errno , "failed to get console terminal settings" );
163
165
164
166
tset .c_oflag |= ONLCR ;
@@ -167,19 +169,19 @@ main (int argc, char **argv)
167
169
if (tcsetattr (fd , TCSANOW , & tset ) == -1 )
168
170
error (0 , errno , "failed to set console terminal settings" );
169
171
170
- stdin_flags = fcntl (STDIN_FILENO , F_GETFL );
172
+ stdin_flags = fcntl (STDIN_FILENO , F_GETFL );
171
173
if (stdin_flags == -1 )
172
174
error (EXIT_FAILURE , errno , "failed to obtain STDIN flags" );
173
175
174
- ret = fcntl (STDIN_FILENO , F_SETFL , stdin_flags | O_NONBLOCK );
176
+ ret = fcntl (STDIN_FILENO , F_SETFL , stdin_flags | O_NONBLOCK );
175
177
if (ret == -1 )
176
178
error (EXIT_FAILURE , errno , "failed to set STDIN to non-blocking" );
177
179
178
- term_flags = fcntl (fd , F_GETFL );
180
+ term_flags = fcntl (fd , F_GETFL );
179
181
if (term_flags == -1 )
180
182
error (EXIT_FAILURE , errno , "failed to obtain terminal flags" );
181
183
182
- ret = fcntl (fd , F_SETFL , term_flags | O_NONBLOCK );
184
+ ret = fcntl (fd , F_SETFL , term_flags | O_NONBLOCK );
183
185
if (ret == -1 )
184
186
error (EXIT_FAILURE , errno , "failed to set terminal to non-blocking" );
185
187
@@ -200,7 +202,7 @@ main (int argc, char **argv)
200
202
data = 1 ;
201
203
}
202
204
203
- ret = read (STDIN_FILENO , buf , sizeof (buf ));
205
+ ret = read (STDIN_FILENO , buf , sizeof (buf ));
204
206
if (ret > 0 )
205
207
{
206
208
ret = write (fd , buf , ret );
@@ -211,13 +213,14 @@ main (int argc, char **argv)
211
213
}
212
214
data = 1 ;
213
215
}
214
- if (!data ) usleep (10000 );
216
+ if (! data )
217
+ usleep (10000 );
215
218
}
216
219
close (conn );
217
- ret = fcntl (STDIN_FILENO , F_SETFL , stdin_flags );
220
+ ret = fcntl (STDIN_FILENO , F_SETFL , stdin_flags );
218
221
if (ret == -1 )
219
222
error (EXIT_FAILURE , errno , "failed to reset STDIN to original setting" );
220
- ret = fcntl (fd , F_SETFL , term_flags );
223
+ ret = fcntl (fd , F_SETFL , term_flags );
221
224
if (ret == -1 )
222
225
error (EXIT_FAILURE , errno , "failed to reset terminal to original setting" );
223
226
}
0 commit comments