networking · beginner · ~15 min

Did accept return a connection?

Interpret accept()'s return.

Challenge

Interpret the return value of accept(), which hands back a new fd or -1 on error.

Task

Implement int accept_ok(int rc).

Input

  • rc: the value accept() returned.

Output

Return 1 if rc >= 0 (a valid descriptor for the accepted connection), else 0.

Example

accept_ok(5)   ->  1
accept_ok(-1)  ->  0

Input format

rc: the return value of accept().

Output format

1 if rc >= 0 (valid fd), else 0.

Constraints

accept() returns a new fd (>= 0) on success, -1 on error.

Starter code

int accept_ok(int rc) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.