tools/build-pkg.c
static FileInfo *read_control_file(const char *filename, uint32_t *nfiles_ret)
{
    FileInfo *filelist = NULL;
    uint32_t nfiles = 0;

    FILE *f = fopen(filename, "r");
    if (!f) {
        fprintf(stderr, "fopen(%s): %s\n", filename, strerror(errno));
        goto error_return;
    }

    int line = 0;
    char buf[LINEMAX+2];  // Leave room for \n\0 at the end of the buffer.
    while (fgets(buf, sizeof(buf), f)) {
        line++;
        char *s = buf + strlen(buf);
        if (s > buf && s[-1] == '\n') {
            *--s = 0;
        }
        if (s > buf && s[-1] == '\r') {
            *--s = 0;
        }
        s = buf + strspn(buf, " \t");
        if (!*s || *s == '#') {
            continue;
        }
        if (*s == '=') {
            fprintf(stderr, "%s:%d: Pathname missing\n", filename, line);
            goto error_close_file;
        }

        char *pathname = buf;
        uint32_t flags = 0;
        if (strnicmp(pathname, "deflate:", 8) == 0) {
            flags = PKGF_DEFLATED;
            pathname += 8;
        }

        char *realfile;
        pathname = strtopath(pathname, &s);
        if (!pathname) {
            fprintf(stderr, "%s:%d: Pathname missing or invalid\n",
                    filename, line);
            goto error_close_file;
        }
        s += strspn(s, " \t");
        if (!*s) {
            realfile = pathname;
            pathname = NULL;
        } else {
            if (*s != '=') {
                fprintf(stderr, "%s:%d: Invalid format (unquoted spaces"
                        " not allowed in pathnames)\n", filename, line);
                goto error_close_file;
            }
            realfile = strtopath(s+1, &s);
            if (!realfile) {
                fprintf(stderr, "%s:%d: Real filename missing or invalid\n",
                        filename, line);
                goto error_close_file;
            }
            s += strspn(s, " \t");
            if (*s) {
                fprintf(stderr, "%s:%d: Junk at end of line\n",
                        filename, line);
                goto error_close_file;
            }
        }

        if (!strchr(realfile, '%')) {
            if (!append_one_file(&filelist, &nfiles, pathname, realfile,
                                 flags)) {
                fprintf(stderr, "%s:%d: Error adding file\n", filename, line);
                goto error_close_file;
            }
        } else {
            if (!append_matching_files(&filelist, &nfiles, pathname,
                                       realfile, flags)) {
                fprintf(stderr, "%s:%d: Error adding files\n", filename, line);
                goto error_close_file;
            }
        }
    }

    fclose(f);
    *nfiles_ret = nfiles;
    return filelist;

  error_close_file:
    fclose(f);
  error_return:
    free(filelist);
    return NULL;
}
/**
 * read_control_file:  Read in the control file and generate the input
 * file list.
 *
 * [Parameters]
 *     filename: Control file pathname.
 *     nfiles_ret: Pointer to variable to receive the number of files.
 * [Return value]
 *     Newly-allocated array of FileInfo structures, or NULL on error.
 */
Read in the control file and generate the input
file list.

