资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言调用linux命令 c语言调用linux内核函数

我想在linux下写一个c程序调用linux的可执行文件或者程序,怎么做

Linux中启动另一个可执行文件或程序用system函数最理想了,这个函数将在你编写的那个程序的内部启动另一个程序,从而创建一个新进程,并等待这个进程执行完毕退出。如果正常执行,system函数将返回被执行程序的退出码;如果无法运行这个程序,将返回错误代码127;如果是其他错误,返回-1。这个函数的原型是:

员工经过长期磨合与沉淀,具备了协作精神,得以通过团队的力量开发出优质的产品。创新互联坚持“专注、创新、易用”的产品理念,因为“专注所以专业、创新互联网站所以易用所以简单”。公司专注于为企业提供成都网站建设、网站制作、微信公众号开发、电商网站开发,重庆小程序开发,软件定制开发等一站式互联网企业服务。

#include stdlib.h

int system(const char *string);

参数string是将要执行的程序的命令字符串。

还有一种执行外部程序的方法是exec系列函数,但这个系列的函数都是将当前进程的替换成新进程,也就是说原来的进程不存在了。

在Linux系统中,如何运行一个C语言程序?

我不太明白你说的是什么意思,Linux下的C编程一般是通过gcc实现的。\x0d\x0a例如,创建了一个hello.c文本,在文本中写入\x0d\x0a#include\x0d\x0aintmain(void)\x0d\x0a{\x0d\x0aprintf(“helloworld!!”);\x0d\x0areturn0;\x0d\x0a}\x0d\x0a然后在终端输入\x0d\x0a$gcc_ohellohello.c\x0d\x0a$/tmp/hello\x0d\x0a注:hello.c文件放在/tmp目录下,通过gcc-ohellohello.c命令生成一个hello文件,它是一个可执行文件,然后直接执行,就可以运行该程序了。

如何用c语言实现 linux的rm命令

./rm filename和

在你的bash里面输入rm filename本质不是一样的么

就是把那个rm的实现放在你的自己的mini bash里面就可以了啊

调用

remove(filename);

就行了

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_len  0) { //把读取部分写入目标文件

p_buf = buf;

while ( (write_len = write(fwd,p_buf,read_len)) ) {

if(write_len == read_len) {

break;

}

else if (write_len  0) { //只写入部分

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 (argc  3) {

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 = argv[1] + (len-1); //指向最后一个字符

/*先找出源文件的文件名*/

while (pSrc = 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 (len  0  pDes != NULL)

free(pDes);

close(frd);

close(fwd);

return 0;

}


本文名称:c语言调用linux命令 c语言调用linux内核函数
本文链接:http://cdkjz.cn/article/dopdidc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220