我想我在终止我的字符串时遇到了问题,因为我无法显示它们。我正在尝试在 C 中重新创建 unix ar 命令,并且几乎已经布置了 -v,但是现在我已经获得了权限,我似乎无法正确打印它们。
我在 perms 数组的末尾添加了一个终止字符,这有助于显示权限,但现在不想打印日期。
当我把它放在一个单独的字符串中时,日期会打印出来,但是当它与其他所有东西在组合格式字符串中时,它就会消失。
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);
}
示例输出:(偶数行也应该有时间)
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