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");
}
相關文章
- cat 命令(轉)
- Linux cat命令Linux
- linux每日命令(11):cat命令Linux
- linux之cat命令Linux
- 命令模式(c++實現)模式C++
- Linux基礎命令—catLinux
- Linux基礎命令---catLinux
- 比cat更好用的命令!
- 常用命令 ---tail-catAI
- 每天一個 Linux 命令(10):cat 命令Linux
- cat 命令的原始碼進化史原始碼
- Linux 常用基本命令 cat grepLinux
- cat ,more ,less 命令的使用和差別
- Centos檔案切割利器_split命令及cat命令合併檔案CentOS
- 比 cat 更好用的命令瞭解一下?
- 用cat命令檢視檔案內的特殊字元(轉)字元
- 簡單解析C++基於Boost庫實現命令列C++命令列
- C均值聚類 C實現 Python實現聚類Python
- Linux基本命令學習之三:cat tac nl moreLinux
- 使用C# (.NET Core) 實現命令設計模式 (Command Pattern)C#設計模式
- Bat:一種具有語法高亮和 Git 整合的 Cat 類命令BATGit
- Linux 常用檢視日誌命令 tail、head、cat、more、lessLinuxAI
- Linux常用命令之cp、mv、rm、cat、more、head、tail、ln命令講解LinuxAI
- Cat, Fox and the Lonely Array
- cat命令有哪些功能用途?學習linux主要學什麼Linux
- 呼叫鏈監控 CAT 之 URL埋點實踐
- [C++]實現memcpyC++memcpy
- C++ binder 實現C++
- 在Unity實現遊戲命令模式Unity遊戲模式
- 命令列進度條實現命令列
- Blue Cat Audio Blue Cat PatchWork mac(藍貓橋接外掛)Mac橋接
- Elasticsearch cat api的用法ElasticsearchAPI
- Cat, Fox and Maximum Array Split
- 【攻防世界】ezbypass-cat
- 利用棧實現佇列(C語言實現)佇列C語言
- Callback在C\C++中的實現C++
- 佇列(Queue)-c實現佇列
- 堆排序(實現c++)排序C++