>
> Here's my 'quick' way of implementing the ability to break lines...
>

I modified your code slightly.  My rendition is attached for interested 
parties.

> There are a few problems with this code snippet: 1) a large amount of code
> repetition, 

Repetiton is not always a no-no.  It depends on the case.  IMHO as a 
developer this small amount of code repetiton is neither hard to understand, 
or large enough to quantify separate methods.  The extra stack calls lose the 
efficiency of the tighly written code.

I would leave it as is.

> 2) nether the \ nor the \n are removed. (note on #2: it seems
> that most implementations of SH (including BASH) will not care about that,
> and will just process it anyway...)

I did make that adjustment in mine.

Andy
char * get_file_ent (FILE * fd)
{
    char * ret;
    char * buf;
    int cnt = 0;
    
    // allocate the read buffer
    if ((buf = malloc(16384)) == 0)
    {
        fprintf(stderr, "Memory allocation error.\n");
        return 0;
    }

    // get a line from the file
    if ((fgets(buf, 16384, fd)) == 0)
    {
            fprintf(stderr, "No data read from file.\n");
            return 0;
    }

    // while the current line ends in a backslash
    while (buf[strlen(buf) - 1] == '\\')
    {
        len = strlen(buf);

        // we either start a new line, or continue a multiple line 
        if (cnt == 0)
        {
            // a new line, copy in the buffer
            if ((ret = malloc(len + 1)) == 0)
            {
                fprintf(stderr, "Memory allocation error.\n");
                return 0;
            }
            strncpy(ret, buf, len);
        } 
        else 
        {
            // continuing a line, append the buffer
            if ((ret = realloc(ret, cnt + len + 1)) == 0)
            {
                fprintf(stderr, "Memory allocation error.\n");
                return 0;
            }
            strncat(ret, buf, len);
        }

        // either way, we don't want the slash any longer
        *(ret[cnt + len - 1]) = '\0';

        // increase our count and free the buffer
        cnt += strlen(buf);
        free(buf);

        // allocate the buffer
        if ((buf = malloc(16384)) == 0)
        {
                fprintf(stderr, "Memory allocation error.\n");
                return 0;
        }

        // read the next line from the file
        if ((fgets(buf, 16384, fd)) == 0)
        {
                fprintf(stderr, "File error.\n");
                return 0;
        }
    }

    // we have one last line to do
    if (cnt == 0)
    {
        if ((ret = malloc(len + 1)) == 0)
        {
            fprintf(stderr, "Memory allocation error.\n");
            return 0;
        }
        strncpy(ret, buf, len);
    } 
    else 
    {
        if ((ret = realloc(ret, cnt + len + 1)) == 0)
        {
            fprintf(stderr, "Memory allocation error.\n");
            return 0;
        }
        strncat(ret, buf, len);
    }

    // free the buffer and return the string
    free(buf);
    return ret;
}

Reply via email to