title: fgets() does not return NULL on empty string
body: <p>I recently tried to use <code>fgets()</code> instead of <code>scanf()</code> to read a string for code security reasons. I used a simple function that I found here to check for errors (no input and too long input). The problem is that whenever i press "ENTER" without actually writing anything, <code>fgets()</code> doesn't return <code>NULL</code> and my program is not able to show the <code>NO_INPUT</code> error.</p>
  
  <p>Here's <code>main.cpp</code>:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  #include "utilities.h"
  
  int main() {
      int rc;
      char str[20];
  
      rc = getLine("Enter string: ", str, sizeof(str));
      if(rc == NO_INPUT) {
          printf("[ERROR] No input\n\n");
      } else if(rc == TOO_LONG) {
          printf("[ERROR] Input is too long\n\n");
      } else {
          printf("This is your input: \"%s\"\n\n", str);
      }
  
      system("pause");
  }
  </code></pre>
  
  <p>Here's <code>utilities.h</code>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define OK       0
  #define NO_INPUT 1
  #define TOO_LONG 2
  
  int getLine(const char *msg, char *buff, size_t len) {  
      if(msg != NULL) {
          printf("%s", msg);
          fflush(stdout);
      }
  
      if(fgets(buff, len, stdin) == NULL /*[+]*/ || strcmp(buff, "\n") == 0) {
          //[-] fflush(stdin);
          return NO_INPUT;
      } else if(buff[strlen(buff)-1] != '\n') {
          //[-] fflush(stdin);
          return TOO_LONG;
      } else {
          buff[strlen(buff)-1] = '\0';
          //[-] fflush(stdin);
          return OK;
      }
  }
  </code></pre>
  
  <p>And here's the output:</p>
  
  <pre>Enter string:
  This is your input: ""
  
  Press any key to continue . . . </pre>
  
  <hr>
  
  <p>I solved my problem by replacing the first <code>if</code> statement in <code>utilities.h</code> with <code>if(fgets(buff, len, stdin) == NULL || strcmp(buff, "\n") == 0)</code>. Doing this, my program will check for input errors OR for empty strings and return the <code>NO_INPUT</code> flag.</p>
  
  <hr>
  
  <p>Thanks to everybody who commented and @Peter for answering. I added the aforementioned <code>if</code> statement in <code>utilities.h</code> and removed every <code>fflush(stdin)</code> occurrence. The code now looks as above.</p>