#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>

int main(int argc, char *argv[]) {
  int rc = fork();
  if (rc < 0) {
    // fork başarısız oldu.
    fprintf(stderr, "fork failed\n");
    exit(1);
  } else if (rc == 0) {
    // Child içinde stdout bağlantısını bir dosyaya yönlendir.
    close(STDOUT_FILENO);
    open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC,
         S_IRWXU);
    // Şimdi exec ile wc programını yükle.
    char *myargs[3];
    myargs[0] = strdup("wc");   // Çalıştırılacak programın adı: wc.
    myargs[1] = strdup("p4.c"); // Satır, sözcük ve bayt sayıları hesaplanacak dosya.
    myargs[2] = NULL;           // NULL, argüman dizisinin sonunu belirtir.
    execvp(myargs[0], myargs);  // wc programını mevcut process içinde çalıştır.
  } else {
    // Parent process bu dalı yürütür.
    int rc_wait = wait(NULL);
  }
  return 0;
}
