Commits

Palmer, 2E0EOL  committed 05371d7 Draft

Standard getopt() code for handling -d

  • Participants
  • Parent commits fb239e8
  • Branches f/201207-debugopt

Comments (0)

Files changed (1)

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <stdbool.h>
 
 /* MAX_HOMES defines the absolutely maximum number of home directory
 that can managed.  There is a tradeoff in terms of managing a lot of symlinks
 	const char *HomeRootLnk,
 	char **UserList, const int UserListMarker
 );
+static bool ProcessOptions(int ArgC, char **ArgV);
+/*-------------------------------------------------------------------------*/
+static unsigned short int DebugLevel = 0;
 /*-------------------------------------------------------------------------*/
 static int EnumHomes(
 	char **Homes, const int MaxHomes,
 	return nCleaned;
 }
 /*-------------------------------------------------------------------------*/
-int main()
+static bool ProcessOptions(int ArgC, char **ArgV)
+{
+	int c;
+	opterr = 0;
+
+	while ( (c = getopt(ArgC, ArgV, "d:")) != -1 ) {
+		switch ( c ) {
+			case 'd' : {
+				DebugLevel = (unsigned int)(abs(atoi(optarg)));
+				break;
+			}
+			default : {
+				return false;
+			}
+		}
+	}
+
+	return true;
+}
+/*-------------------------------------------------------------------------*/
+int main(int argc, char *argv[])
 {
 	const char *const homeRootTgt = "/mnt";
 	const char *const homeRootLnk = "/home";
 	size_t hrTgtLen = strlen(homeRootTgt);
 	size_t htLnkLen = strlen(homeRootLnk);
 
+	/* Bail, if command-line arguments are bad */
+	if ( !ProcessOptions(argc, argv) ) return EXIT_FAILURE;
+
 	nHomes = EnumHomes(
 		homes, sizeof(homes)/sizeof(homes[0]),
 		homeRootTgt, homePattern