我正在尝试在 C 中构建 ar unix 命令的一个版本。我正在处理详细的输出,并且已经涵盖了大部分内容。但是,当我通读文件并将文件名放在格式字符串中时,会不断弹出一个秘密的新行。
void verbose(char *archive){
printf("\nI'm in -v!\n");
int 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];
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 = atoi(tokens = strtok(NULL, " "));
file_size = atoll(&header[48]);
time_box = localtime(&mtime);
printf("%d"
"\t%ld"
"/%ld"
"\t\t%lld "
"%s "
"%s\n", mode, uID, gID, file_size, &asctime(time_box)[4], file_name);
//move over file
if(fseek(fp, file_size, SEEK_CUR) != 0)
fail ('z');
}
fclose(fp);
}
void delete(){
printf("\nI'm in -d!\n");
}
此代码产生如下输出:
I'm in -v!
100644 502/20 28 Jan 27 16:23:59 2013
b.txt
100644 502/20 17 Jan 27 16:24:06 2013
c.txt
100644 502/20 28 Jan 27 16:24:15 2013
d.txt
100644 502/20 17 Jan 27 16:24:06 2013
c.txt
我无法弄清楚为什么文件名在新行上。这太痛苦了。我想也许我没有正确寻找,但如果我向上移动 1 个字节,它会开始切断名称,所以我认为不是这样。
另外,奖金,如果有人知道将八进制转换为unix文件权限的方法,我会很兴奋(即-rw-rw-rw)