From 680126d7e74cebd399156cd6a7dceabf3684dd40 Mon Sep 17 00:00:00 2001
From: trenary2 <trenary@wmich.edu>
Date: Wed, 18 Oct 2017 06:54:49 -0400
Subject: [PATCH] a3

---
 LowerHalf.c | 16 +++++++++++
 Up.c        | 17 ++++++++++++
 makeargv.c  | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 LowerHalf.c
 create mode 100644 Up.c
 create mode 100644 makeargv.c

diff --git a/LowerHalf.c b/LowerHalf.c
new file mode 100644
index 0000000..dc262c7
--- /dev/null
+++ b/LowerHalf.c
@@ -0,0 +1,16 @@
+#include <stdio.h> // Steven's Figure 15.14 "UpCase" 
+#include "apue.h" // modified to lower the last half 
+int main()
+{  int c;
+
+	while (( c = getchar()) != EOF)
+	{	if ( islower(c))
+			c = toupper(c);
+		if (c >= 'M') c = tolower(c);
+		if (putchar(c) == EOF)
+		   err_sys("output error in upcase\n");
+		if (c == '\n') 
+		   fflush(stdout);
+	}
+	exit(0);
+}
diff --git a/Up.c b/Up.c
new file mode 100644
index 0000000..c8f842d
--- /dev/null
+++ b/Up.c
@@ -0,0 +1,17 @@
+#include <stdio.h> // Steven's Figure 15.14 "UpCase" 
+#include "apue.h"
+#include <ctype.h>
+
+int main()
+{  int c;
+
+	while (( c = getchar()) != EOF)
+	{	if ( islower(c))
+			c = toupper(c);
+		if (putchar(c) == EOF)
+		   err_sys("output error in upcase\n");
+		if (c == '\n') 
+		   fflush(stdout);
+	}
+	exit(0);
+}
diff --git a/makeargv.c b/makeargv.c
new file mode 100644
index 0000000..00a446a
--- /dev/null
+++ b/makeargv.c
@@ -0,0 +1,79 @@
+// makeargv.c 
+// takes a null terminated string s, and a string of delimiters and returns 
+// an array of char * pointers, leaving argvp pointing to that array.
+// each of the array locations points to a null terminated "token" found in s
+// defined by the delimiter array. The number of tokens found is returned by makeargv
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+/*
+ * Make argv array (*argvp) for tokens in s which are separated by
+ * delimiters.  Return -1 on error or the number of tokens otherwise.
+ */
+int makeargv(char * s, char * delimiters, char *** argvp)
+{
+	char * t;
+	char * snew;
+	int numtokens;
+	int i;
+	
+	/* snew is real start of string after skipping leading delimiters */
+	snew = s + strspn(s, delimiters); //very clever
+	//printf("s = %s\n", s);
+	//printf("delimiters = %s\n", delimiters);
+	//printf("strspn = %d\n", (int) strspn(s, delimiters));
+	//printf("snew = %s\n", snew);
+	
+	/* create space for a copy of snew in t */	
+	if ((t = calloc(strlen(snew) + 1, sizeof(char))) == NULL) //only true if no
+								  //more memory
+	{
+		*argvp = NULL;
+		numtokens = -1;
+	} 
+	else 
+	{                     /* count the number of tokens in snew */
+		strcpy(t, snew);
+		
+		if (strtok(t, delimiters) == NULL)
+			numtokens = 0;
+		else
+			for (numtokens = 1; strtok(NULL, delimiters) != NULL; numtokens++);  // THE work is in the For Construct
+			//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+			//start at 1 and move to the next,
+			//	if its null stop
+			//	else increment and move to the next
+			
+		/* create an argument array to contain ptrs to tokens */
+		if ((*argvp = calloc(numtokens + 1, sizeof(char *))) == NULL) 
+		   //^Notice the dereferencing		//Again only true if 
+							//no more memory
+		{
+			free(t);
+			numtokens = -1;
+		}
+		else 
+		{            /* insert pointers to tokens into the array */
+			if (numtokens > 0) 
+			{
+				strcpy(t, snew);
+				**argvp = strtok(t, delimiters); //set the first token
+				// printf("1 = %s\n", **argvp);
+				
+				for (i = 1; i < numtokens + 1; i++)
+				{
+					*((*argvp) + i) = strtok(NULL, delimiters);
+					//^^^^^^^^^^^^^^^^ set the other tokens
+					// printf("%d = %s\n", i, *((*argvp) + i));
+				}
+			}
+			else 
+			{
+				**argvp = NULL;
+				free(t);
+			}
+		}
+	}
+	
+	return numtokens;
+}
-- 
GitLab