aboutsummaryrefslogtreecommitdiff
path: root/rpt.c
blob: d6476adefe161dc86221a45ebd265b3bc12baff6 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#define _GNU_SOURCE		/* getopt_long */

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

#define VERSION "1.1.3"

/* Size of the buffer used to clone stdin. */
#define BUFSIZE 8000

/* Exit status */
#define EXIT_SUCCESS 0
#define EXIT_INVALID_ARG 1
#define EXIT_INVALID_COUNT 2
#define EXIT_NO_COMMAND 3
#define EXIT_OTHER 4

/* Clone of stdin.  Used to perform lseek(2) calls between each child
   process. */
FILE *stdin_clone = NULL;

/* File descriptor associated to the clone of stdin. */
int clone_fd;

void
print_usage(FILE *output)
{
	fprintf(output, "Usage:\n");
	fprintf(output, "rpt [-n | --count COUNT] [-f | --force] COMMAND\n");
	fprintf(output, "rpt [-h | --help]\n");
	fprintf(output, "rpt [-V | --version]\n");
}

void
print_help()
{
	puts("Repeat : repeat a shell command");
	puts("");
	print_usage(stdout);
}

void
print_version()
{
	puts(VERSION);
}

/* Exit whenever a subprocess fails. */
void
exit_on_error(int status)
{
	if (!status)
		return;

	fprintf(stderr, "rpt: child process exited with status %d, aborting\n",
		status);

	if (stdin_clone)
		fclose(stdin_clone);

	exit(status);
}

/* Ignore subprocesses' errors. */
void
continue_on_error(int status)
{
	(void) status;
}

void
exec_child(char **argv)
{
	int status;

	if (stdin_clone)
	{
		status = lseek(clone_fd, 0, L_SET);

		if (status < 0)
		{
			perror("lseek");
			exit(EXIT_OTHER);
		}
	}

	status = dup2(clone_fd, STDIN_FILENO);

	if (status < 0)
	{
		perror("dup2");
		exit(EXIT_OTHER);
	}

	status = execvp(argv[0], argv);

	if (status < 0)
	{
		perror("execvp");
		exit(EXIT_OTHER);
	}

	exit(EXIT_OTHER);
}

void
wait_child(void (*handle_error_f)(int status))
{
	int wstatus;

	wait(&wstatus);

	handle_error_f(WEXITSTATUS(wstatus));
}

void
invoke_cmd(char **argv, void (*handle_error_f)(int status))
{
	pid_t pid = fork();

	if (pid < 0)
	{
		perror("fork");
		exit(EXIT_OTHER);
	}

	if (pid == 0)
		exec_child(argv);
	else
		wait_child(handle_error_f);
}

/* Invoke the executable *ARGV COUNT times, passing it the rest of ARGV as
   arguments. Call HANDLE_ERROR_F between each process. */
void
repeat_cmd(char **argv, long count, void (*handle_error_f)(int status))
{
	for(long i = 0; i < count; ++i)
		invoke_cmd(argv, handle_error_f);
}

void
copy_stdin()
{
	ssize_t nbytes;
	char buf[BUFSIZE];

	stdin_clone = tmpfile(); /* closed at the end of the main */

	if (!stdin_clone)
	{
		perror("tmpfile");
		exit(EXIT_OTHER);
	}

	clone_fd = fileno(stdin_clone);

	while ((nbytes = read(STDIN_FILENO, buf, BUFSIZE)) > 0)
	{
		if (write(clone_fd, buf, nbytes) < 0)
			break;
	}

	if (errno)
	{
		fclose(stdin_clone);
		perror("read/write");
		exit(EXIT_OTHER);
	}
}

int
main(int argc, char* argv[])
{
	struct option const opts[] = {
		{"count", required_argument, NULL, 'n'},
		{"force", no_argument, NULL, 'f'},
		{"version", no_argument, NULL, 'V'},
		{"help", no_argument, NULL, 'h'},
		{NULL, 0, NULL, 0}
	};

	opterr = 0;		/* dismiss getopt error message */
	int opt;

	char *strcount = NULL;
	long count = 1;

	void (*handle_error_f)(int status) = exit_on_error;

	while ((opt = getopt_long(argc, argv, "+:n:fVh", opts, NULL)) >= 0)
	{
		switch(opt)
		{
		case 'n':
			strcount = optarg;
			break;
		case 'f':
			handle_error_f = continue_on_error;
			break;
		case 'h':
			print_help();
			exit(EXIT_SUCCESS);
		case 'V':
			print_version();
			exit(EXIT_SUCCESS);
		case ':':
			fprintf(stderr, "rpt: COUNT value missing\n");
			print_usage(stderr);
			exit(EXIT_INVALID_ARG);
		default:
			print_usage(stderr);
			exit(EXIT_INVALID_ARG);
		}
	}

	if (strcount)
	{
		char *err = NULL;
		count = strtol(strcount, &err, 10);

		if (err && (*err != '\0'))
		{
			fprintf(stderr, "rpt: failed to convert COUNT : '%s'\n",
				strcount);
			exit(EXIT_INVALID_COUNT);
		}
	}

	if (optind >= argc)
	{
		fprintf(stderr, "rpt: no command provided\n");
		return 3;
	}

	struct pollfd pollfd = {.fd=STDIN_FILENO, .events=POLLIN, .revents=0};

	if (poll(&pollfd, 1, 0) < 0)
	{
		perror("poll");
		exit(EXIT_OTHER);
	}

	if (pollfd.revents & POLLIN)
		copy_stdin();

	repeat_cmd(argv + optind, count, handle_error_f);

	if (stdin_clone)
		fclose(stdin_clone);

	return EXIT_SUCCESS;
}