STCP/client.c
void
loop_until_end(int sd)
{
    int errcnd;
    char line[1000];
    int length, to_read;
    char *pline, *lenstr, *resp;
    int got;
    FILE *file;

    for (;;)
    {

        errcnd = 0;
        if (filename == NULL)
        {
            /* Prompt for a request to the server (a filename) */
            printf("\nclient> ");
            fflush(stdout);
            if (!fgets(line, sizeof(line), stdin))
                break;

            /* Remove trailing spaces and add CRLF at the end */
            pline = line + strlen(line) - 1;
            while ((pline > line - 1) && isspace((int) (*pline)))
                --pline;

            if (pline <= line)
                continue;
        }
        else
        {
            strcpy(line, filename);
            pline = line + strlen(line) - 1;
        }
        *++pline = '\r';
        *++pline = '\n';
        *++pline = '\0';

        if (mywrite(sd, line, pline - line) < 0)
        {
            perror("mywrite");
            errcnd = 1;
            break;
        }

        if (get_nvt_line(sd, line) < 0)
        {
            perror("get_nvt_line");
            errcnd = 1;
            break;
        }

        printf("server: %s\n", line);
        fflush(stdout);

        /* Parse the response from the server */
        if (NULL == (resp = strrchr(line, ',')))
        {
            fprintf(stderr, "Malformed response from server.\n");
            errcnd = 1;
            break;
        }
        *resp++ = '\0';

        if (NULL == (lenstr = strrchr(line, ',')))
        {
            fprintf(stderr, "Malformed response from server.\n");
            errcnd = 1;
            break;
        }
        *lenstr++ = '\0';


        sscanf(lenstr, "%d", &length);
        if (length == -1)
        {
            /* Error reported from server */
            if (filename == NULL)
                continue;
            else
            {
                errcnd = 1;
                break;
            }

        }
        if ((file = fopen(RCVD_FILENAME, "w")) == NULL)
        {
            perror("file_to_write error");
            errcnd = 1;
            break;
        }
        /* Retrieve the remote file and write it to a local file */
        while (length)
        {
            to_read = MIN(length, (int) sizeof(line));

            if ((got = myread(sd, line, to_read)) < 0)
            {
                perror("myread");
                errcnd = 1;
                break;
            }

            if (!got)
            {
                break;
            }

            if (got < to_read)
            {
                to_read = got;
            }

            if (!quiet_opt)
            {
                while (0 == fwrite(line, 1, to_read, file))
                {
                    if (errno != EINTR)
                    {
                        perror("fwrite");
                        errcnd = 1;
                        break;
                    }
                }
            }
            length -= to_read;
        }

        if (length)
        {
            fprintf(stderr,
                    "Exiting: read bad number of bytes (%d less than expected)...\n",
                    length);
            fclose(file);
            myclose(sd);
            exit(-1);
        }

        fclose(file);
        if (filename != NULL)
            break;

    }                           /* end for(;;) */

    if (errcnd) fprintf(stderr, "An error occurred\n");
}
/**********************************************************************/
/* loop_until_end
 * 
 * Loop until the connection has closed
 */


