资源简介
个人写的一个简单的 ls命令,可能会有错误,希望有兴趣的可以交流交流!!

代码片段和文件信息
#include “stdio.h“
#include “unistd.h“
#include “stdlib.h“
#include “sys/types.h“
#include “sys/stat.h“
#include “fcntl.h“
#include “pwd.h“
#include “grp.h“
#include “dirent.h“
#define NAME_SIZE 100
typedef struct item
{
char name[NAME_SIZE];
int length;
struct stat st;
struct item *next;
}nNode;
#define MARK_FILE(valueflags) ((flags) = (((value)&ALL_FILE)|(flags)))
#define DEL_FILE(valueflags) ((flags) = ((~(value))&(flags)))
#define MARK_INFO(valueflags) ((flags) = (((value)&STAT_ALL_INFO)|(flags)))
#define DEL_INFO(valueflags) ((flags) = ((~(value))&(flags)))
#define MARK(valueflags) ((flags) = ((value)|(flags)))
#define DEL(valueflags) ((flags) = ((~(value))&(flags)))
#define FILE_TYPE 0xf8000000
#define OTH_FILE 0x80000000
#define DIR_FILE 0x40000000
#define REG_FILE 0x20000000
#define BAK_FILE 0x10000000
#define DOT_FILE 0x08000000
#define ALL_FILE 0xf8000000
#define STAT_ALL_INFO 0x07f00000
#define STAT_GROUP 0x04000000
#define STAT_OWNER 0x02000000
#define STAT_NUMID 0x01000000
#define STAT_SIZE 0x00800000 //0 present bytes 1 present k or m
#define STAT_INODE 0x00400000
#define STAT_TIME 0x00200000
#define STAT_PERMISSION 0x00100000
#define STAT_COLOR 0x00080000
#define STAT_RECUR 0x00040000
#define STAT_HR 0x00020000
void AddnNode(nNode **headchar *namestruct stat *st);
void freeNode(nNode *head );
void do_ls(char *filenameint flagsnNode **head);
void showitem(char *name struct stat *stint flags);
void showfile(nNode *headint flags);
void quitsort(char **arrayint highint low);
void format_permission(struct stat *st)
{
int mode = st->st_mode;
char buf[12];
memset(buf‘-‘11);
buf[11]=0;
if(S_ISDIR(mode)) buf[0] = ‘d‘;
else if(S_ISCHR(mode)) buf[0] = ‘c‘;
else if(S_ISBLK(mode)) buf[0] = ‘b‘;
else if(S_ISFIFO(mode)) buf[0] = ‘p‘;
else if(S_ISLNK(mode)) buf[0] = ‘l‘;
else if(S_ISSOCK(mode)) buf[0] = ‘s‘;
if(S_IRUSR&mode) buf[1] = ‘r‘;
if(S_IWUSR&mode) buf[2] = ‘w‘;
if(S_IXUSR&mode) buf[3] = ‘x‘;
if(S_IRGRP&mode) buf[4] = ‘r‘;
if(S_IWGRP&mode) buf[5] = ‘w‘;
if(S_IXGRP&mode) buf[6] = ‘x‘;
if(S_IROTH&mode) buf[7] = ‘r‘;
if(S_IWOTH&mode) buf[8] = ‘w‘;
if(S_IXOTH&mode) buf[9] = ‘x‘;
if(S_ISUID&mode) buf[3] = ‘s‘;
if(S_ISGID&mode) buf[6] = ‘s‘;
if(S_ISVTX&mode) buf[10] = ‘s‘;
printf(“%s “buf);
}
void format_group(struct stat *st)
{
int id = st->st_uid;
struct group *buf = getgrgid(id);
if(buf)
{
printf(“%s “buf->gr_name);
}else
{
printf(“%d “id);
}
}
void format_owner(struct stat *st)
{
int id = st->st_gid;
struct passwd *buf_pw = getpwuid(id);
if(buf_pw)
{
printf(“%s “buf_pw->pw_name);
}else
{
printf(“%d “id);
}
}
void format_time(struct stat *st)
{
char *buf= ctime(&(st->st_ctime));
buf +=4;
buf[strlen(buf)-1]=0;
printf(“%s “buf);
}
void format_numid(struct stat *st)
{
printf(“%d %d “st->st_uidst->st_gid);
}
void main(int arg
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 292 2009-08-25 14:43 ls\add
文件 574 2009-08-25 14:43 ls\add!
文件 49 2009-08-25 14:43 ls\hello
文件 11967 2009-08-25 14:43 ls\ls
文件 9900 2009-08-25 14:43 ls\ls.c
文件 5896 2009-08-25 14:43 ls\ls.c.cpy
文件 9900 2009-08-25 14:43 ls\ls.c~
文件 5605 2009-08-25 14:43 ls\quitsort
文件 934 2009-08-25 14:43 ls\quitsort.c
文件 934 2009-08-25 14:43 ls\quitsort.c~
文件 1 2009-08-25 14:43 ls\quitsort~
文件 659 2009-08-25 14:43 ls\strcmp.c
文件 659 2009-08-25 14:43 ls\strcmp.c~
文件 4931 2009-08-25 14:43 ls\test
文件 1335 2009-08-25 14:43 ls\test.c
文件 1302 2009-08-25 14:43 ls\test.c~
文件 5126 2009-08-25 14:43 ls\testpp
文件 276 2009-08-25 14:43 ls\testpp.c
文件 278 2009-08-25 14:43 ls\testpp.c~
目录 0 2009-09-23 13:46 ls
----------- --------- ---------- ----- ----
60618 20
- 上一篇:员工绩效管理系统最终版
- 下一篇:飞机票生成器
相关资源
- LS-DYNA THEORETICAL MANUAL
- DIR2病毒源代码(汇编语言编写)
- VC仿Windows记事本源代码
- 蓝牙源代码应用于LINUX
- MySQL Notes For Professionals
- bp神经网络源代码,可直接运行
- 仿知乎界面小程序源代码
- 贪吃蛇源代码.fla
- uboot到linux logo显示不间断 补丁
- ANSYS LS-DYNA
- VisualStudioUninstaller vs卸载工具
- UNIX/LINUX编程实践教程的源码
- dotnet 写字板 实验 源代码 不好请要不
- 图像二维小波变换的实现源代码
- 八三编码器设计 VHDL代码 简单,包附
- B/S 网上订餐系统
- Linux任务管理器
- linux应用层的华容道游戏源代码
- VMware65_SLP_DeLLSLIC2.1
- 网上拍卖系统完整源代码
- CSMA/CD等动画演示加源代码
- silicon lab公司的收音IC SI47XX全套开发工
- 加密cad图形的lisp文件
- 合同管理系统的源代码(附数据库)
- 用VC 编写的仿QQ聊天室程序源代码
- STM32F103 串口程序(完整版)
- VPC3_DPV1源代码,Profibus
- PB做的托盘程序(最小化后在左下角显
- ubuntu9.10 可加载内核模块和字符设备驱
- 透明加密源码及说明
评论
共有 条评论