Can somebody help me to make this C POSIX code to port it to run under Windows? (no Cygwin, MinGW just Windows native APIs and Visual Studio), tried many things without any luck.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
void usage(char *s);
int test_function(char *host);
FILE *infile, *outfile;
int numforks = 0;
void usage(char *s) {
printf("Usage: %s <input> <childs>\n", s);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
char buf[1024];
time_t start;
if (argc < 2)
usage(argv[0]);
outfile = fopen("test.txt", "a+");
infile = fopen(argv[1], "r");
printf("[*] In: %s Childs: %s\n", argv[1], argv[2]);
start = time(0);
while (!feof(infile)) {
fgets((char *)&buf, sizeof(buf), infile);
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
if (!(fork())) {
test_function(buf);
exit(0);
} else {
numforks++;
if (numforks > atoi(argv[2]))
for (numforks; numforks > atoi(argv[2]); numforks--)
wait(NULL);
}
}
fclose(infile);
fclose(outfile);
printf("[*] Completed in: %lu secs\n", (time(0) - start));
exit(EXIT_SUCCESS);
}
int test_function(char *host) {
usleep(2000000); // for debugging
fprintf(outfile, "%s\n", host);
printf("%s\n", host);
return 1;
}
I tried to do this:
#include <stdio.h>
#include <string.h>
#ifdef LINUX
#include <unistd.h>
#endif
#include <time.h>
#include <stdlib.h>
#ifdef WIN32
#define fork _forkWin32
#define usleep Sleep
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <process.h>
#include <windows.h>
using namespace std;
#endif
void usage(char *s);
#ifdef WIN32
unsigned int _stdcall test_function(void *ptr);
#else
int test_function(char *host);
#endif
FILE *infile, *outfile;
int numforks = 0;
char proc;
void usage(char *s) {
printf("Usage: %s <input> <threads>\n", s);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
char buf[1024];
time_t start;
//#ifdef WIN32
//HANDLE child;
//#endif
if (argc < 3)
usage(argv[0]);
outfile = fopen("test.txt", "a+");
infile = fopen(argv[1], "r");
printf("[*] In: %s Threads: %s\n", argv[1], argv[2]);
start = time(0);
HANDLE child;
while (!feof(infile)) {
fgets((char *)&buf, sizeof(buf), infile);
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
#ifdef WIN32
child = (HANDLE) _beginthreadex(NULL, 0, test_function, buf, 0, NULL);
if (child != 0) {
#else
if (!(fork())) {
test_function(buf);
#endif
exit(0);
} else {
numforks++;
if (numforks > atoi(argv[2]))
for (numforks; numforks > atoi(argv[2]); numforks--)
#ifdef WIN32
WaitForSingleObject(child, INFINITE);
#else
wait(NULL);
#endif
}
}
fclose(infile);
fclose(outfile);
printf("[*] Completed in: %lu secs\n", (time(0) - start));
exit(EXIT_SUCCESS);
}
#ifdef WIN32
unsigned int test_function(void *ptr) {
char* host = (char*)ptr;
#else
int test_function(char *host) {
#endif
usleep(2000000); // for debugging
fprintf(outfile, "%s\n", host);
printf("%s\n", host);
return 1;
}
Doesn't seem to do anything. What am I doing wrong? Is it all wrong?
fork
function (actually a system call) is only in Posix. You want to use a Posix environment for Windows (and Cygwin might help).fork
works in e.g. Cygwin is to useCreateProcess
to load the program again, and insert custom code to jump to the point where the fork call is made in the new process. It's a hack since Windows otherwise doesn't have functionality similar to the fork call. Cygwin is open-source, so you can see how they do it if you want to know the exact technical details.