当前位置:网站首页>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
边栏推荐
- String sort
- 7-10 stack of hats (25 points) (C language solution)
- adc128s022 ADC verilog设计实现
- Zzuli:1041 sum of sequence 2
- 牛客 BM83 字符串变形(大小写转换,字符串反转,字符串替换)
- Zzuli:1052 sum of sequence 4
- Sword finger offer 28 Symmetric binary tree
- Accelerating strategy learning using parallel differentiable simulation
- 【7.3】146. LRU caching mechanism
- Time conversion ()
猜你喜欢

On MEM series functions of C language

556. The next larger element III

Puzzle (016.4) domino effect

Tonybot humanoid robot checks the port and corresponds to port 0701

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

MySQL multi table query subquery

Niuke: crossing the river

Pyqt interface production (login + jump page)

Sword finger offer 28 Symmetric binary tree

C language to realize mine sweeping
随机推荐
Luogu p4047 [jsoi2010] tribal division solution
Output student grades
Zzuli:1053 sine function
Zzuli:1058 solving inequalities
Luogu p5536 [xr-3] core city solution
如何查询淘宝天猫的宝贝类目
NOI OPENJUDGE 1.5(23)
Zzuli:1055 rabbit reproduction
7-24 reduction of the simplest fraction (rolling Division)
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
C language to implement a password manager (under update)
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
Find the sum of the elements of each row of the matrix
分布式事务(Seata) 四大模式详解
Programming language: the essence of type system
Address book sorting
ZABBIX saves the page blank after adding calculated items
puzzle(016.4)多米诺效应
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
Detailed explanation of four modes of distributed transaction (Seata)