c實現cat命令
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <sys/types.h>
#include <grp.h>
#include <sys/stat.h>
int cats(const char *filename);
void print(const char *filename, struct stat *st);
void mode_to_letters(int mode, char * str);
char *uid_to_name(uid_t uid);
char *gid_to_name(gid_t gid);
void Usage();
int main(int argc, char **argv)
{
if (argc < 2)
{
Usage();
return -1;
}
else
{
cats(argv[1]);
}
return 0;
}
int cats(const char *filename)
{
FILE *fp = NULL;
char *buffer = NULL;
struct stat st;
stat(filename, &st);
int number = 0;
buffer = (char *)malloc(sizeof(char)*st.st_size);
memset(buffer, 0, st.st_size);
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("open file failer!\n");
fclose(fp);
return -1;
}
number = fread(buffer, st.st_size, 1, fp);
if (number < 0)
{
printf("read file failer!\n");
fclose(fp);
return -1;
}
print(filename, &st);
printf("%s", buffer);
free(buffer);
buffer = NULL;
fclose(fp);
return 0;
}
void print(const char *filename, struct stat *st)
{
char modestr[11];
mode_to_letters( st->st_mode, modestr );
printf("\n*********************************************\n");
printf("File Name:\t%s\n",filename);
printf("File EePer:\t%s\n", modestr);
printf("File Size:\t%ld bytes\n", (long)st->st_size);
printf("ID Users:\t%s\n", uid_to_name(st->st_uid));
printf("ID Group:\t%s\n", gid_to_name(st->st_gid));
printf("Last MTime:\t%s", (char *)(4+ctime(&st->st_mtime)));
printf("*********************************************\n\n");
}
void mode_to_letters(int mode, char *str)
{
strcpy(str, "----------");
if (S_ISDIR(mode))
{
str[0] = 'd';
}
if (S_ISCHR(mode))
{
str[0] = 'c';
}
if (S_ISBLK(mode))
{
str[0] = 'b';
}
if (mode & S_IRUSR)
{
str[1] = 'r';
}
if (mode & S_IWUSR)
{
str[2] = 'w';
}
if (mode & S_IXUSR)
{
str[3] = 'x';
}
if (mode & S_IRGRP)
{
str[4] = 'r';
}
if (mode & S_IWGRP)
{
str[5] = 'w';
}
if (mode & S_IXGRP)
{
str[6] = 'x';
}
if (mode & S_IROTH)
{
str[7] = 'r';
}
if (mode & S_IWOTH)
{
str[8] = 'w';
}
if (mode & S_IXOTH)
{
str[9] = 'x';
}
}
char *uid_to_name(uid_t uid)
{
struct passwd *pw_ptr;
static char numstr[10];
if ((pw_ptr = getpwuid(uid)) == NULL)
{
sprintf(numstr, "%d", uid);
return numstr;
}
else
{
return pw_ptr->pw_name ;
}
}
char *gid_to_name(gid_t gid)
{
struct group *grp_ptr;
static char numstr[10];
if ((grp_ptr = getgrgid(gid)) == NULL)
{
sprintf(numstr, "%d", gid);
return numstr;
}
else
{
return grp_ptr->gr_name;
}
}
void Usage()
{
printf("format error!Parameter input format:\n");
printf("\tformat: ./cats filename\n");
}
相關文章
- Linux cat命令Linux
- linux每日命令(11):cat命令Linux
- 命令模式(c++實現)模式C++
- Linux基礎命令—catLinux
- Linux基礎命令---catLinux
- 比cat更好用的命令!
- 常用命令 ---tail-catAI
- cat 命令的原始碼進化史原始碼
- 比 cat 更好用的命令瞭解一下?
- cat > file << EOF 與 cat > file << -
- 使用C# (.NET Core) 實現命令設計模式 (Command Pattern)C#設計模式
- 簡單解析C++基於Boost庫實現命令列C++命令列
- Linux 常用檢視日誌命令 tail、head、cat、more、lessLinuxAI
- C均值聚類 C實現 Python實現聚類Python
- Linux常用命令之cp、mv、rm、cat、more、head、tail、ln命令講解LinuxAI
- Bat:一種具有語法高亮和 Git 整合的 Cat 類命令BATGit
- cat命令有哪些功能用途?學習linux主要學什麼Linux
- [C++]實現memcpyC++memcpy
- 【命令設計模式詳解】C/Java/JS/Go/Python/TS不同語言實現設計模式JavaJSGoPython
- 任務4測試1:ls cat mv touch命令列及explainshell.com命令列AI
- Cat, Fox and the Lonely Array
- 呼叫鏈監控 CAT 之 URL埋點實踐
- Blue Cat Audio Blue Cat PatchWork mac(藍貓橋接外掛)Mac橋接
- Android Binder實現示例(C/C++層)AndroidC++
- c# 迭代器實現C#
- C#樹的實現C#
- 堆排序(實現c++)排序C++
- LinkBlockedQueue的c++實現BloCC++
- 堆排序c++實現排序C++
- 佇列(Queue)-c實現佇列
- C實現奇偶校驗
- c++實現Json庫C++JSON
- python實現批次執行命令列Python命令列
- 在Unity實現遊戲命令模式Unity遊戲模式
- Artisan 命令列 2種實現方式命令列
- Elasticsearch cat api的用法ElasticsearchAPI
- Cat, Fox and Maximum Array Split
- 【攻防世界】ezbypass-cat