当前位置:网站首页>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
边栏推荐
- C language to realize mine sweeping
- 洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
- On MEM series functions of C language
- Zzuli:1058 solving inequalities
- Luogu p5536 [xr-3] core city solution
- Tonybot humanoid robot checks the port and corresponds to port 0701
- 7-22 tortoise and rabbit race (result oriented)
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- Selective sorting
- Rasterization: a practical implementation (2)
猜你喜欢

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

提高效率 Or 增加成本,开发人员应如何理解结对编程?

US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars

7-15 calculation of PI

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

ConstraintLayout 的使用

Why is this error reported when modifying records in the database

Rasterization: a practical implementation (2)

Puzzle (016.4) domino effect

puzzle(016.3)千丝万缕
随机推荐
To improve efficiency or increase costs, how should developers understand pair programming?
Convert string to decimal integer
7-10 stack of hats (25 points) (C language solution)
7-23 currency conversion (using array conversion)
Statistical capital consonants
Add ZABBIX calculation type itemcalculated items
Output student grades
Find books ()
tonybot 人形机器人 定距移动 代码编写玩法
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
puzzle(016.4)多米诺效应
Zzuli:1057 prime number determination
C language STR function
动态获取权限
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Why is this error reported when modifying records in the database