[Parameters]
filename: Control file pathname.
nfiles_ret: Pointer to variable to receive the number of files.
[Return value]
Newly-allocated array of FileInfo structures, or NULL on error.
Read in the control file and generate the input file list . [ Parameters ] filename : Control file pathname . nfiles_ret : Pointer to variable to receive the number of files . [ Return value ] Newly - allocated array of FileInfo structures or NULL on error .
static FileInfo *read_control_file(const char *filename, uint32_t *nfiles_ret)
{
    FileInfo *filelist = NULL;
    uint32_t nfiles = 0;
    FILE *f = fopen(filename, "r");
    if (!f) {
        fprintf(stderr, "fopen(%s): %s\n", filename, strerror(errno));
        goto error_return;
    }
    int line = 0;
    char buf[LINEMAX+2];  
    while (fgets(buf, sizeof(buf), f)) {
        line++;
        char *s = buf + strlen(buf);
        if (s > buf && s[-1] == '\n') {
            *--s = 0;
        }
        if (s > buf && s[-1] == '\r') {
            *--s = 0;
        }
        s = buf + strspn(buf, " \t");
        if (!*s || *s == '#') {
            continue;
        }
        if (*s == '=') {
            fprintf(stderr, "%s:%d: Pathname missing\n", filename, line);
            goto error_close_file;
        }
        char *pathname = buf;
        uint32_t flags = 0;
        if (strnicmp(pathname, "deflate:", 8) == 0) {
            flags = PKGF_DEFLATED;
            pathname += 8;
        }
        char *realfile;
        pathname = strtopath(pathname, &s);
        if (!pathname) {
            fprintf(stderr, "%s:%d: Pathname missing or invalid\n",
                    filename, line);
            goto error_close_file;
        }
        s += strspn(s, " \t");
        if (!*s) {
            realfile = pathname;
            pathname = NULL;
        } else {
            if (*s != '=') {
                fprintf(stderr, "%s:%d: Invalid format (unquoted spaces"
                        " not allowed in pathnames)\n", filename, line);
                goto error_close_file;
            }
            realfile = strtopath(s+1, &s);
            if (!realfile) {
                fprintf(stderr, "%s:%d: Real filename missing or invalid\n",
                        filename, line);
                goto error_close_file;
            }
            s += strspn(s, " \t");
            if (*s) {
                fprintf(stderr, "%s:%d: Junk at end of line\n",
                        filename, line);
                goto error_close_file;
            }
        }
        if (!strchr(realfile, '%')) {
            if (!append_one_file(&filelist, &nfiles, pathname, realfile,
                                 flags)) {
                fprintf(stderr, "%s:%d: Error adding file\n", filename, line);
                goto error_close_file;
            }
        } else {
            if (!append_matching_files(&filelist, &nfiles, pathname,
                                       realfile, flags)) {
                fprintf(stderr, "%s:%d: Error adding files\n", filename, line);
                goto error_close_file;
            }
        }
    }
    fclose(f);
    *nfiles_ret = nfiles;
    return filelist;
  error_close_file:
    fclose(f);
  error_return:
    free(filelist);
    return NULL;
}
static FileInfo * read_control_file ( const char * filename , uint32_t * nfiles_ret ) { FileInfo * filelist = NULL ; uint32_t nfiles = 0 ; FILE * f = fopen ( filename , " " ) ; if ( ! f ) { fprintf ( stderr , " \n " , filename , strerror ( errno ) ) ; goto error_return ; } int line = 0 ; char buf [ LINEMAX + 2 ] ; while ( fgets ( buf , sizeof ( buf ) , f ) ) { line ++ ; char * s = buf + strlen ( buf ) ; if ( s > buf && s [ -1 ] == ' \n ' ) { * -- s = 0 ; } if ( s > buf && s [ -1 ] == ' \r ' ) { * -- s = 0 ; } s = buf + strspn ( buf , " \t " ) ; if ( ! * s || * s == ' ' ) { continue ; } if ( * s == ' ' ) { fprintf ( stderr , " \n " , filename , line ) ; goto error_close_file ; } char * pathname = buf ; uint32_t flags = 0 ; if ( strnicmp ( pathname , " " , 8 ) == 0 ) { flags = PKGF_DEFLATED ; pathname += 8 ; } char * realfile ; pathname = strtopath ( pathname , & s ) ; if ( ! pathname ) { fprintf ( stderr , " \n " , filename , line ) ; goto error_close_file ; } s += strspn ( s , " \t " ) ; if ( ! * s ) { realfile = pathname ; pathname = NULL ; } else { if ( * s != ' ' ) { fprintf ( stderr , " " " \n " , filename , line ) ; goto error_close_file ; } realfile = strtopath ( s + 1 , & s ) ; if ( ! realfile ) { fprintf ( stderr , " \n " , filename , line ) ; goto error_close_file ; } s += strspn ( s , " \t " ) ; if ( * s ) { fprintf ( stderr , " \n " , filename , line ) ; goto error_close_file ; } } if ( ! strchr ( realfile , ' ' ) ) { if ( ! append_one_file ( & filelist , & nfiles , pathname , realfile , flags ) ) { fprintf ( stderr , " \n " , filename , line ) ; goto error_close_file ; } } else { if ( ! append_matching_files ( & filelist , & nfiles , pathname , realfile , flags ) ) { fprintf ( stderr , " \n " , filename , line ) ; goto error_close_file ; } } } fclose ( f ) ; * nfiles_ret = nfiles ; return filelist ; error_close_file : fclose ( f ) ; error_return : free ( filelist ) ; return NULL ; }
read_control_file:  Read in the control file and generate the input
 file list.
read_control_file : Read in the control file and generate the input file list .
// Leave room for \n\0 at the end of the buffer.