I have written the following function which works for all parameters well rather than calling InteractiveCredentialDispathcer. When this function called, loop executed again from the top and doing all checks again. How should I fix this issue?
void InteractiveMode()
{
char commands[MAX_PATH] = { 0 };
PrintColorful(0, "%s", PTH_CMD);
while (fgets(commands, MAX_PATH - 1, stdin) != NULL)
{
if (strstr(commands, "help") || strstr(commands, "?"))
{
ShowHelpMessage();
}
else if (strstr(commands, "clear"))
{
ClearConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE));
}
else if (strstr(commands, "privilege"))
{
ShowPrivilegeStatus();
}
else if (strstr(commands, "login"))
{
if (InteractiveCredentialDispatcher())
{
NormalMessage("%s\n", "Process has been spawned successfully");
}
else
{
ErrorMessage("%s\n", "Process has not been spawned successfully");
}
}
else if (strstr(commands, "version"))
{
ShowProgramVersion();
}
else if (strstr(commands, "exit"))
{
NormalMessage("%s\n", "Program has been finished.");
ProgramExit();
}
else
{
printf("\n");
ErrorMessage("%s\n\n", "The command isn't appropriate:");
printf("\t");
NormalMessage("%s\n", "You entered a blank input or a wrong command.");
printf("\t");
NormalMessage("%s\n\n", "Execute help or ? command to see the manual.");
}
PrintColorful(0, "%s", PTH_CMD);
}
}
The following code called when the function InteractiveCredentialDispatcher executed:
BOOL InteractiveCredentialDispatcher()
{
TCHAR l_tc_Username[MAX_PATH];
TCHAR l_tc_Domain[MAX_PATH];
TCHAR l_tc_HashNtlm[MAX_PATH];
TCHAR l_tc_ComputerName[MAX_PATH];
TCHAR l_tc_ProcessName[MAX_PATH];
printf("\n\t");
WarningMessage("%s\n", "Fill following information:");
printf("\n\t\tUsername: ");
wscanf(L"%ls", l_tc_Username);
printf("\t\tDomain: ");
wscanf(L"%ls", l_tc_Domain);
printf("\t\tNTLM Hash: ");
wscanf(L"%ls", l_tc_HashNtlm);
printf("\t\tComputer Name: ");
wscanf(L"%ls", l_tc_ComputerName);
wcscpy(l_tc_ProcessName, TEXT("FM.exe "));
wcscat(l_tc_ProcessName, l_tc_ComputerName);
if (InteractiveAuthticationOnWindows(l_tc_Username, l_tc_Domain, l_tc_HashNtlm, l_tc_ProcessName))
return TRUE;
else
return FALSE;
}
The problem belonged to using scanf rather fgets in the function InteractiveCredentialDispatcher. I have solved the problem with the usage of just fgets and fgetws variant in the program. After that, while loop never executed twice.
BOOL InteractiveCredentialDispatcher()
{
TCHAR l_tc_Username[MAX_PATH];
TCHAR l_tc_Domain[MAX_PATH];
TCHAR l_tc_HashNtlm[MAX_PATH];
TCHAR l_tc_ComputerName[MAX_PATH];
TCHAR l_tc_ProcessName[MAX_PATH];
printf("\n\t");
WarningMessage("%s\n", "Fill following information:");
printf("\n\t\tUsername: ");
fgetstr(l_tc_Username, MAX_PATH, stdin);
printf("\t\tDomain: ");
fgetstr(l_tc_Domain, MAX_PATH, stdin);
printf("\t\tNTLM Hash: ");
fgetstr(l_tc_HashNtlm, MAX_PATH, stdin);
printf("\t\tComputer Name: ");
fgetstr(l_tc_ComputerName, MAX_PATH, stdin);
wcscpy(l_tc_ProcessName, TEXT("PTH-FileManager.exe "));
wcscat(l_tc_ProcessName, l_tc_ComputerName);
if (InteractiveAuthticationOnWindows(l_tc_Username, l_tc_Domain, l_tc_HashNtlm, l_tc_ProcessName))
{
return TRUE;
}
else
{
return FALSE;
}
}
Related
This code has one problem. The problem is in
Problem in if-statement
if(all_digits(to_find) && strlen(to_find) == 13)
Whenever I enter 13 characters but not from the file then it gives me a message of else statement but printing Found. Hello World! also. Although Found. Hello World! is in if-statement. It should not be printed. How to make if-statement work correctly?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int all_digits(char *s){
for (; *s!=0; s++){
if (!isdigit(*s)){
return 0;
}
}
return 1;
}
Another part of code
int main(){
FILE * fr = fopen("file.csv", "r");
char save[500], line[200], to_find[50];
int oneByOne = 0, numberOfFields=8;
char *word = NULL;
printf("Enter the ID card number: ");
scanf("%s", to_find);
if(all_digits(to_find) && strlen(to_find) == 13){
while(fgets(line, 200, fr)){
word = strtok(line, "\n");
strcpy(save, line);
if (strstr(save, to_find)){
char *wordone = strtok(save, ",");
while (wordone != NULL){
printf("Here are your details: %s\n", wordone);
wordone = strtok(NULL, ",");
}
}
}
fclose(fr);
printf("Found. Hello World!\n");
}
else {
printf("enter correclty\n");
}
return 0;
}
To run multiple tests without cascading if-thens you can use an error flag along with a one-time loop like so:
int err = 0; /* Be optimistic! (0 indicates success) */
do {
if (!test1-passed) {
err = 1;
break;
}
if (!test2-passed) {
err = 2;
break;
}
...
if (!testN-passed) {
err = N;
break;
}
printf("Success! All tests passed");
} while (0);
if (err) {
printf("Test %d failed", err);
}
Applied to you particular problem the code might look like this
... /* definitions here */
int err = 0; /* Be optimistic! (0 indicates success) */
do {
... /* input here */
do {
if (!all_digits(to_find)) {
err = 1;
break;
}
if (strlen(to_find) != 13) {
err = 2;
break;
}
{
err = 3 /* be pessimistic! */
while(fgets(line, 200, fr)){
/* word = strtok(line, "\n"); */ /* word is not needed. */
strcpy(save, line); /* Introducing save here is not necessary,
all following operation can be applied to line. */
if (strstr(save, to_find)){
char *wordone = strtok(save, ",");
while (wordone != NULL){
printf("Here are your details: %s\n", wordone);
wordone = strtok(NULL, ",");
err = 0; /* Phew, we are lucky! */
}
}
}
if (err) {
break;
}
}
printf("Success! All tests passed");
} while (0);
if (err) {
printf("Test %d failed", err);
}
} while (err);
In a multi-client/server implementation I am connecting the client and server and then taking inputs from the client's stdin to send to server (otherwise if there's an incoming message from the server, I am performing a recv() and I am using select() to switch between the two). Now, as an example, I am giving an input "LOGIN IP PORT" and that connects me to the server. And then I am back to the client's command line again.
NOW, when I am back at the command line again after the login command, if I press enter without typing any specific command, the previous LOGIN command is executed again, it seems. I think the stdin is not being flushed. Although I have done fseek(stdin,0,SEEK_END), it's still not getting flushed. The client side main loop code is as below:
while(TRUE) {
fd_max = server_fd;
FD_ZERO(&sock_list);
FD_SET(STDIN, &sock_list);
FD_SET(server_fd, &sock_list);
printf("\n[PA1-Client]$ ");
fflush(stdout);
fseek(stdin,0,SEEK_END);
selret = select(fd_max + 1, &sock_list, NULL, NULL, NULL);
if(selret < 0)
perror("Select failed");
else {
int sock_index;
for(sock_index = 0; sock_index<=fd_max; sock_index++) {
if(FD_ISSET(sock_index, &sock_list)) {
if(sock_index == STDIN) {
char* cmd = (char*) malloc(sizeof(char)*CMD_SIZE);
if(fgets(cmd, CMD_SIZE - 1, stdin) == NULL)
exit(-1);
cmd[strcspn(cmd, "\n")] = '\0'; char* token[3];
tokenize(cmd, token, " ");
if(!strcmp(token[0], "LOGIN")) {
server_fd = clientLogin(token[1], token[2]);
}
else if(!strcmp(token[0], "REFRESH")) {
clientRefresh(token[0]);
}
else if(!strcmp(token[0], "LOGOUT")) {
clientLogout(token[0]);
}
else if(!strcmp(token[0], "EXIT")) {
clientExit(token[0]);
exit(0);
}
else if(!strcmp(token[0], "SEND")) {
clientSend(cmd);
}
else if(!strcmp(token[0], "BROADCAST")) {
clientBroadcast(cmd);
}
else if(!strcmp(token[0], "BLOCK")) {
clientBlock(cmd);
}
else if(!strcmp(token[0], "UNBLOCK")) {
clientUnblock(cmd);
}
else if(!strcmp(token[0], "AUTHOR")) {
commonAuthor();
}
else if(!strcmp(token[0], "IP")) {
commonIP();
}
else if(!strcmp(token[0], "PORT")) {
commonPort();
}
else if(!strcmp(token[0], "LIST")) {
commonList();
}
}
else {
char* buffer = (char*)malloc(sizeof(char)*MSG_SIZE);
if(recv(server_fd, buffer, sizeof(buffer), 0) == 0) {
close(server_fd);
server_fd = 0;
}
else
msgRecvEvent(buffer);
}
}
}
}
}
return 0;
}
I have done the program as reading a string and then converted to a file input. For a single line, it is getting output. I also want to display character,integer and floating point constants and symbols in the output.
For a multiline or for a c program as input, the arrays which are printed in the last just mix up each other and some another input is generated. Anyone have idea how this happen?
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define KEYWORDS 32
#define OPERATORS 25
#define DEBUG_MODE 1
typedef enum {FALSE,TRUE} boolean;
char *trimString(char *str)
{
char *end;
// Trim leading space
while(isspace((unsigned char)*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
int main()
{
char delimit[]=" \t\r\n\v\f(){};""";
char str[80],filename[40],*token,*keyword[30],*identifier[30],*operator[30],*literal[30],tmp[80];
int state=0,i=0,j=0,k=0,l=0,m=0,c,ch=0,isKeyword=0,isOperator=0;
boolean isComment=FALSE;
char* key[] = {"auto","break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long","register", "return", "short","signed", "sizeof", "static","struct", "switch","typedef","union", "unsigned", "void","volatile", "while"};
char* operators[] = {"+", "-", "/", "*", "%", "=", "+=", "++", "--", "-=", "*=", "/=", "%=", "==", ">", "<", "!=", ">=", "<=", "&&", "||", "!", "<<", ">>", "sizeof"};
printf("Enter the filename: ");
gets(filename);
FILE* file = fopen(filename, "r");
if(file==NULL)
{
printf("\nError opening file with filename : \'%s\'!!\n\n",filename);
exit(0);
}
while (fgets(str, sizeof(str), file))
{
//Skip=FALSE;
str[sizeof(str)-1]='\0';
if((str[0]=='#') || (str[0]=='\n')) //remove preprocessor directives and blank lines
goto gotoNextLine;
if(str[0]=='/')
{
if(str[1]=='/')
goto gotoNextLine;
else if(str[1]=='*')
{
isComment=TRUE;
goto gotoNextLine;
}
}
if(DEBUG_MODE)
printf("\n\n**isComment=%d**\n\n",isComment);
if(isComment) // skip block comments
{
c=0;
while(str[c]!='\0')
{
if(str[c]=='*')
{
if(str[c+1]=='/')
{
isComment=FALSE;
for (i=c+2; str[i]!='\0'; i++)
tmp[ch++] = str[i];
strcpy(str,tmp);
break;
}
}
if(DEBUG_MODE)
printf("\n\n**The comment loop**\n\n");
c++;
}
}
if(!isComment)
{
for (token = strtok(str, delimit); token != NULL; token = strtok(NULL, delimit))
{
isKeyword=0;
isOperator=0;
if(DEBUG_MODE)
printf("\n\n**token=%s**\n\n",token);
for (i=0;i<OPERATORS;i++)
{
if(!strcmp(trimString(token),operators[i]))
{
isOperator=1;
operator[l++]=token;
break;//goto nextToken;
}
}
if(DEBUG_MODE)
printf("\n\n**isOperator=%d**\n\n",isOperator);
if(!isOperator)
{
for (i=0;i<KEYWORDS;i++)
{
if(!strcasecmp(trimString(token),key[i]))
{
isKeyword=1;
keyword[j++]=token;
break;//goto nextToken;
}
}
}
if(DEBUG_MODE)
printf("\n\n**isKeyword=%d**\n\n",isKeyword);
if(!isKeyword && !isOperator)
{
i=ch=state=0;
if(token[0]=='"')
{
for (i=1; str[i]!='"'; i++)
tmp[ch++] = token[i];
strcpy(token,tmp);
literal[m++]=token;
goto nextToken;
}
while(token[i]!='\0')
{
if((token[0]=='#') || (token[0]=='\n'))
goto nextToken;
switch(state)
{
case 0: if(isalpha(token[i]) || token[i]=='_')
{
state=1;
i++;
}
else
state=2;
break;
case 1: if(isalnum(token[i]) || token[i]=='_')
{
state=1;
i++;
}
else
state=2;
break;
case 2: goto nextToken;
}
}
if(state==1)
{
identifier[k++]=token;
if(DEBUG_MODE)
printf("\n\n**Identifier_True=%s**\n\n",token);
}
}
nextToken: ;
}
}
gotoNextLine: ;
}
if(DEBUG_MODE)
printf("\n\n**Keywords_Count=%d**\n\n",j);
if(j)
{
printf("\nKeywords: \n");
for(i=0;i<j;i++)
{
printf("%s",keyword[i]);
if((i+1)<j)
printf(", ");
}
printf("\n");
}
if(DEBUG_MODE)
printf("\n\n**Operators_Count=%d**\n\n",l);
if(l)
{
printf("\nOperators: \n");
for(i=0;i<l;i++)
{
printf("%s",operator[i]);
if((i+1)<l)
printf(", ");
}
printf("\n");
}
if(DEBUG_MODE)
printf("\n\n**Identifiers_Count=%d**\n\n",k);
if(k)
{
printf("\nIdentifiers: \n");
for(i=0;i<k;i++)
{
printf("%s",identifier[i]);
if((i+1)<k)
printf(", ");
}
}
printf("\n");
fclose(file);
}
void main()
{
Password();
}
int Password()
{
// Declare local variables//
char cPassCode[] = "String";
int iFlag, iComparison = 0;
// Run the code to check the password//
while (iFlag = 0)
{
printf("Please enter the password: ");
scanf("%s", cPassCode);
iComparison = strcmp(cPassCode, "A23bc5");
if (iComparison = 0)
{
ArrayPrinter(Array);
iFlag = 1;
}
else
{
printf("Wrong password");
iFlag = 0;
}
return(iFlag);
}
}
I edited the section of code and as far as I can tell it should run properly when the password A23bc5 is entered. However it is always returning a wrong password. Any ideas?
strcmp function returns 0 if the strings are equal. You should edit the condition block.
Edit:
Actually you have also very basic syntax problems.
int Password()
{
//declare local variables//
char cPassCode[] = "String";
int iFlag=0, iComparison = 0;
//Run the code to check the password//
while (iFlag == 0)
{
printf("Please enter the password: ");
scanf("%s", cPassCode);
iComparison = strcmp(cPassCode,"A23bc5");
if (iComparison == 0)
{
printf("\n Accepted");
iFlag = 1;
}
else
{
printf("Wrong password");
iFlag = 0;
}
return(iFlag);
}
}
int main()
{
Password();
return 0;
}
This will work
if (iComparison = 0)
Is assigning 0 to the variable and then testing it, and 0 evaluates to false.
if (iComparison == 0)
Is checking if the variable is 0, which is probably what you meant
//String compare in C for a password Until 5 try//
#include<stdio.h>
#include<string.h>
int main(){
int i,value;
char pass[10],id[10],key[]="1234";
printf("enter your id: ");
scanf("%s", id);
for(i=5;i>=0;i--){
printf("\nenter your password: ");
scanf("%s",pass);
value=strcmp(pass,key);
if(value==0){
printf("matched");
return 0 ;
}
else{
printf("not matched you have %d try left", i);
}
}
return 0;
}
#include"shell.h"
int main()
{
char cInput[50];
char cCopy[50];
char *pCommand;
char pArguement[10];
bool bRun = true;
while(strcmp(pCommand, "exit"))//run untill exit
{
printf("[myshell]%% ");
cin >> cInput; //get command from user
for(int i=0; i<50; i++)
cCopy[i] = cInput[i];
//get command
pCommand = strtok(cInput, " ");
if(!strcmp(pCommand, "pwd"))
{
printf("No FUNCTIONAILITY FOR PWD! \n");
}
else if(!strcmp(pCommand, "list"))
{
printf("No FUNCTIONAILITY FOR LIST! \n");
}
else if(!strcmp(pCommand, "history"))
{
printf("No FUNCTIONAILITY FOR HISTORY! \n");
}
else if(!strcmp(pCommand, "prompt"))
{
// pCommand = strtok(cCopy, "y");
while(pCommand != NULL)
{
printf(" %s \n", pCommand);
pCommand = strtok(NULL, " ");
}
return 0;
}
else//command not found
{
printf("%s: Command not found \n", pCommand);
}
}
return 0;
}
So I'm trying to follow the example of strtok from the C standard library, but I can't figure out why I'm unable to get the next word from the cstring cInput. I was under the impression that using:
while(pCommand != NULL)
{
printf(" %s \n", pCommand);
pCommand = strtok(NULL, " ");
}
would result in getting the next words.
Any help?
Thanks!