3 * UCC (University [of WA] Computer Club) Electronic Accounting System
6 * main.c - Core and Initialisation
8 * This file is licenced under the 3-clause BSD Licence. See the file
9 * COPYING for full details.
14 #include <ctype.h> // isspace
20 #include <unistd.h> // close
21 #include <netdb.h> // gethostbyname
22 #include <pwd.h> // getpwuids
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <openssl/sha.h> // SHA1
28 #define USE_NCURSES_INTERFACE 0
29 #define DEBUG_TRACE_SERVER 0
32 typedef struct sItem {
39 int main(int argc, char *argv[]);
42 int ShowNCursesUI(void);
43 void ShowItemAt(int Row, int Col, int Width, int Index);
44 void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1, const char *Mid, char Pad2, const char *Right, ...);
45 // --- Coke Server Communication ---
46 int OpenConnection(const char *Host, int Port);
47 int Authenticate(int Socket);
48 void PopulateItemList(int Socket);
49 int DispenseItem(int Socket, int ItemID);
50 int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason);
51 int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason);
52 int Dispense_EnumUsers(int Socket);
53 int Dispense_ShowUser(int Socket, const char *Username);
54 void _PrintUserLine(const char *Line);
55 int Dispense_AddUser(int Socket, const char *Username);
56 int Dispense_SetUserType(int Socket, const char *Username, const char *TypeString);
58 char *ReadLine(int Socket);
59 int sendf(int Socket, const char *Format, ...);
60 char *trim(char *string);
61 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage);
62 void CompileRegex(regex_t *regex, const char *pattern, int flags);
65 char *gsDispenseServer = "localhost";
66 int giDispensePort = 11020;
70 regex_t gArrayRegex, gItemRegex, gSaltRegex, gUserInfoRegex;
71 int gbIsAuthenticated = 0;
73 char *gsItemPattern; //!< Item pattern
74 char *gsOverrideUser; //!< '-u' Dispense as another user
75 int gbUseNCurses = 0; //!< '-G' Use the NCurses GUI?
76 int giMinimumBalance = INT_MIN; //!< '-m' Minumum balance for `dispense acct`
77 int giMaximumBalance = INT_MAX; //!< '-M' Maximum balance for `dispense acct`
80 int main(int argc, char *argv[])
86 // -- Create regular expressions
87 // > Code Type Count ...
88 CompileRegex(&gArrayRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([0-9]+)", REG_EXTENDED); //
89 // > Code Type Ident Price Desc
90 CompileRegex(&gItemRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([A-Za-z0-9:]+?)\\s+([0-9]+)\\s+(.+)$", REG_EXTENDED);
92 CompileRegex(&gSaltRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+(.+)$", REG_EXTENDED);
93 // > Code 'User' Username Balance Flags
94 CompileRegex(&gUserInfoRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([^ ]+)\\s+(-?[0-9]+)\\s+(.+)$", REG_EXTENDED);
97 for( i = 1; i < argc; i ++ )
110 case 'm': // Minimum balance
111 giMinimumBalance = atoi(argv[++i]);
113 case 'M': // Maximum balance
114 giMaximumBalance = atoi(argv[++i]);
117 case 'u': // Override User
118 gsOverrideUser = argv[++i];
129 if( strcmp(arg, "acct") == 0 )
132 sock = OpenConnection(gsDispenseServer, giDispensePort);
133 if( sock < 0 ) return -1;
136 if( i + 1 == argc ) {
137 Dispense_EnumUsers(sock);
141 // argv[i+1]: Username
146 if( i + 3 >= argc ) {
147 fprintf(stderr, "Error: `dispense acct' needs a reason\n");
151 // Authentication required
152 if( Authenticate(sock) )
155 // argv[i+1]: Username
156 // argv[i+2]: Ammount
159 if( argv[i+2][0] == '=' ) {
161 Dispense_SetBalance(sock, argv[i+1], atoi(argv[i+2] + 1), argv[i+3]);
165 Dispense_AlterBalance(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]);
169 Dispense_ShowUser(sock, argv[i+1]);
174 else if( strcmp(arg, "user") == 0 )
176 // Check argument count
177 if( i + 1 >= argc ) {
178 fprintf(stderr, "Error: `dispense user` requires arguments\n");
184 sock = OpenConnection(gsDispenseServer, giDispensePort);
185 if( sock < 0 ) return -1;
187 // Attempt authentication
188 if( Authenticate(sock) )
192 if( strcmp(argv[i+1], "add") == 0 )
194 if( i + 2 >= argc ) {
195 fprintf(stderr, "Error: `dispense user add` requires an argument\n");
200 Dispense_AddUser(sock, argv[i+2]);
203 else if( strcmp(argv[i+1], "type") == 0 )
205 if( i + 3 >= argc ) {
206 fprintf(stderr, "Error: `dispense user type` requires two arguments\n");
211 Dispense_SetUserType(sock, argv[i+2], argv[i+3]);
215 fprintf(stderr, "Error: Unknown sub-command for `dispense user`\n");
222 // Item name / pattern
229 sock = OpenConnection(gsDispenseServer, giDispensePort);
230 if( sock < 0 ) return -1;
236 PopulateItemList(sock);
242 else if( gbUseNCurses )
248 for( i = 0; i < giNumItems; i ++ ) {
249 printf("%2i %s\t%3i %s\n", i, gaItems[i].Ident, gaItems[i].Price, gaItems[i].Desc);
258 fgets(buffer, BUFSIZ, stdin);
262 if( buf[0] == 'q' ) break;
266 if( i != 0 || buf[0] == '0' )
268 if( i < 0 || i >= giNumItems ) {
269 printf("Bad item %i (should be between 0 and %i)\n", i, giNumItems);
277 // Check for a valid item ID
279 DispenseItem(sock, i);
291 "\t\tShow interactive list\n"
292 "\tdispense <item>\n"
293 "\t\tDispense named item\n"
294 "\tdispense give <user> <ammount> \"<reason>\"\n"
295 "\t\tGive some of your money away\n"
296 "\tdispense acct [<user>]\n"
297 "\t\tShow user balances\n"
298 "\tdispense acct <user> [+-=]<ammount> \"<reason>\"\n"
299 "\t\tAlter a account value (Coke members only)\n"
303 "\t\tSet a different user (Coke members only)\n"
305 "\t\tShow help text\n"
307 "\t\tUse alternate GUI\n"
308 "\t-m <min balance>\n"
309 "\t-M <max balance>\n"
310 "\t\tSet the Maximum/Minimum balances shown in `dispense acct`\n"
314 // -------------------
315 // --- NCurses GUI ---
316 // -------------------
318 * \brief Render the NCurses UI
320 int ShowNCursesUI(void)
322 // TODO: ncurses interface (with separation between item classes)
323 // - Hmm... that would require standardising the item ID to be <class>:<index>
328 const int displayMinWidth = 40;
329 const int displayMinItems = 8;
330 char *titleString = "Dispense";
331 int itemCount = displayMinItems;
334 int ret = -2; // -2: Used for marking "no return yet"
343 // - 6: randomly chosen (Need at least 3)
344 itemCount = LINES - 6;
345 if( itemCount > giNumItems )
346 itemCount = giNumItems;
349 height = itemCount + 3;
350 width = displayMinWidth;
353 xBase = COLS/2 - width/2;
354 yBase = LINES/2 - height/2;
359 PrintAlign(yBase, xBase, width, "/", '-', titleString, '-', "\\");
362 for( i = 0; i < itemCount; i ++ )
364 move( yBase + 1 + i, xBase );
366 if( currentItem == itemBase + i ) {
374 // - Oh god, magic numbers!
375 if( i == 0 && itemBase > 0 ) {
377 times = width-1 - 8 - 3;
378 while(times--) addch(' ');
380 else if( i == itemCount - 1 && itemBase < giNumItems - itemCount ) {
382 times = width-1 - 8 - 3;
383 while(times--) addch(' ');
387 ShowItemAt( yBase + 1 + i, xBase + 5, width - 7, itemBase + i);
391 // Scrollbar (if needed)
392 if( giNumItems > itemCount ) {
396 else if( i == itemCount - 1 ) {
400 int percentage = itemBase * 100 / (giNumItems-itemCount);
401 if( i-1 == percentage*(itemCount-3)/100 ) {
415 PrintAlign(yBase+height-2, xBase, width, "\\", '-', "", '-', "/");
428 //if( itemBase < giNumItems - (itemCount) )
430 if( currentItem < giNumItems - 1 )
432 if( itemBase + itemCount - 1 <= currentItem && itemBase + itemCount < giNumItems )
438 if( currentItem > 0 )
440 if( itemBase + 1 > currentItem && itemBase > 0 )
456 ret = -1; // -1: Return with no dispense
460 // Check if the return value was changed
461 if( ret != -2 ) break;
473 * \brief Show item \a Index at (\a Col, \a Row)
474 * \note Part of the NCurses UI
476 void ShowItemAt(int Row, int Col, int Width, int Index)
484 if( Index < 0 || Index >= giNumItems ) {
489 name = gaItems[Index].Desc;
490 price = gaItems[Index].Price;
493 printw("%02i %s", Index, name);
495 getyx(stdscr, _y, _x);
496 // Assumes max 4 digit prices
497 times = Width - 4 - (_x - Col); // TODO: Better handling for large prices
498 while(times--) addch(' ');
499 printw("%4i", price);
503 * \brief Print a three-part string at the specified position (formatted)
504 * \note NCurses UI Helper
506 * Prints \a Left on the left of the area, \a Right on the righthand side
507 * and \a Mid in the middle of the area. These are padded with \a Pad1
508 * between \a Left and \a Mid, and \a Pad2 between \a Mid and \a Right.
510 * ::printf style format codes are allowed in \a Left, \a Mid and \a Right,
511 * and the arguments to these are read in that order.
513 void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1,
514 const char *Mid, char Pad2, const char *Right, ...)
516 int lLen, mLen, rLen;
521 // Get the length of the strings
522 va_start(args, Right);
523 lLen = vsnprintf(NULL, 0, Left, args);
524 mLen = vsnprintf(NULL, 0, Mid, args);
525 rLen = vsnprintf(NULL, 0, Right, args);
529 if( lLen + mLen/2 > Width/2 || mLen/2 + rLen > Width/2 ) {
530 return ; // TODO: What to do?
536 va_start(args, Right);
540 vsnprintf(tmp, lLen+1, Left, args);
544 times = Width/2 - mLen/2 - lLen;
545 while(times--) addch(Pad1);
549 vsnprintf(tmp, mLen+1, Mid, args);
553 times = Width/2 - mLen/2 - rLen;
554 while(times--) addch(Pad2);
558 vsnprintf(tmp, rLen+1, Right, args);
563 // ---------------------
564 // --- Coke Protocol ---
565 // ---------------------
566 int OpenConnection(const char *Host, int Port)
568 struct hostent *host;
569 struct sockaddr_in serverAddr;
572 host = gethostbyname(Host);
574 fprintf(stderr, "Unable to look up '%s'\n", Host);
578 memset(&serverAddr, 0, sizeof(serverAddr));
580 serverAddr.sin_family = AF_INET; // IPv4
581 // NOTE: I have a suspicion that IPv6 will play sillybuggers with this :)
582 serverAddr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
583 serverAddr.sin_port = htons(Port);
585 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
587 fprintf(stderr, "Failed to create socket\n");
593 struct sockaddr_in localAddr;
594 memset(&localAddr, 0, sizeof(localAddr));
595 localAddr.sin_family = AF_INET; // IPv4
596 localAddr.sin_port = 1023; // IPv4
597 // Attempt to bind to low port for autoauth
598 bind(sock, &localAddr, sizeof(localAddr));
602 if( connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0 ) {
603 fprintf(stderr, "Failed to connect to server\n");
611 * \brief Authenticate with the server
612 * \return Boolean Failure
614 int Authenticate(int Socket)
621 regmatch_t matches[4];
623 if( gbIsAuthenticated ) return 0;
626 pwd = getpwuid( getuid() );
628 // Attempt automatic authentication
629 sendf(Socket, "AUTOAUTH %s\n", pwd->pw_name);
631 // Check if it worked
632 buf = ReadLine(Socket);
634 responseCode = atoi(buf);
635 switch( responseCode )
638 case 200: // Authenticated, return :)
639 gbIsAuthenticated = 1;
643 case 401: // Untrusted, attempt password authentication
646 sendf(Socket, "USER %s\n", pwd->pw_name);
647 printf("Using username %s\n", pwd->pw_name);
649 buf = ReadLine(Socket);
652 // Expected format: 100 SALT <something> ...
654 RunRegex(&gSaltRegex, buf, 4, matches, "Malformed server response");
655 responseCode = atoi(buf);
656 if( responseCode != 100 ) {
657 fprintf(stderr, "Unknown repsonse code %i from server\n%s\n", responseCode, buf);
663 if( memcmp( buf+matches[2].rm_so, "SALT", matches[2].rm_eo - matches[2].rm_so) == 0) {
664 // Store it for later
665 memcpy( salt, buf + matches[3].rm_so, matches[3].rm_eo - matches[3].rm_so );
666 salt[ matches[3].rm_eo - matches[3].rm_so ] = 0;
670 // Give three attempts
671 for( i = 0; i < 3; i ++ )
673 int ofs = strlen(pwd->pw_name)+strlen(salt);
676 char *pass = getpass("Password: ");
679 // Create hash string
680 // <username><salt><hash>
681 strcpy(tmp, pwd->pw_name);
683 SHA1( (unsigned char*)pass, strlen(pass), h );
684 memcpy(tmp+ofs, h, 20);
687 SHA1( (unsigned char*)tmp, ofs+20, h );
688 sprintf(tmpBuf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
689 h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7], h[ 8], h[ 9],
690 h[10], h[11], h[12], h[13], h[14], h[15], h[16], h[17], h[18], h[19]
694 sendf(Socket, "PASS %s\n", tmpBuf);
695 buf = ReadLine(Socket);
697 responseCode = atoi(buf);
699 if( responseCode == 200 ) break;
700 // Bad username/password
701 if( responseCode == 401 ) continue;
703 fprintf(stderr, "Unknown repsonse code %i from server\n%s\n", responseCode, buf);
709 gbIsAuthenticated = 1;
713 return 2; // 2 = Bad Password
715 case 404: // Bad Username
716 fprintf(stderr, "Bad Username '%s'\n", pwd->pw_name);
721 fprintf(stderr, "Unkown response code %i from server\n", responseCode);
730 * \brief Fill the item information structure
731 * \return Boolean Failure
733 void PopulateItemList(int Socket)
738 char *itemType, *itemStart;
740 regmatch_t matches[4];
742 // Ask server for stock list
743 send(Socket, "ENUM_ITEMS\n", 11, 0);
744 buf = ReadLine(Socket);
746 //printf("Output: %s\n", buf);
748 responseCode = atoi(buf);
749 if( responseCode != 201 ) {
750 fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode);
759 RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response");
761 itemType = &buf[ matches[2].rm_so ]; buf[ matches[2].rm_eo ] = '\0';
762 count = atoi( &buf[ matches[3].rm_so ] );
765 if( strcmp(itemType, "Items") != 0 ) {
767 fprintf(stderr, "Unexpected array type, expected 'Items', got '%s'\n",
772 itemStart = &buf[ matches[3].rm_eo ];
777 gaItems = malloc( giNumItems * sizeof(tItem) );
779 // Fetch item information
780 for( i = 0; i < giNumItems; i ++ )
782 regmatch_t matches[6];
785 buf = ReadLine(Socket);
786 responseCode = atoi(buf);
788 if( responseCode != 202 ) {
789 fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode);
793 RunRegex(&gItemRegex, buf, 6, matches, "Malformed server response");
795 buf[ matches[3].rm_eo ] = '\0';
797 gaItems[i].Ident = strdup( buf + matches[3].rm_so );
798 gaItems[i].Price = atoi( buf + matches[4].rm_so );
799 gaItems[i].Desc = strdup( buf + matches[5].rm_so );
805 buf = ReadLine(Socket);
806 responseCode = atoi(buf);
808 if( responseCode != 200 ) {
809 fprintf(stderr, "Unknown response from dispense server %i\n'%s'",
819 * \brief Dispense an item
820 * \return Boolean Failure
822 int DispenseItem(int Socket, int ItemID)
824 int ret, responseCode;
827 if( ItemID < 0 || ItemID > giNumItems ) return -1;
830 sendf(Socket, "DISPENSE %s\n", gaItems[ItemID].Ident);
831 buf = ReadLine(Socket);
833 responseCode = atoi(buf);
834 switch( responseCode )
837 printf("Dispense OK\n");
841 printf("Not authenticated\n");
845 printf("Insufficient balance\n");
849 printf("Bad item name, bug report\n");
853 printf("Item failed to dispense, is the slot empty?\n");
857 printf("Dispense not possible (slot empty/permissions)\n");
861 printf("Unknown response code %i ('%s')\n", responseCode, buf);
871 * \brief Alter a user's balance
873 int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason)
878 sendf(Socket, "ADD %s %i %s\n", Username, Ammount, Reason);
879 buf = ReadLine(Socket);
881 responseCode = atoi(buf);
886 case 200: return 0; // OK
887 case 403: // Not in coke
888 fprintf(stderr, "You are not in coke (sucker)\n");
890 case 404: // Unknown user
891 fprintf(stderr, "Unknown user '%s'\n", Username);
894 fprintf(stderr, "Unknown response code %i\n", responseCode);
902 * \brief Alter a user's balance
904 int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason)
909 sendf(Socket, "SET %s %i %s\n", Username, Ammount, Reason);
910 buf = ReadLine(Socket);
912 responseCode = atoi(buf);
917 case 200: return 0; // OK
918 case 403: // Not in coke
919 fprintf(stderr, "You are not in coke (sucker)\n");
921 case 404: // Unknown user
922 fprintf(stderr, "Unknown user '%s'\n", Username);
925 fprintf(stderr, "Unknown response code %i\n", responseCode);
933 * \brief Enumerate users
935 int Dispense_EnumUsers(int Socket)
940 regmatch_t matches[4];
942 if( giMinimumBalance != INT_MIN ) {
943 if( giMaximumBalance != INT_MAX ) {
944 sendf(Socket, "ENUM_USERS %i %i\n", giMinimumBalance, giMaximumBalance);
947 sendf(Socket, "ENUM_USERS %i\n", giMinimumBalance);
951 if( giMaximumBalance != INT_MAX ) {
952 sendf(Socket, "ENUM_USERS - %i\n", giMaximumBalance);
955 sendf(Socket, "ENUM_USERS\n");
958 buf = ReadLine(Socket);
959 responseCode = atoi(buf);
963 case 201: break; // Ok, length follows
966 fprintf(stderr, "Unknown response code %i\n%s\n", responseCode, buf);
971 // Get count (not actually used)
972 RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response");
973 nUsers = atoi( buf + matches[3].rm_so );
974 printf("%i users returned\n", nUsers);
979 // Read returned users
981 buf = ReadLine(Socket);
982 responseCode = atoi(buf);
984 if( responseCode != 202 ) break;
988 } while(responseCode == 202);
990 // Check final response
991 if( responseCode != 200 ) {
992 fprintf(stderr, "Unknown response code %i\n%s\n", responseCode, buf);
1002 int Dispense_ShowUser(int Socket, const char *Username)
1005 int responseCode, ret;
1007 sendf(Socket, "USER_INFO %s\n", Username);
1008 buf = ReadLine(Socket);
1010 responseCode = atoi(buf);
1012 switch(responseCode)
1015 _PrintUserLine(buf);
1020 printf("Unknown user '%s'\n", Username);
1025 fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
1035 void _PrintUserLine(const char *Line)
1037 regmatch_t matches[6];
1040 RunRegex(&gUserInfoRegex, Line, 6, matches, "Malformed server response");
1045 int usernameLen = matches[3].rm_eo - matches[3].rm_so;
1046 char username[usernameLen + 1];
1047 int flagsLen = matches[5].rm_eo - matches[5].rm_so;
1048 char flags[flagsLen + 1];
1050 memcpy(username, Line + matches[3].rm_so, usernameLen);
1051 username[usernameLen] = '\0';
1052 memcpy(flags, Line + matches[5].rm_so, flagsLen);
1053 flags[flagsLen] = '\0';
1055 bal = atoi(Line + matches[4].rm_so);
1056 printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, bal%100, flags);
1060 int Dispense_AddUser(int Socket, const char *Username)
1063 int responseCode, ret;
1065 sendf(Socket, "USER_ADD %s\n", Username);
1067 buf = ReadLine(Socket);
1068 responseCode = atoi(buf);
1070 switch(responseCode)
1073 printf("User '%s' added\n", Username);
1078 printf("Only wheel can add users\n");
1083 printf("User '%s' already exists\n", Username);
1088 fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
1098 int Dispense_SetUserType(int Socket, const char *Username, const char *TypeString)
1101 int responseCode, ret;
1103 // TODO: Pre-validate the string
1105 sendf(Socket, "USER_FLAGS %s %s\n", Username, TypeString);
1107 buf = ReadLine(Socket);
1108 responseCode = atoi(buf);
1110 switch(responseCode)
1113 printf("User '%s' updated\n", Username);
1118 printf("Only wheel can modify users\n");
1123 printf("User '%s' does not exist\n", Username);
1128 printf("Flag string is invalid\n");
1133 fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
1146 char *ReadLine(int Socket)
1148 static char buf[BUFSIZ];
1149 static int bufPos = 0;
1150 static int bufValid = 0;
1152 char *newline = NULL;
1154 char *ret = malloc(10);
1156 #if DEBUG_TRACE_SERVER
1157 printf("ReadLine: ");
1169 len = recv(Socket, buf+bufPos, BUFSIZ-1-bufPos, 0);
1170 buf[bufPos+len] = '\0';
1173 newline = strchr( buf+bufPos, '\n' );
1178 retLen += strlen(buf+bufPos);
1179 ret = realloc(ret, retLen + 1);
1180 strcat( ret, buf+bufPos );
1183 int newLen = newline - (buf+bufPos) + 1;
1184 bufValid = len - newLen;
1187 if( len + bufPos == BUFSIZ - 1 ) bufPos = 0;
1190 #if DEBUG_TRACE_SERVER
1191 printf("%i '%s'\n", retLen, ret);
1197 int sendf(int Socket, const char *Format, ...)
1202 va_start(args, Format);
1203 len = vsnprintf(NULL, 0, Format, args);
1208 va_start(args, Format);
1209 vsnprintf(buf, len+1, Format, args);
1212 #if DEBUG_TRACE_SERVER
1213 printf("sendf: %s", buf);
1216 return send(Socket, buf, len, 0);
1220 char *trim(char *string)
1224 while( isspace(*string) )
1227 for( i = strlen(string); i--; )
1229 if( isspace(string[i]) )
1238 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
1242 ret = regexec(regex, string, nMatches, matches, 0);
1244 size_t len = regerror(ret, regex, NULL, 0);
1246 regerror(ret, regex, errorStr, len);
1247 printf("string = '%s'\n", string);
1248 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
1255 void CompileRegex(regex_t *regex, const char *pattern, int flags)
1257 int ret = regcomp(regex, pattern, flags);
1259 size_t len = regerror(ret, regex, NULL, 0);
1261 regerror(ret, regex, errorStr, len);
1262 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);