summaryrefslogtreecommitdiff
path: root/greet.c
blob: 9d524aee2e7949e159f6bd6639de203230cd70a9 (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
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

	people = NULL;
	size = 0;
	written = 0;

args:	if(argc==1){
		fprintf(stderr, "usage: %s person ...\n", argv[0]);
		return 1;
	}
	else if(argc==2)
		people = argv[1];
	else
		for(i = 1; i<argc; i++){
			if(written+strlen(argv[i])>size){
				size += written+strlen(argv[i])+20;
				people = realloc(people, size+1);
				if(people==NULL) err(1, "realloc");
			}
concat:			if(i==argc-1) strcat(people, " and ");
			else if(i>1) strcat(people, ", ");
			strcat(people, argv[i]);
		}

end:	printf("Hello %s!\n", people);
}