資源簡介
自己學習《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

代碼片段和文件信息
/*
????本程序模擬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個文件信息
評論
共有 條評論