Openldap服务器配置错误解决。
2012-04-19 14:02
218 查看
This article will outline how to call shell commands from your C program using the
system() call and the popen() functions. The system() call and the popen()
call each have their place and differ enough to give an example and explanation of both.
this string should consist of the shell command you wish to call.
Its return value must be run through another function call to retrieve
the result of your executed shell command. For example,
if you typed ls on the command line, its return value is 0 if it was successful.
Let me show you:
The return value is -1 on error, or the exit code of the command WEXITSTATUS(res). This way if you were calling a Bash script it may have various exit codes,
this allows you to get that exit code. The downside of the system() function is you couldn’t grab say data in a file, or if you were to “cat” a file you could only get the exit code
as the return value and not the contents of that file. This is where popen() comes into play.
This example shows grabbing a process listing from the shell and reading it back through the pipe then displaying it to the screen.
Depending on what you need to achieve and what results you need from the shell command, system() and popen() should cover the basic for executing shell commands.
system() call and the popen() functions. The system() call and the popen()
call each have their place and differ enough to give an example and explanation of both.
Execute a Shell Command Using system()
The system() function takes a string as its argument,this string should consist of the shell command you wish to call.
Its return value must be run through another function call to retrieve
the result of your executed shell command. For example,
if you typed ls on the command line, its return value is 0 if it was successful.
Let me show you:
int run_sys_call(char *buffer) { int res; res = system(buffer); if ( WEXITSTATUS(res) != 0 ) { syslog(LOG_CRIT," System call failed.\n"); syslog(LOG_CRIT," %s\n",buffer); } return res; }OR more simply:
int res; // This will execute the LS command in the current directory, // unfortunately the result is just an exit code. res = system("ls -l");This function takes a character array, then passes this string into your “system()” function.
The return value is -1 on error, or the exit code of the command WEXITSTATUS(res). This way if you were calling a Bash script it may have various exit codes,
this allows you to get that exit code. The downside of the system() function is you couldn’t grab say data in a file, or if you were to “cat” a file you could only get the exit code
as the return value and not the contents of that file. This is where popen() comes into play.
Execute a Shell Command Using popen()
The popen() command allows a user to execute the shell command exclusively as a read or a write. The function is a pipe stream that opens a process by creating that pipe, forking, and invoking a shell! All from one command, not too bad, not too bad at all. The return value from popen() is a normal standard I/O stream and must be closed with pclose() rather than fclose(). Writing to such a stream writes to the standard input of the command; the command’s standard output is the same as that of the process that called popen(), unless this is altered by the command itself. Reading from a “popened” stream reads the command’s standard output, and the command’s standard input is the same as that of the process that called popen().#define COMMAND_LEN 20 #define DATA_SIZE 512 void get_popen_data() FILE *pf; char command[COMMAND_LEN]; char data[DATA_SIZE]; // Execute a process listing sprintf(command, "ps aux wwwf"); // Setup our pipe for reading and execute our command. pf = popen(command,"r"); if(!pf){ fprintf(stderr, "Could not open pipe for output.\n"); return; } // Grab data from process execution fgets(data, DATA_SIZE , pf); // Print grabbed data to the screen. fprintf(stdout, "-%s-\n",data); if (pclose(pf) != 0) fprintf(stderr," Error: Failed to close command stream \n"); return; }The popen() function takes either “r” for reading or “w” for writing.
This example shows grabbing a process listing from the shell and reading it back through the pipe then displaying it to the screen.
Depending on what you need to achieve and what results you need from the shell command, system() and popen() should cover the basic for executing shell commands.
相关文章推荐
- SQL Server 2000安装错误“安装配置服务器失败”解决方法
- HTTP 错误 401.2 - 未授权:服务器配置导致登录失败(已解决)
- 监听独立于数据库服务器的配置,解决ORA-12520及ORA-12545错误
- Nginx服务器中414错误和504错误的配置解决方法
- 监听独立于数据库服务器的配置,解决ORA-12520及ORA-12545错误
- 解决Asp.net Web网站发布到服务器上运行时,某界面传参数过多,造成的“此请求的查询字符串的长度超过配置的 maxQueryStringLength 值”错误。
- SQL安装时出错,提示:安装程序配置服务器失败 参考服务器错误日志。针对其中一种情况的解决方法!
- 解决配置ASP.NET Web服务器出现的错误提示“当前服务器不可用”
- smb服务器配置过程遇到错误及解决
- 解决和剖析IIS错误提示:您不具备查看该目录或页面的权限,因为访问控制列表(ACL)对Web 服务器上的该资源进行了配置
- SQL Server 2000 安装错误“安装配置服务器失败”解决方法
- 解决【HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容】
- 解决在IIS7.0上部署ASP.NET1.1站点时【“/”应用程序中的服务器错误。无法识别的配置节“system.webServer”】的问题
- SQL错误提示档案(2):SQL Server 2000安装错误“安装配置服务器失败”解决方法
- 解决没有my.ini配置文件MySql Error Code: 2006 – MySQL 服务器已离线错误
- kamailio服务器负载均衡配置过程中,将几个sip服务器同时指向同一个mysql,出现的错误解决
- fedora9下嵌入式开发环境之NFS服务器的配置及常见错误的解决
- 解决关于"因为配置类型不正确,系统无法开始服务器进程..."的错误
- IIS7部署项目时提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败。"的解决办法
- 配置MyEclipse的WebLogic服务器的配置及错误解决