myls.c

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main(int argc, char *argv[])
{
        DIR *dp;
        struct dirent *dirp;

        if(argc != 2){
                printf("usage: ls directory_name\n");
                exit(1);
        }
        if((dp = opendir(argv[1])) == NULL){
                printf("can't open %s\n",argv[1]);
                exit(1);
        }
        while((dirp = readdir(dp)) != NULL)
                printf("d_name: %s\n",dirp->d_name);

        closedir(dp);
        return 0;
}

DIR结构体的定义如下:

<pre class="plain" name="code">struct __dirstream
{
	void *__fd;
	char *__data;
	int __entry_data;
	char *ptr;
	int __entry_ptr;
	size_t __allocation;
	size_t __size;
	__lib_lock_define (, __lock)
};

typedef struct __dirstream DIR;

DIR结构体类似于FILE,是一个内部结构体。

dirent结构体的定义如下:

struct dirent
{
	long d_ino;  //索引节点号
	off_t d_off; //在目录文件中的偏移
	unsigned short d_reclen; //文件名的长度
	unsigned char d_type; //文件类型
	char d_name[NAME_MAX + 1]; // 文件名,最长255字符
};

opendir函数:

函数 DIR *opendir(const char *pathname)   	//打开一个目录,成功返回的指向DIR的结构体的指针。失败返回NULL。该指针有以下几个函数使用:

struct dirent *readdir(DIR *dp);  	//读取目录,成功返回参数目录流的下个目录进入点,失败返回NULL

void rewinddir(DIR *dp);   //用来设置参数dp目录流目前读取位置为原来开头的读取位置

int closedir(DIR *dp);		// 关闭参数dp所指的目录流

long telldir(DIR *dp);     //  返回参数dp目录流当前的读取位置,此返回值代表距离目录文件开头的偏移量返回值返回下个读取位置,有错误发生时返回-1

void seekdir(DIR *dp, long loc);  //设置参数dp目录流的读取位置。参数loc代表距离目录文件开头的偏移量

时间: 2024-07-29 01:30:12

myls.c的相关文章

UC高级编程,实现myls程序

跟着达内视频,学习UC高级编程,完成程序小练习. 主要练习的函数为:  int lstat(const char *path, struct stat *buf);  size_t strftime(char *s, size_t max, const char *format,  const struct tm *tm);此函数, 第一次使用. time_t mktime(struct tm *tm);//把分离的时间合成整数,写项目代码中,当时自己实现了这个函数功能. #include <s

【转】APUE学习1:迈出第一步,编译myls.c

原文网址:http://blog.csdn.net/sddzycnqjn/article/details/7252444 注:以下写作风格均学习自潘云登前辈 /******************************************************************/   By:             聂强 Date:          2012-2-12 Email:         [email protected] Homepage: http://blog.c

myls

/* 尝试实现ls命令的功能 加选项-l -a -i -h */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include "mystat.h" #include "dir_reg.h" #include "readblocks.h" #include "chec

简单,不易于使用的myls

main.c 文件 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <glob.h> 5 #include <error.h> 6 #include <dirent.h> 7 #include "myls.h" 8 9 void stat_attbute(struct stat *buf); 10 int is_

UC高级编程--实现myls程序

跟着达内视频,学习UC高级编程,完毕程序小练习. 主要练习的函数为:  int lstat(const char *path, struct stat *buf);  size_t strftime(char *s, size_t max, const char *format,  const struct tm *tm);此函数, 第一次使用. time_t mktime(struct tm *tm);//把分离的时间合成整数,写项目代码中,当时自己实现了这个函数功能. #include <s

C#初识

初识C#程序结构 一.编写第一个C#程序.注释: 1.编写程序 static void Main(string[] args)//在Mian方法下编写代码输出数据 { Console.WriteLine("Hello World!");//输出"Hello World!" Console.ReadLine();//暂停 } 2.注释 C#程序结构分为注释.命名空间.类.Main方法.标示符及关键字.语句. 注释:行注释都是以"//"开头 段注释引

APUE fig 1.10示例代码的完善--对提示符及输入回车的优化

APUE 第3版第15页的shell2.c示例程序,运行效果如下: 1 gcc ol.shell.c -o origin_shell 2 3 ./origin_shell 4 % date 5 2015年12月13日 星期日 22时04分48秒 CST 6 % ^Cinterrupt 7 ls 8 Makefile getcputc.c mycat.c myls_info.c note.md origin_shell shell2.c uidgid.c 9 a.out hello.c myls.

管理目录与文件(1-4)

一.内核官网 linux内核的官方网站http://www.kernel.org/ 二.切换界面 图形界面-命令行界面    Ctrl+Alt+Fn(F1.F2.F3.F4.F5.F6) 命令行界面-图形界面    Alt+F7 字符-字符        Alt+Fn(F1.F2.F3.F4.F5.F6) 三.常见目录作用 /boot:存放启动linux系统所必需的文件,包括内核文件.启动菜单配置文件等 /bin:存放linux系统中最基本的用户命令,普通用户都有权限执行 /dev:存放linu

Linux&amp;C编程之Linux系统命令“ls -l”的简单实现

一.基础知识: 1.获取文件详细信息的函数: (1).获取文件信息的函数: #include<sys/stat.h> int stat(const char * path,struct stat * buf);/*将path参数(文件或目录)的文件信息写到buf中,buf为传出参数*/ (2).文件信息结构体: /*用不到的成员被注释掉,只需了解需要的成员即可*/ struct stat{ //dev_t st_dev;/*设备id号*/ //ino_t st_ino;/*i节点号*/ mod