system.c

来自「标准c库代码,可以应用于各个系统提供了大量的基本函数」· C语言 代码 · 共 112 行

C
112
字号
/*FUNCTION<<system>>---execute command stringINDEX	systemINDEX	_system_rANSI_SYNOPSIS	#include <stdlib.h>	int system(char *<[s]>);	int _system_r(void *<[reent]>, char *<[s]>);TRAD_SYNOPSIS	#include <stdlib.h>	int system(<[s]>)	char *<[s]>;	int _system_r(<[reent]>, <[s]>)	char *<[reent]>;	char *<[s]>;DESCRIPTIONUse <<system>> to pass a command string <<*<[s]>>> to <</bin/sh>> onyour system, and wait for it to finish executing.Use `<<system(NULL)>>' to test whether your system has <</bin/sh>>available.The alternate function <<_system_r>> is a reentrant version.  Theextra argument <[reent]> is a pointer to a reentrancy structure.RETURNS<<system(NULL)>> returns a non-zero value if <</bin/sh>> is available, and<<0>> if it is not.With a command argument, the result of <<system>> is the exit statusreturned by <</bin/sh>>.PORTABILITYANSI C requires <<system>>, but leaves the nature and effects of acommand processor undefined.  ANSI C does, however, specify that<<system(NULL)>> return zero or nonzero to report on the existence ofa command processor.POSIX.2 requires <<system>>, and requires that it invoke <</bin/sh>>.Supporting OS subroutines required: <<_exit>>, <<execve>>, <<fork>>,<<wait>>.*/#include <stddef.h>#include <stdlib.h>#include <_syslist.h>#ifndef NO_EXECextern int execve ();extern int fork ();extern int wait ();extern char *environ[];#endifint_system_r (ptr, s)     struct _reent *ptr;     _CONST char *s;{  char *argv[4];  int pid, status;#ifdef NO_EXEC  return 0;#else  argv[0] = "sh";  argv[1] = "-c";  if (s == NULL)    argv[2] = (char *) "exit 0";  else    argv[2] = (char *) s;  argv[3] = NULL;  if ((pid = _fork_r (ptr)) == 0)    {      _execve ("/bin/sh", argv, environ);      exit (100);    }  else if (pid == -1)    return s == NULL ? 0 : -1;  else    {      _wait_r (ptr, &status);      status = (status >> 8) & 0xff;      return s == NULL ? status == 0 : status;    }#endif}#ifndef _REENT_ONLYintsystem (s)     _CONST char *s;{  return _system_r (_REENT, s);}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?