当前位置:网站首页>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
边栏推荐
- pyQt界面制作(登录+跳转页面)
- Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
- Sendmail can't send mail and it's too slow to send. Solve it
- 7-23 currency conversion (using array conversion)
- Use of form text box (I) select text
- retrofit
- Container of symfony
- 1017 a divided by B (20 points)
- 7-14 sum integer segments (C language)
- 洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
猜你喜欢
Showmebug entered Tencent conference, opening the era of professional technical interview
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Dllexport and dllimport
Puzzle (016.3) is inextricably linked
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
puzzle(016.3)千丝万缕
Tonybot humanoid robot starts for the first time 0630
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
随机推荐
Paper sharing: generating playful palettes from images
7-6 mixed type data format input
7-10 stack of hats (25 points) (C language solution)
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
ZABBIX saves the page blank after adding calculated items
Zzuli:1053 sine function
Dllexport et dllimport
7-3 rental (20 points)
Zzuli:1042 sum of sequence 3
洛谷P5536 【XR-3】核心城市 题解
7-17 crawling worms (break exercise)
tonybot 人形机器人 查看端口并对应端口 0701
Detailed explanation of four modes of distributed transaction (Seata)
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
2021-10-16 initial programming
Zzuli:1059 highest score
NPM install is stuck with various strange errors of node NPY
Luogu p5536 [xr-3] core city solution
Zzuli:1047 logarithmic table
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?