当前位置:网站首页>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
边栏推荐
- FPGA blocking assignment and non blocking assignment
- How Facebook moves instagram from AWS to its own server
- Zzuli:1042 sum of sequence 3
- Tonybot humanoid robot starts for the first time 0630
- 7-3 count the number of words in a line of text
- Puzzle (016.4) domino effect
- 使用并行可微模拟加速策略学习
- Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th
- 【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
- SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
猜你喜欢
随机推荐
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
一文了解微分段应用场景与实现机制
Zzuli:1044 failure rate
C language memory function
7-24 reduction of the simplest fraction (rolling Division)
动态获取权限
puzzle(016.3)千丝万缕
分布式事务(Seata) 四大模式详解
牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
Convert string to decimal integer
Sword finger offer 28 Symmetric binary tree
2021-10-16 initial programming
MySQL multi table query subquery
Preliminary summary of structure
7-15 calculation of PI
Detailed explanation of four modes of distributed transaction (Seata)
Zzuli:1058 solving inequalities
Rasterization: a practical implementation (2)
Luogu p5536 [xr-3] core city solution
China PETG market forecast and Strategic Research Report (2022 Edition)