c语言的linux命令 c语言实现linux命令

如何在C语言编程中调用linux系统终端下的命令system(执行shell 命令)
相关函数 fork,execve,waitpid,popen
表头文件 #includestdlib.h
定义函数 int system(const char * string);
函数说明 system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程 。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略 。
返回值 如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1 。若参数string为空指针(NULL),则返回非零值 。如果system()调用成功则最后会返回执行shell命令后的返回值 , 但是此返回值也有可能为system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功 。
附加说明 在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题 。
范例 #includestdlib.h
main()
{
system(“ls -al /etc/passwd /etc/shadow”);
}
执行 -rw-r--r-- 1 root root 705 Sep 3 13 :52 /etc/passwd
-r--------- 1 root root 572 Sep 2 15 :34 /etc/shadow
linux中cp命令如何用 C语言实现1,首先需要了解cp的原理 。
2,可以参考cp的源码去了解其原理
3 , cp命令的源码可以在linux内核中找到 。
4,或者下载busybox其中也会有cp的源码
只有了解其原理之后才能谈如何实现 。参考代码如下:
#include stdio.h
#include stdlib.h
#include sys/stat.h
#include sys/types.h
#include fcntl.h
#include errno.h
#include unistd.h
#include string.h
#define BUF_SIZE 1024
#define PATH_LEN 128
void my_err(char *err_string, int line )
{
fprintf(stderr,"line:%d ",line);
perror(err_string);
exit(1);
}
void copy_data(const int frd,const int fwd)
{
int read_len = 0, write_len = 0;
unsigned char buf[BUF_SIZE], *p_buf;
while ( (read_len = read(frd,buf,BUF_SIZE)) ) {
if (-1 == read_len) {
my_err("Read error", __LINE__);
}
else if (read_len0) { //把读取部分写入目标文件
p_buf = buf;
while ( (write_len = write(fwd,p_buf,read_len)) ) {
if(write_len == read_len) {
break;
}
else if (write_len0) { //只写入部分
p_buf= write_len;
read_len -= write_len;
}
else if(-1 == write_len) {
my_err("Write error", __LINE__);
}
}
if (-1 == write_len) break;
}
}
}
int main(int argc, char **argv)
{
int frd, fwd; //读写文件描述符
int len = 0;
char *pSrc, *pDes; //分别指向源文件路径和目标文件路径
struct stat src_st,des_st;
if (argc3) {
printf("用法 ./MyCp 源文件路径 目标文件路径\n");
my_err("arguments error ", __LINE__);
}
frd = open(argv[1],O_RDONLY);
if (frd == -1) {
my_err("Can not opne file", __LINE__);
}
if (fstat(frd,src_st) == -1) {
my_err("stat error",__LINE__);
}
/*检查源文件路径是否是目录*/
if (S_ISDIR(src_st.st_mode)) {
my_err("略过目录",__LINE__);
}
pDes = argv[2];
stat(argv[2],des_st);
if (S_ISDIR(des_st.st_mode)) { //目标路径是目录,则使用源文件的文件名
len = strlen(argv[1]);
pSrc = https://www.04ip.com/post/argv[1](len-1); //指向最后一个字符
/*先找出源文件的文件名*/
while (pSrc = https://www.04ip.com/post/argv[1]*pSrc !='/') {
pSrc--;
}
pSrc;//指向源文件名
len = strlen(argv[2]);
// . 表示复制到当前工作目录
if (1 == len'.' == *(argv[2])) {
len = 0; //没有申请空间,后面就不用释放
pDes = pSrc;
}
else { //复制到某目录下,使用源文件名
pDes = (char *)malloc(sizeof(char)*PATH_LEN);
if (NULL == pDes) {
my_err("malloc error ", __LINE__);
}
strcpy(pDes,argv[2]);
if ( *(pDes (len-1)) != '/' ) { //目录缺少最后的'/',则补上’/‘
strcat(pDes,"/");
}
strcat(pDes len,pSrc);
}
}
/* 打开目标文件,使权限与源文件相同*/
fwd = open(pDes,O_WRONLY | O_CREAT | O_TRUNC,src_st.st_mode);
if (fwd == -1) {
my_err("Can not creat file", __LINE__);
}
copy_data(frd,fwd);
//puts("end of copy");
if (len0pDes != NULL)
free(pDes);
close(frd);
close(fwd);
return 0;
}
怎么用C语言实现linux的命令命令是查询当前登录的每个用户,它的输出包括用户名、终端类型、登录日期及远程主机,在Linux系统中输入who命令输出如下:
我们先man一下who,在帮助文档里可以看到 , who命令是读取/var/run/utmp文件来得到以上信息的 。
我们再man一下utmp,知道utmp这个文件,是二进制文件 , 里面保存的是结构体数组,这些数组是struct utmp结构体的 。
struct utmp {
shortut_type;
pid_tut_pid;
charut_line[UT_LINESIZE];
charut_id[4];
charut_user[UT_NAMESIZE];
charut_host[UT_HOSTSIZE];
struct {
int32_t tv_sec;
int32_t tv_usec;
} ut_tv;
/***等等***/
};
要实现who只需要把utmp文件的所有结构体扫描过一遍,把需要的信息显示出来就可以了,我们需要的信息有ut_user、ut_line、ut_tv、ut_host 。
老师给的初始代码:who1.c运行结果如下:
需要注意的是utmp中所保存的时间是以秒和微妙来计算的,所以我们需要把这个时间转换为我们能看懂的时间,利用命令man -k time | grep 3搜索C语言中和时间相关的函数:
经过搜索发现了一个ctime()函数,似乎可以满足我们的需求,于是对代码中关于时间的printf进行修改:
printf("%s",ctime(utbufp-ut_time));
编译运行发现出来的结果虽然已经转换成了我们能看懂的时间格式,但是很明显这个时间是错的:
搜索一下ut_time这个宏,发现它被定义为int32_t类型:
但是ctime()函数中要求参数的类型是time_t类型,所以重新定义一下类型 , 编译运行之后,发现时间已经改成了正确的,但是发现()中的内容被换行了,猜想ctime()函数的返回值可能自动在最后补了一个字符\n:
一开始想通过\r\b来实现“退行”,但实践后发现并不可取 , 最后考虑到直接修改字符串中最后一个字符为\0,让其字符串结束,使输出达到与系统who命令一样的效果,即在输出语句前添加如下代码:
cp[strlen(cp)-1] = '\0'
最后编译执行效果,发现解决了该问题:
虽然能看出基本上和who指令的执行结果一致 , 但是并非完全一样,主要在两点,第一是时间格式不一样,第二个是比who执行的结果多了几条,需要注意的是utmp中保存的用户,不仅仅是已经登陆的用户,还有系统的其他服务所需要的“用户”,所以在显出所有登陆用户的时候,应该过滤掉其他用户,只保留登陆用户 。我们可以通过ut_type来区别,登陆用户的ut_type是USER_PROCESS 。
先用if语句对执行结果进行过滤,效果如下:
接着解决时间格式问题 , 利用man命令收到了两个非常有用的函数:localtime()和strftime(),localtime()是把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间 , strftime()则是用来定义时间格式的,如:年-月-日,利用这两个函数对时间进行修改后,结果显示终于和系统中who命令一模一样:
最终完整的代码如下:
#include stdio.h
#include stdlib.h
#include utmp.h
#include fcntl.h
#include unistd.h
#include time.h
#define SHOWHOST
void show_time(long timeval){
char format_time[40];
struct tm *cp;
cp = localtime(timeval);
strftime(format_time,40,"%F %R",cp);
printf("%s",format_time);
}
int show_info( struct utmp *utbufp )
{
if(utbufp-ut_type == USER_PROCESS){
printf("%-8.8s", utbufp-ut_name);
printf(" ");
printf("%-8.8s", utbufp-ut_line);
printf("");
show_time(utbufp-ut_time);
printf(" ");
【c语言的linux命令 c语言实现linux命令】#ifdefSHOWHOST
printf("(%s)", utbufp-ut_host);
#endif
printf("\n");
}
return 0;
}
int main()
{
struct utmpcurrent_record;
intutmpfd;
intreclen = sizeof(current_record);
if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){
perror( UTMP_FILE );
exit(1);
}
while ( read(utmpfd, current_record, reclen) == reclen )
show_info(current_record);
close(utmpfd);
return 0;
}
关于c语言的linux命令和c语言实现linux命令的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读