xref: /inferno-os/utils/awk/missing95.c (revision 5d0c4cf3fc288434c41cba52dd998ab1d7375a7b)
1  /* popen and pclose are not part of win 95 and nt,
2     but it appears that _popen and _pclose "work".
3     if this won't load, use the return NULL statements. */
4  
5  #include <stdio.h>
6  FILE *popen(char *s, char *m) {
7  	return _popen(s, m);	/* return NULL; */
8  }
9  
10  int pclose(FILE *f) {
11  	return _pclose(f);	/* return NULL; */
12  }
13  
14  #include <windows.h>
15  #include <winbase.h>
16  #include <winsock.h>
17  
18  /* system doesn't work properly in some 32/64 (WoW64) combinations */
19  int system(char *s) {
20  	int status, n;
21  	PROCESS_INFORMATION pinfo;
22  	STARTUPINFO si;
23  	char *cmd;
24  	char app[256];
25  	static char cmdexe[] = "\\cmd.exe";
26  
27  	memset(&si, 0, sizeof(si));
28  	si.cb = sizeof(si);
29  //	si.dwFlags = STARTF_USESHOWWINDOW;
30  //	si.wShowWindow = SW_SHOW;
31  
32  	n = GetSystemDirectory(app, sizeof(app)-sizeof(cmdexe));
33  	if(n > sizeof(app))
34  		return -1;
35  	strcat_s(app, sizeof(app), cmdexe);
36  	n = strlen(s)+20;
37  	cmd = malloc(n);
38  	if(cmd == NULL)
39  		return -1;
40  	strcpy_s(cmd, n, "cmd.exe /c");
41  	strcat_s(cmd, n, s);
42  	if(!CreateProcess(app, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE, NULL/* env*/, NULL /*wdir*/, &si, &pinfo)){
43  		fprintf(stderr, "can't create process %s %d\n", s, GetLastError());
44  		free(cmd);
45  		return -1;
46  	}
47  	free(cmd);
48  	if(WaitForSingleObject(pinfo.hProcess, INFINITE) == WAIT_FAILED)
49  		return -1;
50  	if(!GetExitCodeProcess(pinfo.hProcess, &status))
51  		status = 1;
52  	//fprintf(stderr, "status %d\n", status);
53  	return status;
54  }
55