fgets without the line return What do you think of the following function to offer an alternative to fgets, looking for insights about portability, safety, readability and performance:
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;

int sc_gets(char *buf, int n)
{
    int count = 0;
    char c;

    if (__glibc_unlikely(n &lt;= 0))
        return -1;

    while (--n &amp;&amp; (c = fgetc(stdin)) != '\n')
        buf[count++] = c;
    buf[count] = '\0';

    return (count != 0 || errno != EAGAIN) ? count : -1;
}

#define BUFF_SIZE 10

int main (void) {
    char buff[BUFF_SIZE];

    sc_gets(buff, sizeof(buff)); // how to use
    printf (&quot;%s\n&quot;, buff);

    return 0;
}

NB: I have already read fgets() alternative which was interesting but my code is different.

Second version thanks to the comments and vnp's insight:

#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;

long sc_gets(char *buf, long n)
{
    long count = 0;
    char c;

    while (--n &amp;&amp; (c = fgetc(stdin)) != '\n') {
        if (c == EOF)
            break;
        buf[count++] = c;
    }
    buf[count] = '\0';

    return (count != 0 || errno != EAGAIN) ? count : -1;
}

#define BUFF_SIZE 1024

int main (void) {
    char buff[BUFF_SIZE];
    sc_gets(buff, sizeof(buff));
    printf (&quot;%s\n&quot;, buff);

    return 0;
}
The function does not handle end-of-file correctly. stdin is just a stream, and could be closed like any other stream. For example, type Ctrl-D. Or
  echo -n a | ./a.out
  a????????

Keep in mind that reading beyond end-of-file doesn't set errno.

There is no reason to special-case count == 0.
An IO error doesn't break the loop. If it happened, all the subsequent invocations of fgets would return EOF, and upon the loop termination the count will be n.

Pass the buffer size as size_t. This is what sizeof yields, and the buffers could be large enough for their size to overflow an integer.