列出图像文件中的目录内容:跳过。和..并标记目录

时间:2015-03-08 22:25:15

标签: c directory filesystems ext2

读取指定为命令的磁盘映像的C程序 line参数并打印所有文件和目录的路径 如果条目是目录,则在末尾打印正斜杠 路径....这是我在程序中的问题,我完成了以下代码....

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>

#define K 1024

typedef struct dir {
unsigned int my_node;
unsigned short my_length;
unsigned char my_namelen;
}DIRPATH;



unsigned int getint(int fd, int block, int byte);
unsigned short int getshort(int fd, int block, int byte);


int main (int argc , char *argv[])
{
int i;
int nameleng;
char name[K];
DIRPATH dh;
int argument;
int fd;

argument = (argc > 1) ? atoi(argv[1]) : 0;
fd=open(argv[1],O_RDONLY);
fprintf(stderr,"fd=%d\n",fd);
if(lseek(fd,K,SEEK_SET)<0)
{
fprintf(stderr,"unable to seek");
exit(0);
}
int itab = getint(fd,2,8);
unsigned int first_block = getint(fd,itab,inode_size + 40);
int length=0;
for(i=0;i<6;i++)
{
lseek(fd,first_block * K + length,SEEK_SET);
read(fd,&dh,sizeof(DIRPATH));
length += dh.my_length;
printf("",dh.my_namelen);
read(fd,name,dh.my_namelen);
name[dh.my_namelen] = 0;
printf("%s\n",name);
}
close(fd);
}

unsigned int getint(int fd, int block, int byte)
{
unsigned int x;
int check;
lseek(fd,block * K + byte, SEEK_SET);
check = read(fd, &x, 4);
if(check != 4){
fprintf(stderr,"error to read %d %d \n",block, byte);
exit(0);
}
return(x);
}

unsigned short int getshort(int fd, int block, int byte)
{
unsigned short int x;
int check;
lseek(fd,block * K + byte, SEEK_SET);
check = read(fd, &x, 4);
if(check != 2){
fprintf(stderr,"error to read %d %d \n",block, byte);
exit(0);
}
return(x);
}

我正在获取输出,但在输出中它提供...。我必须删除这些点并将/添加到目录而不是文件。

0 个答案:

没有答案