Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:35:43+00:00 2026-06-18T03:35:43+00:00

I think I have an issue with terminating my strings, because I’m having trouble

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:35:44+00:00Added an answer on June 18, 2026 at 3:35 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having an issue here that I have tried everything I can think of but
I'm having a strange issue that I think I have a workaround for, but
I have an issue that (I think) might have to do with scope, but
I have an issue in firefox and not in IE, didn't think I'd hear
I am have an issue with my scrollview which holds multiple views. I think
I have what I think should be a fairly simple mapping issue, but not
I have a batch script, I think my issue has to do with parentheses
I think I have read every stackoverflow and google result available on this issue
I have a site http://www.ratingscorner.com/colleges i have an issue like performance. I think the
I think I have a CSS issue with my Google Custom Search. The search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.