Universität Ulm, SAI, Übungen zu Systemnahe Software I

Lösungsbeispiel zu Blatt 9 (Aufgabe 13): contents

Inhaltsverzeichnis ausgeben.

contents.h

/*
 *      contents.h      - Headerfile for SAM table of contents function.
 *
 *      Martin Hasch, University of Ulm, January 1997.
 */

#include        "names.h"

/*
 *      Print table of archive contents, either complete or
 *      selecting given names.
 *      If any names are not present return ECODE_ERROR, on success
 *      return ECODE_SUCCESS, on serious problems abort program.
 */
int contents(char *archive, names_List list);

contents.c

/*
 *      contents.c      - SAM table of contents function.
 *
 *      Martin Hasch, University of Ulm, January 1997.
 */

#include        <stdio.h>
#include        <string.h>
#include        <time.h>
#include        "sam.h"
#include        "names.h"
#include        "parser.h"
#include        "contents.h"

/*
 *      Display information of given header.
 */
static void display(sam_Header *header)
{
        char buffer[18];
        char *tstr;

        /*
         *      Map ctime format:
         *              "Fri Sep 13 00:00:00 1986\n\0"
         *               ^   ^           ^  ^     ^
         *               0   4          16 19    24
         *
         *      To output format:
         *              "Sep 13 00:00 1986\0"
         *               ^           ^     ^
         *               0          12    17
         */
        tstr = ctime(&header->sam_time);
        (void) strncpy(buffer, tstr+4, 12);
        (void) strncpy(buffer+12, tstr+19, 5);
        buffer[17] = '\0';

        (void) printf("%10d %s %s\n", header->sam_size, buffer,
                header->sam_name);
}

/*
 *      Print table of archive contents, either complete or
 *      selecting given names.
 *      If any names are not present return ECODE_ERROR, on success
 *      return ECODE_SUCCESS, on serious problems abort program.
 */
int contents(char *archive, names_List list)
{
        parser_Locator oldarch;
        sam_Header header;
        names_List thisname;
        int all;

        all = list == NULL;
        parser_openread(archive, &oldarch);
        while ( parser_getheader(oldarch, &header) ) {
                if ( all || (thisname = names_get(list, header.sam_name)) !=
                                NULL ) {
                        display(&header);
                        if ( !all )
                                list = names_delete(list, thisname);
                }
                parser_skipbody(oldarch, &header);
        }
        parser_close(oldarch);

        if ( list != NULL ) {
                names_notfound(list);
                return ECODE_ERROR;
        }
        return ECODE_SUCCESS;
}

<- Alle Module
Martin Hasch, Februar 1997