title: C stdin does not work
body: <p>I trying to fix some bugs in a legacy code. This is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;sys/msg.h&gt;
  
  #include "libSvr.h"
  
  #define CONEC 3
  #define CONFIG 4
  
  typedef struct {
      long type;
      int cmd;
      int id;
      char data[7000];
  } msg;
  
  typedef struct {
      int id[100];
      unsigned long ip[100];
      int type[100];
      char name[100][20];
      int num;
  } conec;
  
  typedef struct {
      char ip;
      char svr;
  } param_cfg;
  
  typedef struct {
      unsigned long my_ip;
      unsigned long svr_ip;
  } tcp_cfg;
  
  msg client, server;
  conec conec_tab;
  
  param_cfg param;
  tcp_cfg setup;
  
  void *Receive_MSG(void *arg) {
      int i;
      while (1) {
          if ( msgrcv(msgkey,&amp;client,7008,2,0) != -1 ) {
              switch (client.cmd) {
                  case CONEC:
                      memmove((char*)&amp;conec_tab,client.data,sizeof(conec_tab));
                      printf("\n%d\n",conec_tab.num);
                      break;
                  case CONFIG:
                      switch (conec_tab.type[client.id]) {
                          case 0:
                              memmove((char*)&amp;param,&amp;client.data[sizeof(param_cfg)],sizeof(param_cfg));
                              printf("------------PARAM Config------------\n");
                              printf("%c\n%c\n\n",param.ip,param.svr);
                              break;
                          case 1:
                              memmove((char*)&amp;setup,client.data,sizeof(tcp_cfg));
                              printf("\n------------Setup------------\n");
                              printf("%s\n",Inet_ntoa(setup.my_ip));
                              break;
                      }
              }
          }
      }
  }
  
  void send_msg() {
      msgsnd(msgkey,&amp;(server.type),7008,0);
  }
  
  void Process(char *command) {
      server.type = 1;
  
      if ( !strncmp(command,"text",8) ) {
          server.cmd = 1;
          send_text();
          send_msg();
      }
      else if ( !strncmp(command,"image",9) ) {
          server.cmd = 2;
          send_image();
          send_msg();
      }
      else
          printf("\nCommand not found.\n");
  }
  
  int main()
  {
      int i;
      char cmd[100];
      msgkey=msgget(100,0);
  
      if ( msgkey == -1) {
          printf("ERROR msgkey!!");
          exit(1);
      }
  
      pthread_t receive;
      pthread_create(&amp;receive,NULL,Receive_MSG,NULL);
  
      do {
          printf("Enter a command -&gt; ");
  
          if (fgets(cmd,100,stdin) == NULL) {
              printf("error");
              exit(1);
          }
  
          cmd[strlen(cmd)-1]='\0';
          if ( (strcmp(cmd,"exit")) &amp;&amp; (strcmp(cmd,"")) )
              Process(cmd);
  
      } while (strcmp(cmd,"exit"));
  
      return 0; 
  }
  </code></pre>
  
  <p>The <code>libSvr.h</code> code:</p>
  
  <pre><code>#ifndef LIBSVR_H
  #define LIBSVR_H
  
  #pragma pack(push,1)
  #pragma pack(1)
  
  
  extern "C" void send_text();
  
  extern "C" void send_image();
  
  
  #pragma pack(pop)
  
  #endif
  </code></pre>
  
  <p>The thread <code>receive</code> just receive some messages from another application in the queue id <code>msgkey</code> and print some text.</p>
  
  <p>The problem is when I get into the do while loop, where I need to get some user inputs, the <code>fgets</code> fails... When I printed the error (with <code>errno</code>) I saw that it is a bad file descriptor error (<code>EBADF</code>).</p>
  
  <p>The <code>Process</code> function get the command a calls the appropriate function, that basically will send some data through a socket. And these functions called by <code>Process</code> are from a lib (<code>libSvr</code>) writen in C++ with functions C extern-ed.</p>
  
  <p>I'm using an ubuntu 12.10 machine with gcc 4.7.2.</p>
  
  <p>I tried to see the code after preprocessor (with gcc -E) and <code>stdin</code> is still showed as <code>stdin</code>. I tried to print the output of <code>fileno(stdin)</code> and it was 0 (I saw somewhere that it the right value) and then I tried to call <code>fgets</code> this way: <code>fgets(cmd,100,0)</code>. But nothing works.</p>
  
  <p>Anyone have any idea of what is happening?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Included all the code.</p>
  
  <p>I tried call <code>gets()</code>, <code>scanf()</code> and <code>read()</code> and also comment the thread creation (<code>pthread_create</code>) but nothing changed.</p>
  
  <p>I ran the command <code>strace -s2000 -etrace=open,close &lt;MYPROGRAM&gt;</code> and this is the output: </p>
  
  <pre><code>open("/mnt/hgfs/projects/tls/i686/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/tls/i686/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/tls/i686/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/tls/i686/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/tls/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/tls/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/tls/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/tls/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/i686/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/i686/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/i686/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/i686/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/mnt/hgfs/projects/libSvr.so.1", O_RDONLY|O_CLOEXEC) = 3
  close(3)                                = 0
  open("/mnt/hgfs/projects/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
  close(3)                                = 0
  open("/lib/i386-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
  close(3)                                = 0
  open("/mnt/hgfs/projects/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/lib/i386-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
  close(3)                                = 0
  open("/mnt/hgfs/projects/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/usr/lib/i386-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
  close(3)                                = 0
  open("/mnt/hgfs/projects/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/lib/i386-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
  close(3)                                = 0
  open("/mnt/hgfs/projects/libm.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
  open("/lib/i386-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
  close(3)                                = 0
  close(0)                                = 0
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  close(0)                                = -1 EBADF (Bad file descriptor)
  
  Enter a command -&gt; 
  errorEnter a command -&gt; 
  errorEnter a command -&gt; 
  errorEnter a command -&gt; 
  </code></pre>
  
  <p>So, the <code>stdin</code> file descriptor is beeing closed but and now I need to know where it is beeing closed.</p>
  
  <p>I'm studying some howto's and the man page of strace now, because I never use it before.</p>
  
  <p>Any help and tips will be welcome.</p>
  
  <p>Thanks again!</p>