Question

I think I have an issue with terminating my strings, because I'm having trouble displaying them. I'm trying to recreate the unix ar command in C and I almost have -v laid out, but now that I've got permissions in there, I can't seem to get them printed out correctly.

I've added a termination char to the end of the perms array, which helped to display the permissions, but now the date doesn't want to print.

The date will print ok when I have it in a separate string, but when it's in the combined format string with everything else it disappears.

void verbose(char *archive){
    mode_t mode;
    size_t readNum;
    time_t mtime;
    struct tm * time_box;
    long long arch_size, file_size;
    long uID, gID;
    char header[60], file_name[16], time[21], perms[8];
    char* tokens;
    FILE *fp = fopen(archive, "r");
    if (!fp)
        fail('f');

    //get file size
    fseek(fp, 0, SEEK_END);
    arch_size = ftell(fp);
    rewind(fp);

    //move over ARMAG
    if(fseek(fp, strlen(ARMAG), SEEK_SET) != 0)
        fail ('z');

    //loop over files
    while(ftell(fp) < arch_size -1){
        //reads header into
        readNum = fread(header, 1, sizeof(header), fp);
        if(readNum != sizeof(header))
            fail('r');

        tokens = strtok(header, " ");
        strcpy(file_name, tokens);
        mtime = (time_t)(atol(tokens = strtok(NULL, " ")));
        uID = atol(tokens = strtok(NULL, " "));
        gID = atol(tokens = strtok(NULL, " "));
        mode = (mode_t)(strtoul(tokens = strtok(NULL, " "), NULL, 8));
        file_size = atoll(&header[48]);
        time_box = localtime(&mtime);
        strncpy(time, &asctime(time_box)[4], 20);


        printf("Time is %s\n", time);

        perms[0] = mode & S_IRUSR? 'r' : '-';
        perms[1] = mode & S_IWUSR? 'w' : '-';
        perms[2] = mode & S_IXUSR? 'x' : '-';
        perms[3] = mode & S_IRGRP? 'r' : '-';
        perms[4] = mode & S_IWGRP? 'w' : '-';
        perms[5] = mode & S_IXGRP? 'x' : '-';
        perms[6] = mode & S_IROTH? 'r' : '-';
        perms[7] = mode & S_IWOTH? 'w' : '-';
        perms[8] = mode & S_IXOTH? 'x' : '-';
        perms[9] = '\0';

        printf("%s"
                "\t%ld"
                "/%ld"
                "\t\t%lld "
                "%s "
                "%s\n", perms, uID, gID, file_size, time, file_name);

        //move over file
        if(fseek(fp, file_size, SEEK_CUR) != 0)
            fail ('z');
    }
    fclose(fp);
}

Sample output: (there should be times in the even lines as well)

Time is Jan 27 16:23:59 2013
rw-r--r--   502/20      28 - b.txt
Time is Jan 27 16:24:06 2013
rw-r--r--   502/20      17 - c.txt
Time is Jan 27 16:24:15 2013
rw-r--r--   502/20      28 - d.txt
Time is Jan 27 16:24:06 2013
rw-r--r--   502/20      17 - c.txt
Was it helpful?

Solution

You're allocating perms[8] with a size of 8. So valid indexes for this tab are going to be [0-7].

After you assign perms[8] = mode & S_IXOTH? 'x' : '-'; and perms[9] = '\0'; which are out of boundaries of your perms array.

Your time array must be right after perms in memory. so perms[8] and perms[9] act like you were affecting time[0] = '-' then time[1] = '\0'.

time is now equal to - and printf displays it correctly.

Just correct your perms[8] declaration to perms[10] and you will affect value to the good variable memory part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top