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.
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
المحلول
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.