You should turn on warnings when using C and you would most likely find out why this is failing. If you used this to compile it with Clang
gcc -Wall -std=c11 -pedantic goo.c
You would have got these errors:
goo.c:19:13: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if (av[1] == "stop" && av[2] == "A")
^ ~~~~~~
goo.c:19:32: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if (av[1] == "stop" && av[2] == "A")
^ ~~~
goo.c:24:13: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if (av[1] == "start" && av[2] == "^Q")
^ ~~~~~~~
goo.c:24:33: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
if (av[1] == "start" && av[2] == "^Q")
You need to compare strings using string comparison functions. You cannot compare strings the way you're doing it using ==
. Try something like this instead:
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "termios.h"
int main (int ac, char* av[])
{
struct termios ttyinfo;
int result;
result = tcgetattr(0, &ttyinfo);
if (result == -1) {
perror("cannot get params about stdin");
exit (1);
}
if(ac > 2) {
if (strcmp(av[1], "stop") == 0 && strcmp(av[2], "A") == 0) {
printf ("Stop: ^%c\n", ttyinfo.c_cc[VSTOP] - 19 + 'A');
}
if (strcmp(av[1], "start") == 0 && strcmp(av[2], "^Q") == 0) {
printf ("Stop: ^%c\n", ttyinfo.c_cc[VSTOP] - 3 + 'A');
}
}
else {
printf("Need two arguments\n");
}
return 0;
}
Read up on strncmp
and strcmp
. In particular make sure you kno why and when strncmp
is preferable over strcmp
.