-2
#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 (av[1] == "stop" && av[2] == "A") {
    printf ("Stop: ^%c\n", ttyinfo.c_cc[VSTOP] - 19 + 'A');
  }
  if (av[1] == "start" && av[2] == "^Q") {
    printf ("Stop: ^%c\n", ttyinfo.c_cc[VSTOP] - 3 + 'A');
  }
  return 0;
}

I'm learning Linux, and this code is written in C. Using command line to display character change. For example: ./example stop A. However, it doesn't show anything on screen.

Harry
  • 11,298
  • 1
  • 29
  • 43
Fed
  • 7
  • It don't `print` because you use [`strncmp`](http://en.cppreference.com/w/c/string/byte/strncmp) to test for equality (not `==`). – Elliott Frisch May 17 '16 at 00:40
  • Also even after you fix the string compares, you program will print nothing if you don't pass it one of the two combinations of arguments that it's looking for (so it might not be unusual for it to print nothing at all). Finally, it will behave unpredictably - probably crash - if fewer than two arguments are passed to the program. – Michael Burr May 17 '16 at 00:51
  • `==` on strings just compares their address, so in your case the comparison will fail. You need to call `strcmp` to compare the actual strings. – Tom Karzes May 17 '16 at 01:02

1 Answers1

3

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.

Harry
  • 11,298
  • 1
  • 29
  • 43
  • On what platform is `gcc` an alias for `clang`? – Elliott Frisch May 17 '16 at 02:24
  • @ElliottFrisch The mac. If you want to run the real gcc you need to specify it. On my system it's `gcc-5`. [OS X 10.9 gcc links to clang](http://stackoverflow.com/questions/19535422/os-x-10-9-gcc-links-to-clang). I use both because you do see some interesting differences in implementation ie different error messages and different optimizations are the two most notable. – Harry May 17 '16 at 03:38