资源简介
自己学习《APUE》时写的linux下一些命令(大概40个左右)实现,仅当学习使用,这些命令包括cat cp echo head ls paste rmdir tail umask who chattr cut expand join mkdir pwd sed tee uniq chgrp date find last mkfifo reboot sort wc chmod df ln mv rename split touch which chown du grep lsattr od rm tac tr whoami
![](http://www.nz998.com/pic/46084.jpg)
代码片段和文件信息
/*
本程序模拟cat命令(只模拟打印,不处理特殊字符)
历史记录:
日期 作者 描述 联系方式
20140417 卢富波 首次创建 1164830775@qq.com
需求:
仅仅只是将数据打印到标准输出,不对数据中的特殊字符进行处理
如果没输入任何参数,或者输入的文件名为-则从标准输入读数据
*/
#include /* for printf */
#include /* for exit */
#include
#include
#include
#include
#include
#include
#define EXIT_FAILURE 1
#define EXIT_SUCESS 0
#define MAX_BUF_SIZE 1024
static char *program_name = NULL; /* 程序名指针 */
static char *infile = NULL; /* 指向输入文件 */
static int input_desc = -1; /* 输入文件描述符 */
static int exit_status = EXIT_FAILURE; /* 命令退出状态 */
static void
usage(int status)
{
if(status != 0){
fprintf(stderr “Try ‘%s --help‘ for more infomation\n“
program_name);
}else{
printf(“Usage: %s [FILE] ...\n“ program_name);
printf(“with no FILE or when FILE is - %s will read from stdin\n“ program_name);
}
exit(status);
}
static void
cat_exit()
{
exit(exit_status);
}
static void
_cat(int fileno int needexit)
{
char buf[MAX_BUF_SIZE];
int n;
while((n = read(fileno buf MAX_BUF_SIZE)) > 0){
if(write(STDOUT_FILENO buf n) != n){
perror(“in write“);
exit(exit_status);
}
}
if(n < 0){
perror(“in read“);
if(!needexit)
exit(exit_status);
}
if(needexit){
exit_status = EXIT_SUCESS;
exit(exit_status);
}
}
static void
simple_cat(char *file)
{
struct stat statbuf;
if(file == NULL || file[0] == 0){
//sprintf(stderr “some asset error happend\n“);
exit(exit_status);
}
if(file[0] == ‘-‘ && file[1] == 0)/*从标准输入读数据*/
_cat(STDIN_FILENO 1); /*从标准输入读数据并显示数据,然后退出*/
/*下面是从file文件里面读数据了*/
input_desc = open(file O_RDONLY);
if(input_desc < 0){
fprintf(stderr “open file ‘%s‘ error: %s\n“ file strerror(errno));
exit(exit_status);
}
if(fstat(input_desc &statbuf) < 0){
fprintf(stderr “fstat file ‘%s‘ error: %s\n“ file strerror(errno));
close(input_desc);
exit(exit_status);
}
if(S_ISREG(statbuf.st_mode) == 0){
fprintf(stderr “file ‘%s‘ is not a genel file\n“ file);
close(input_desc);
exit(exit_status);
}
_cat(input_desc 0);
close(input_desc);
}
int
main(int argc char *argv[])
{
struct stat stat_buf;
int c;
static struct option const long_options[] =
{
{“help“ 0 NULL ‘h‘}
{NULL 0 NULL 0}
};
program_name = a
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 965 2014-06-25 13:37 my_linux_command\ReadMe.txt
....... 3899 2014-04-17 23:38 my_linux_command\cat.c
....... 4538 2014-05-30 11:55 my_linux_command\chattr.c
....... 2037 2014-05-16 16:37 my_linux_command\chgrp.c
....... 2299 2014-05-15 22:30 my_linux_command\chmod.c
....... 2053 2014-05-16 16:35 my_linux_command\chown.c
....... 2209 2014-05-09 21:30 my_linux_command\cp.c
....... 7807 2014-06-05 12:09 my_linux_command\cut.c
....... 6111 2014-05-16 13:37 my_linux_command\date.c
....... 2869 2014-06-05 12:08 my_linux_command\df.c
....... 1533 2014-06-05 12:08 my_linux_command\du.c
....... 636 2014-05-29 13:49 my_linux_command\echo.c
....... 2479 2014-06-17 09:56 my_linux_command\expand.c
....... 1869 2014-06-03 09:45 my_linux_command\find.c
....... 2131 2014-04-17 19:12 my_linux_command\getopt_long.c
....... 2857 2014-06-05 12:09 my_linux_command\grep.c
....... 4835 2014-05-09 10:37 my_linux_command\head.c
....... 7690 2014-06-17 09:56 my_linux_command\join.c
....... 4370 2014-06-05 12:08 my_linux_command\last.c
....... 268 2014-05-30 11:55 my_linux_command\lib\bit_opr.h
....... 2943 2014-06-17 09:56 my_linux_command\lib\char_map.h
....... 3019 2014-05-15 22:26 my_linux_command\lib\chmod.h
....... 3740 2014-05-16 16:35 my_linux_command\lib\chown.h
....... 513 2014-04-18 00:20 my_linux_command\lib\command_comm.h
....... 8973 2014-05-09 21:22 my_linux_command\lib\copy.h
....... 906 2014-06-17 09:56 my_linux_command\lib\err_msg.h
....... 1887 2014-05-13 21:53 my_linux_command\lib\file_isexist.h
....... 1868 2014-06-03 09:45 my_linux_command\lib\fileflag.h
....... 3328 2014-06-03 09:45 my_linux_command\lib\find_file.h
....... 2360 2014-06-17 09:56 my_linux_command\lib\get_filesize.h
............此处省略50个文件信息
相关资源
- C++中头文件与源文件的作用详解
- C语言代码高亮html输出工具
- 猜数字游戏 c语言代码
- C语言课程设计
- 数字电位器C语言程序
- CCS FFT c语言算法
- 使用C语言编写的病房管理系统
- 通信过程中的RS编译码程序(c语言)
- 计算机二级C语言上机填空,改错,编
- 用回溯法解决八皇后问题C语言实现
- 简易教务管理系统c语言开发文档
- 操作系统课设 读写者问题 c语言实现
- 小波变换算法 c语言版
- C流程图生成器,用C语言代码 生成C语
- 3des加密算法C语言实现
- 简单的C语言点对点聊天程序
- 单片机c语言源程序(51定时器 八个按
- 个人日常财务管理系统(C语言)
- c语言电子商务系统
- 小甲鱼C语言课件 源代码
- 将图片转换为C语言数组的程序
- C语言实现的一个内存泄漏检测程序
- DES加密算法C语言实现
- LINUX下命令行界面的C语言细胞游戏
- 用单片机控制蜂鸣器播放旋律程序(
- 学校超市选址问题(数据结构C语言版
- 电子时钟 有C语言程序,PROTEUS仿真图
- 尚观培训linux许巍老师关于c语言的课
- 算符优先语法分析器(C语言编写)
- 基于C语言的密码锁程序
评论
共有 条评论