list.c

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.5 kB
1
Indexable
Never
#include "list.h"

void list_file(struct header *file_header){
    char *verbose_buff;
    char *name;
    long size;

    name = rebuild_name(file_header);

    if(verbose_option == 0){
        printf("%s\n", name);
    }
    else{
        /* Create size */
        char size_field[8];
        size = strtol(file_header->size, NULL, 0);
        sprintf(size_field, "%8o\n", (unsigned int) size);
    }
}

char *rebuild_name(struct header *file_header){
    char *name;
    int i;
    int name_len, prefix_len;

    /* Initiliaze name len and prefix len to zero */
    name_len = 0;
    prefix_len = 0;

    if(file_header->name != NULL){
        name_len = strlen(file_header->name);
    }
    if(file_header->prefix != NULL){
        prefix_len = strlen(file_header->prefix);
    }

    if(name_len == 0 && prefix_len == 0){
        return NULL;
    }
    else if(prefix_len == 0){
        return file_header->name;
    }
    else if(name_len == 0){
        return file_header->prefix;
    }
   else{
        name = malloc(sizeof(char) * (name_len + prefix_len + 2));
        if(name == NULL){
            perror("malloc");
            exit(EXIT_FAILURE);
        }

        /* Copy name over */
        for(i = 0; i < name_len; i++){
            name[i] = file_header->name[i];
        }

        name[name_len] = '/';

        /* Copy prefix over */
        for(i = 0; i < prefix_len; i++){
            name[(name_len + 1) + i] = file_header->prefix[i];
        }

        name[name_len + prefix_len + 1] = '\0';
        return name;
    }
}
Leave a Comment