aboutsummaryrefslogtreecommitdiff
path: root/etc/flip.c
blob: 63bacdf254d9811c42f067e4f1e4b270f61ad4b4 (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
/*
 *  flip -- put last argument after first argument
 *  $ cc -O2 -o flip flip.c
 */

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	int i;

	if(argc-1 == 0){
		fprintf(stderr, "usage: %s command [arg ...]\n", argv[0]);
		return 1;
	}

	/* Move last argument after first argument. */
	if(argc-1 > 2){
		for(i = argc; i >= 3; i--)
			argv[i] = argv[i-1];
		argv[2] = strdup(argv[argc]);
		argv[argc] = NULL;
	}

	execvp(argv[1], argv+1);
	err(1, "%s", argv[1]);
}