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 <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <grp.h>
#include <pwd.h> void show01(struct stat st)//文件属性-rwxr--r--
{
/*打印文件类型*/
if (S_ISLNK(st.st_mode)) {
printf("l");
} else if (S_ISREG(st.st_mode)) {
printf("-");
} else if (S_ISDIR(st.st_mode)) {
printf("d");
} else if (S_ISCHR(st.st_mode)) {
printf("c");
} else if (S_ISBLK(st.st_mode)) {
printf("b");
} else if (S_ISFIFO(st.st_mode)) {
printf("f");
} else if (S_ISSOCK(st.st_mode)) {
printf("s");
}
/*U文件全部者的权限*/
if (st.st_mode & S_IRUSR){
printf("r");
} else {
printf("-");
}
if (st.st_mode & S_IWUSR){
printf("w");
} else {
printf("-");
}
if (st.st_mode & S_IXUSR){
printf("x");
} else {
printf("-");
}
/*G文件全部组的权限*/
if (st.st_mode & S_IRGRP){
printf("r");
} else {
printf("-");
}
if (st.st_mode & S_IWGRP){
printf("w");
} else {
printf("-");
}
if (st.st_mode & S_IXGRP){
printf("x");
} else {
printf("-");
}
/*O其他用户的权限*/
if (st.st_mode & S_IROTH){
printf("r");
} else {
printf("-");
}
if (st.st_mode & S_IWOTH){
printf("w");
} else {
printf("-");
}
if (st.st_mode & S_IXOTH){
printf("x");
} else {
printf("-");
}
printf(" ");
} void show02(struct stat st)//硬链接数
{
printf("%lu", st.st_nlink);
printf(" ");
} void show03(struct stat st)//username
{
struct passwd *psd ;
psd = getpwuid(st.st_uid);
printf("%s", psd->pw_name);
printf(" ");
} void show04(struct stat st)//组名
{
struct group *grp = getgrgid(st.st_gid);
printf("%s", grp->gr_name);
printf(" ");
} void show05(struct stat st)//文件大小
{
printf("%lu", st.st_size);
printf(" ");
} void show06(struct stat st)//文件时间
{
char timebuf[20];
struct tm* newtime = localtime(&st.st_mtime);
strftime(timebuf, 20,"%B %d %H:%M",newtime);
printf("%s", timebuf);
printf(" ");
} void show07(const char *fname)//文件名
{
printf("%s", fname);
printf(" ");
} int main(int argc, const char *argv[])
{
int ret = 0;
struct stat st; if(argc<2)
{
printf("./a.out file\n");return ;
}
ret = lstat(argv[1], &st);
if(ret<0) perror("lstat()"); show01(st);
show02(st);
show03(st);
show04(st);
show05(st);
show06(st);
show07(argv[1]); puts("");//换行
return 0;
}
函数的接口设计的方面,不是非常合理,主要是练习函数的使用。
UC高级编程--实现myls程序的更多相关文章
- multiple definition of `err_sys' 《UNIX环境高级编程》
本文地址:http://www.cnblogs.com/yhLinux/p/4079930.html 问题描述: [点击此处直接看解决方案] 在练习<UNIX环境高级编程>APUE程序清单 ...
- 关于UNIX/Linux下安装《UNIX环境高级编程》源代码的问题
<UNIX环境高级编程(第三版)>是一本广为人知的unix系统编程书籍. 但是,书中的代码示例,要想正确的编译运行,要先做好准备工作: 1.下载源代码 传送门:http://apueboo ...
- 读《C#高级编程》第1章问题
读<C#高级编程>第1章 .Net机构体系笔记 网红的话:爸爸说我将来会是一个牛逼的程序员,因为我有一个梦,虽然脑壳笨但是做事情很能坚持. 本章主要是了解.Net的结构,都是一些概念,并没 ...
- 《C#高级编程》读书笔记
<C#高级编程>读书笔记 C#类型的取值范围 名称 CTS类型 说明 范围 sbyte System.SByte 8位有符号的整数 -128~127(−27−27~27−127−1) sh ...
- jquery插件开发继承了jQuery高级编程思路
要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...
- (十三) [终篇] 一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- Node.js高级编程读书笔记Outline
Motivation 世俗一把,看看前端的JavaScript究竟能做什么. 顺便检验一下自己的学习能力. Audience 想看偏后台的Java程序员关于前端JavaScript的认识的职业前端工程 ...
- (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
- (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO
. . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...
随机推荐
- [每日一题] OCP1z0-047 :2013-08-29 NULL............................................................168
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10558305 正确答案:B 用函数可以针对各种数据类型时行操作,包括NULL值在内.其中有 ...
- Android 最火高速开发框架AndroidAnnotations使用具体解释
Android 最火的高速开发框架androidannotations配置具体解释文章中有eclipse配置步骤,Android 最火高速开发框架AndroidAnnotations简介文章中的简介, ...
- shell脚本中怎样同时执行多个.sql文件,并把结果写入文件中(转)
转载:http://joewalker.iteye.com/blog/408879命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名;导出整个数据 ...
- Oracle 排序规则
<pre name="code" class="html">SQL> select * from t1 where id>=1 and ...
- VIM 用正则表达式
VIM 用正则表达式 批量替换文本,多行删除,复制,移动 在VIM中 用正则表达式 批量替换文本,多行删除,复制,移动 :n1,n2 m n3 移动n1-n2行(包括n1,n2)到n3行之下: ...
- hdu1695(莫比乌斯)或欧拉函数+容斥
题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...
- 可以根据柜子内表取出所有的柜子信息的BAPI函数
DATA: gt_hunumbers TYPE STANDARD TABLE OF bapihunumber, gt_huitem TYPE STANDARD TABLE OF bapih ...
- J2EE开发框架搭建(2) - springmvc4 + spring4 + hibernate4 整合
1. 打开hqhop-framework-parent项目下的pom.xml文件.加入springmvc4 , spring4 , hibernate4 ,以及数据源druid的依赖包,插件,依赖包版 ...
- Cocos2d-x3.3RC0的Android编译Activity启动流程分析
本文将从引擎源代码Jni分析Cocos2d-x3.3RC0的Android Activity的启动流程,以下是具体分析. 1.引擎源代码Jni.部分Java层和C++层代码分析 watermark/2 ...
- Win7和VS2013上使用Intel的TBB
源地址:http://www.th7.cn/system/win/201505/103966.shtml http://wenku.baidu.com/link?url=zH7vwmWltWF5R-9 ...