STCP/server.c
static int
process_line(int sd, char *line)
{
    char resp[5000];
    int fd = -1, length;

    if (!*line || access(line, R_OK) < 0)
    {
        sprintf(resp, "%s,-1,File does not exist or access denied\r\n", line);
    }
    else
    {
        if ((fd = open(line, O_RDONLY)) < 0)
        {
            sprintf(resp, "%s,-1,File could not be opened\r\n", line);
        }
        else
        {
            sprintf(resp, "%s,%lu,Ok\r\n", line, lseek(fd, 0, SEEK_END));
            lseek(fd, 0, SEEK_SET);
        }
    }
  /** fprintf(stderr, "sending to client: %s of length %d bytes\n", resp, strlen(resp)); **/
    /* Return the response to the client */
    if (mywrite(sd, resp, strlen(resp)) < 0)
    {
        if (fd != -1)
            close(fd);
        return -1;
    }

    if (fd == -1)
        return 0;

    for (;;)
    {
        length = read(fd, resp, sizeof(resp));
        if (length == 0)
            break;

        if (length == -1)
        {
            perror("read");
            close(fd);
            return -1;
        }

        /* fwrite(resp, length, 1, stdout); */

        if (mywrite(sd, resp, length) < 0)
        {
            close(fd);
            return -1;
        }
    }

    close(fd);
    return 0;
}
/**********************************************************************/
/* process_line
 * 
 * Process the request (a filename) from the client. Send back the
 * response and then the content of the requested file through
 * mysocket layer.
 *
 * Returns 
 *  0 on success
 *  -1 on failure
 */


Process the request (a filename) from the client. Send back the
response and then the content of the requested file through
mysocket layer.

Returns
0 on success
-1 on failure
Process the request ( a filename ) from the client . Send back the response and then the content of the requested file through mysocket layer . Returns 0 on success - 1 on failure
static int
process_line(int sd, char *line)
{
    char resp[5000];
    int fd = -1, length;
    if (!*line || access(line, R_OK) < 0)
    {
        sprintf(resp, "%s,-1,File does not exist or access denied\r\n", line);
    }
    else
    {
        if ((fd = open(line, O_RDONLY)) < 0)
        {
            sprintf(resp, "%s,-1,File could not be opened\r\n", line);
        }
        else
        {
            sprintf(resp, "%s,%lu,Ok\r\n", line, lseek(fd, 0, SEEK_END));
            lseek(fd, 0, SEEK_SET);
        }
    }
    if (mywrite(sd, resp, strlen(resp)) < 0)
    {
        if (fd != -1)
            close(fd);
        return -1;
    }
    if (fd == -1)
        return 0;
    for (;;)
    {
        length = read(fd, resp, sizeof(resp));
        if (length == 0)
            break;
        if (length == -1)
        {
            perror("read");
            close(fd);
            return -1;
        }
        if (mywrite(sd, resp, length) < 0)
        {
            close(fd);
            return -1;
        }
    }
    close(fd);
    return 0;
}
static int process_line ( int sd , char * line ) { char resp [ 5000 ] ; int fd = -1 , length ; if ( ! * line || access ( line , R_OK ) < 0 ) { sprintf ( resp , " \r \n " , line ) ; } else { if ( ( fd = open ( line , O_RDONLY ) ) < 0 ) { sprintf ( resp , " \r \n " , line ) ; } else { sprintf ( resp , " \r \n " , line , lseek ( fd , 0 , SEEK_END ) ) ; lseek ( fd , 0 , SEEK_SET ) ; } } if ( mywrite ( sd , resp , strlen ( resp ) ) < 0 ) { if ( fd != -1 ) close ( fd ) ; return -1 ; } if ( fd == -1 ) return 0 ; for ( ; ; ) { length = read ( fd , resp , sizeof ( resp ) ) ; if ( length == 0 ) break ; if ( length == -1 ) { perror ( " " ) ; close ( fd ) ; return -1 ; } if ( mywrite ( sd , resp , length ) < 0 ) { close ( fd ) ; return -1 ; } } close ( fd ) ; return 0 ; }
process_line
process_line
/** fprintf(stderr, "sending to client: %s of length %d bytes\n", resp, strlen(resp)); **/ /* Return the response to the client */ /* fwrite(resp, length, 1, stdout); */