comparison nnchat.c @ 211:c40329e95445

Trim both left and right side of username+password prompt input. Also allow empty passwords, to be able to use guest accounts.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 20 Nov 2010 23:13:30 +0200
parents 4554f9abc686
children 3ccfe8902fd5
comparison
equal deleted inserted replaced
210:4554f9abc686 211:c40329e95445
877 } 877 }
878 878
879 return FALSE; 879 return FALSE;
880 } 880 }
881 881
882 char *promptRequester(const char *info) 882 char *promptRequester(const char *info, BOOL allowEmpty)
883 { 883 {
884 char tmpBuf[512]; 884 char tmpBuf[512], *ptr;
885 ssize_t bufLen; 885 ssize_t pos;
886 886
887 fputs(info, stdout); 887 fputs(info, stdout);
888 fgets(tmpBuf, sizeof(tmpBuf), stdin); 888 fgets(tmpBuf, sizeof(tmpBuf), stdin);
889 889
890 for (bufLen = strlen(tmpBuf) - 1; bufLen > 0 && (tmpBuf[bufLen] == '\n' || tmpBuf[bufLen] == '\r' || th_isspace(tmpBuf[bufLen])); bufLen--) 890 for (pos = strlen(tmpBuf) - 1; pos > 0 && (tmpBuf[pos] == '\n' || tmpBuf[pos] == '\r' || th_isspace(tmpBuf[pos])); pos--)
891 tmpBuf[bufLen] = 0; 891 tmpBuf[pos] = 0;
892 892
893 if (bufLen > 0) 893 ptr = trimLeft(tmpBuf);
894 return th_strdup(tmpBuf); 894
895 if (allowEmpty || strlen(ptr) > 0)
896 return th_strdup(ptr);
895 else 897 else
896 return NULL; 898 return NULL;
897 } 899 }
898 900
899 int main(int argc, char *argv[]) 901 int main(int argc, char *argv[])
982 } 984 }
983 985
984 /* Check if we have username and password */ 986 /* Check if we have username and password */
985 if (optUserName == NULL || optPassword == NULL) { 987 if (optUserName == NULL || optPassword == NULL) {
986 printf("\nYou can avoid this prompt by issuing '/save' after logging in.\n\n"); 988 printf("\nYou can avoid this prompt by issuing '/save' after logging in.\n\n");
987 optUserName = promptRequester("NN username: "); 989 optUserName = promptRequester("NN username: ", FALSE);
988 optPassword = promptRequester("NN password: "); 990 optPassword = promptRequester("NN password: ", TRUE);
989 if (optUserName == NULL || optPassword == NULL) { 991 if (optUserName == NULL || optPassword == NULL) {
990 THERR("User/pass not specified, get some --help\n"); 992 THERR("User/pass not specified, get some --help\n");
991 return -1; 993 return -1;
992 } 994 }
993 } 995 }