diff options
author | John Ankarstr\xf6m <john@ankarstrom.se> | 2021-06-02 12:57:24 +0200 |
---|---|---|
committer | John Ankarstr\xf6m <john@ankarstrom.se> | 2021-06-02 14:14:13 +0200 |
commit | b61fcab3baf91ecd4b2ad4185e591cf339ad2b34 (patch) | |
tree | 9091676b4b07be854408b31e0be1b6d5eb5028e6 /greet.c | |
download | ref-b61fcab3baf91ecd4b2ad4185e591cf339ad2b34.tar.gz |
First commit
Diffstat (limited to 'greet.c')
-rw-r--r-- | greet.c | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,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); +} |