summaryrefslogtreecommitdiff
path: root/greet.c
diff options
context:
space:
mode:
authorJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-02 12:57:24 +0200
committerJohn Ankarstr\xf6m <john@ankarstrom.se>2021-06-02 14:14:13 +0200
commitb61fcab3baf91ecd4b2ad4185e591cf339ad2b34 (patch)
tree9091676b4b07be854408b31e0be1b6d5eb5028e6 /greet.c
downloadref-b61fcab3baf91ecd4b2ad4185e591cf339ad2b34.tar.gz
First commit
Diffstat (limited to 'greet.c')
-rw-r--r--greet.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/greet.c b/greet.c
new file mode 100644
index 0000000..9d524ae
--- /dev/null
+++ b/greet.c
@@ -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);
+}