当前位置:网站首页>Simulation of LS -al command in C language
Simulation of LS -al command in C language
2022-07-03 14:40:00 【ma_ de_ hao_ mei_ le】
C The code is as follows :
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <string.h>
// Simulation Implementation ls -al command
// -rwxrwxrwx 1 x x 12 Feb 10 18:49 1.txt
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("usage:\n\t %s filename\n", argv[0]);
return -1;
}
// adopt stat Function to get the information of the file passed in by the user
struct stat st;
int ret = stat(argv[1], &st);
if (ret == -1) {
perror("stat");
return -1;
}
// Get file type and file permissions , altogether 10 position
// perms String array is used to save file types and permissions
char perms[11] = {
0};
// S_IFMT It's a mask , And it can get the file type
switch (st.st_mode & S_IFMT) {
// A symbolic link
case S_IFLNK:
perms[0] = 'l';
break;
// Catalog
case S_IFDIR:
perms[0] = 'd';
break;
// Ordinary documents
case S_IFREG:
perms[0] = '-';
break;
// Block device
case S_IFBLK:
perms[0] = 'b';
break;
// Character device
case S_IFCHR:
perms[0] = 'c';
break;
// The Conduit
case S_IFIFO:
perms[0] = 'p';
break;
// Socket
case S_IFSOCK:
perms[0] = 's';
break;
default:
perms[0] = '?';
break;
}
// Get the permission of the file
// Get the file owner's read 、 Write 、 Executive authority
perms[1] = st.st_mode & S_IRUSR ? 'r' : '-';
perms[2] = st.st_mode & S_IWUSR ? 'w' : '-';
perms[3] = st.st_mode & S_IXUSR ? 'x' : '-';
// Get the read of the group to which the file belongs 、 Write 、 Executive authority
perms[4] = st.st_mode & S_IRGRP ? 'r' : '-';
perms[5] = st.st_mode & S_IWGRP ? 'w' : '-';
perms[6] = st.st_mode & S_IXGRP ? 'x' : '-';
// Get others to read the file 、 Write 、 Executive authority
perms[7] = st.st_mode & S_IROTH ? 'r' : '-';
perms[8] = st.st_mode & S_IWOTH ? 'w' : '-';
perms[9] = st.st_mode & S_IXOTH ? 'x' : '-';
// Get the number of hard links
int link_num = st.st_nlink;
// File owner
char* file_user = getpwuid(st.st_uid)->pw_name;
// File group
char* file_group = getgrgid(st.st_gid)->gr_name;
// Get file size
long int file_size = st.st_size;
// Get modification time
char* temp_file_time = ctime(&st.st_mtime);
// Remove the newline character in the time string
char file_time[512] = {
0};
strncpy(file_time, temp_file_time, strlen(temp_file_time) - 1);
char buf[1024];
sprintf(buf, "%s %d %s %s %ld %s %s", perms, link_num, file_user, file_group, file_size, file_time, argv[1]);
printf("%s\n", buf);
return 0;
}
effect :
[email protected]:/mnt/c/Users/x/Pictures/code/lesson12$ gcc ls-l.c -o ll
[email protected]:/mnt/c/Users/x/Pictures/code/lesson12$ ./ll 1.txt
-rwxrwxrwx 1 x x 12 Thu Feb 10 18:49:38 2022 1.txt
边栏推荐
- 分布式事务(Seata) 四大模式详解
- 基因家族特征分析 - 染色体定位分析
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- 关于敏捷的一些概念
- Dllexport et dllimport
- Address book sorting
- Zzuli:1048 factorial table
- Common shortcut keys in PCB
- Solr series of full-text search engines - basic principles of full-text search
- Showmebug entered Tencent conference, opening the era of professional technical interview
猜你喜欢

Detailed explanation of four modes of distributed transaction (Seata)

Why is this error reported when modifying records in the database

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

MySQL multi table query subquery

一文了解微分段应用场景与实现机制

Puzzle (016.4) domino effect

Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing

Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million

tonybot 人形機器人 紅外遙控玩法 0630
随机推荐
论文分享:Generating Playful Palettes from Images
Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
Dllexport and dllimport
Programming language: the essence of type system
puzzle(016.4)多米诺效应
Rasterization: a practical implementation (2)
Find books ()
洛谷P5536 【XR-3】核心城市 题解
Bucket sorting in C language
7-22 tortoise and rabbit race (result oriented)
7-3 count the number of words in a line of text
DDK for XP
Zzuli:1042 sum of sequence 3
适用于XP的DDK
Recent learning summary
7-10 stack of hats (25 points) (C language solution)
7-24 reduction of the simplest fraction (rolling Division)
tonybot 人形機器人 紅外遙控玩法 0630
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
动态获取权限