Loop until the connection has closed
Loop until the connection has closed
void
loop_until_end(int sd)
{
    int errcnd;
    char line[1000];
    int length, to_read;
    char *pline, *lenstr, *resp;
    int got;
    FILE *file;
    for (;;)
    {
        errcnd = 0;
        if (filename == NULL)
        {
            printf("\nclient> ");
            fflush(stdout);
            if (!fgets(line, sizeof(line), stdin))
                break;
            pline = line + strlen(line) - 1;
            while ((pline > line - 1) && isspace((int) (*pline)))
                --pline;
            if (pline <= line)
                continue;
        }
        else
        {
            strcpy(line, filename);
            pline = line + strlen(line) - 1;
        }
        *++pline = '\r';
        *++pline = '\n';
        *++pline = '\0';
        if (mywrite(sd, line, pline - line) < 0)
        {
            perror("mywrite");
            errcnd = 1;
            break;
        }
        if (get_nvt_line(sd, line) < 0)
        {
            perror("get_nvt_line");
            errcnd = 1;
            break;
        }
        printf("server: %s\n", line);
        fflush(stdout);
        if (NULL == (resp = strrchr(line, ',')))
        {
            fprintf(stderr, "Malformed response from server.\n");
            errcnd = 1;
            break;
        }
        *resp++ = '\0';
        if (NULL == (lenstr = strrchr(line, ',')))
        {
            fprintf(stderr, "Malformed response from server.\n");
            errcnd = 1;
            break;
        }
        *lenstr++ = '\0';
        sscanf(lenstr, "%d", &length);
        if (length == -1)
        {
            if (filename == NULL)
                continue;
            else
            {
                errcnd = 1;
                break;
            }
        }
        if ((file = fopen(RCVD_FILENAME, "w")) == NULL)
        {
            perror("file_to_write error");
            errcnd = 1;
            break;
        }
        while (length)
        {
            to_read = MIN(length, (int) sizeof(line));
            if ((got = myread(sd, line, to_read)) < 0)
            {
                perror("myread");
                errcnd = 1;
                break;
            }
            if (!got)
            {
                break;
            }
            if (got < to_read)
            {
                to_read = got;
            }
            if (!quiet_opt)
            {
                while (0 == fwrite(line, 1, to_read, file))
                {
                    if (errno != EINTR)
                    {
                        perror("fwrite");
                        errcnd = 1;
                        break;
                    }
                }
            }
            length -= to_read;
        }
        if (length)
        {
            fprintf(stderr,
                    "Exiting: read bad number of bytes (%d less than expected)...\n",
                    length);
            fclose(file);
            myclose(sd);
            exit(-1);
        }
        fclose(file);
        if (filename != NULL)
            break;
    }                           
    if (errcnd) fprintf(stderr, "An error occurred\n");
}
void loop_until_end ( int sd ) { int errcnd ; char line [ 1000 ] ; int length , to_read ; char * pline , * lenstr , * resp ; int got ; FILE * file ; for ( ; ; ) { errcnd = 0 ; if ( filename == NULL ) { printf ( " \n " ) ; fflush ( stdout ) ; if ( ! fgets ( line , sizeof ( line ) , stdin ) ) break ; pline = line + strlen ( line ) - 1 ; while ( ( pline > line - 1 ) && isspace ( ( int ) ( * pline ) ) ) -- pline ; if ( pline <= line ) continue ; } else { strcpy ( line , filename ) ; pline = line + strlen ( line ) - 1 ; } * ++ pline = ' \r ' ; * ++ pline = ' \n ' ; * ++ pline = ' \0 ' ; if ( mywrite ( sd , line , pline - line ) < 0 ) { perror ( " " ) ; errcnd = 1 ; break ; } if ( get_nvt_line ( sd , line ) < 0 ) { perror ( " " ) ; errcnd = 1 ; break ; } printf ( " \n " , line ) ; fflush ( stdout ) ; if ( NULL == ( resp = strrchr ( line , ' ' ) ) ) { fprintf ( stderr , " \n " ) ; errcnd = 1 ; break ; } * resp ++ = ' \0 ' ; if ( NULL == ( lenstr = strrchr ( line , ' ' ) ) ) { fprintf ( stderr , " \n " ) ; errcnd = 1 ; break ; } * lenstr ++ = ' \0 ' ; sscanf ( lenstr , " " , & length ) ; if ( length == -1 ) { if ( filename == NULL ) continue ; else { errcnd = 1 ; break ; } } if ( ( file = fopen ( RCVD_FILENAME , " " ) ) == NULL ) { perror ( " " ) ; errcnd = 1 ; break ; } while ( length ) { to_read = MIN ( length , ( int ) sizeof ( line ) ) ; if ( ( got = myread ( sd , line , to_read ) ) < 0 ) { perror ( " " ) ; errcnd = 1 ; break ; } if ( ! got ) { break ; } if ( got < to_read ) { to_read = got ; } if ( ! quiet_opt ) { while ( 0 == fwrite ( line , 1 , to_read , file ) ) { if ( errno != EINTR ) { perror ( " " ) ; errcnd = 1 ; break ; } } } length -= to_read ; } if ( length ) { fprintf ( stderr , " \n " , length ) ; fclose ( file ) ; myclose ( sd ) ; exit ( -1 ) ; } fclose ( file ) ; if ( filename != NULL ) break ; } if ( errcnd ) fprintf ( stderr , " \n " ) ; }
loop_until_end
loop_until_end
/* Prompt for a request to the server (a filename) */ /* Remove trailing spaces and add CRLF at the end */ /* Parse the response from the server */ /* Error reported from server */ /* Retrieve the remote file and write it to a local file */ /* end for(;;) */