秀杰空间

  • 首页
  • 心情笔记
  • Linux/Unix C/C++
  • PHP
  • 我的项目
秀杰笔记
做些有意义的事情
  1. 首页
  2. Linux/Unix C/C++
  3. 正文

UNIX环境高级编程学习之第四章文件和目录-用C实现Shell中的"ls -l"功能

2016年7月19日 774点热度 0人点赞 0条评论

UNIX环境高级编程学习之第四章文件和目录-用C实现Shell中的"ls -l"功能
[code lang="cpp"]// 只能查看目录中的所有文件属性
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
char szDirPath[512] = { 0 };
char szFilePath[512] = { 0 };
DIR * pDIR;
if (argc > 1)
{
strcpy(szDirPath, argv[1]);
}else {
strcpy(szDirPath, ".");
}
pDIR = opendir(szDirPath);
if (NULL == pDIR)
{
printf("Open DIR[%s] Error! /n", szDirPath);
return -1;
}
struct dirent* pdirent;
int n = 0;
int i;
int y;
char szName[256];
char szLineData[512];
struct stat file_stat;
while ( (pdirent = readdir(pDIR)) != NULL)
{
memset(szName, 0, sizeof(szName));
memset(szLineData, 0, sizeof(szLineData));
memset(szFilePath, 0, sizeof(szFilePath));
memset(&file_stat, '/0', sizeof(file_stat));
strcpy(szName, pdirent->d_name);
sprintf(szFilePath, "%s/%s", szDirPath, szName);
stat(szFilePath, &file_stat);
time_t t = file_stat.st_mtime;
int len = strlen(szName);
if (pdirent->d_type == DT_DIR) // 文件夹
{
szName[len] = '//';
}else if (pdirent->d_type == DT_DIR) // 符号连接
{
szName[len] = '@';
}
sprintf(szLineData, "%-32s uid=%-8u gid=%-8u LastDate=%s ", szName, file_stat.st_uid, file_stat.st_gid, ctime(&t));
printf(szLineData);
}
printf("/n");
closedir(pDIR);
return 0;
}

[/code]

标签: UNIX环境高级编程
最后更新:2016年7月19日

秀杰

做些有意义的事情

点赞
< 上一篇
下一篇 >

文章评论

您需要 登录 之后才可以评论

秀杰

做些有意义的事情

标签聚合
pb UNIX环境高级编程 zookeeper protobuf select 分布式 mac epoll tuxedo ubuntu socket
最新 热点 随机
最新 热点 随机
C++使用protobuf快速入门简明教程 Mac安装Brew(Homebrew)国内镜像源加速 从sockaddr_storage结构中取IP地址和端口 [转载]分布式之数据库和缓存双写一致性方案解析 TPS和QPS的区别和理解 2018最新可靠好用的DNS服务器地址汇总
Makefile 同时编译静态库和动态库 windows下虚拟机安装ubuntu环境 [myNote.live]我的笔记,高效知识库管理笔记应用 UNIX环境高级编程学习之第十三章守护进程 - 单实例的守护进程 2018最新可靠好用的DNS服务器地址汇总 XJPHP RESTful : 是一个超轻量的快速开发框架。Light and Fast。
京ICP备11019155号-2

COPYRIGHT © 2023 个人笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang