SHARE
TWEET

Untitled

a guest Jan 22nd, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _XOPEN_SOURCE 700
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/wait.h>
  12.  
  13. #define osAssert(cond) osError(cond, __LINE__)
  14. void osError(bool cond, int line);
  15.  
  16. #define MAXSZ (256)
  17. #define MAX_BYTES (4096)
  18. #define RD_END (0)
  19. #define WR_END (1)
  20.  
  21. int main(int argc, char** argv){
  22.  
  23.     osAssert(argc == 2);
  24.     FILE* file;
  25.     osAssert((file = fopen(argv[1], "r")) != NULL);
  26.     char* program[2];
  27.     program[0] = malloc(sizeof(char)*MAXSZ);
  28.     program[1] = malloc(sizeof(char)*MAXSZ);
  29.     int c2p[2];
  30.     int errLog = open("errors.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
  31.     osAssert(errLog != -1);
  32.     char buf[MAX_BYTES];
  33.     while(fscanf(file, "%s %s", program[0], program[1]) == 2){
  34.         osAssert(pipe(c2p) != -1);
  35.         if(fork() == 0){
  36.             close(c2p[RD_END]);
  37.             close(errLog);
  38.             dup2(c2p[WR_END], STDERR_FILENO);
  39.             execlp(program[0], program[0], program[1], NULL);
  40.             exit(EXIT_FAILURE);
  41.         }
  42.         close(c2p[WR_END]);
  43.         int status;
  44.         osAssert(wait(&status) != -1);
  45.         int bytesRead;
  46.         if(WEXITSTATUS(status) != EXIT_SUCCESS){
  47.             osAssert((bytesRead = read(c2p[RD_END], buf, MAX_BYTES)) != -1);
  48.             osAssert(write(errLog, buf, bytesRead) != -1);
  49.         }
  50.         close(c2p[RD_END]);
  51.     }
  52.     close(errLog);
  53.     fclose(file);
  54.  
  55.     exit(EXIT_SUCCESS);
  56. }
  57.  
  58. void osError(bool cond, int line){
  59.     if(!cond){
  60.         fprintf(stderr, "Greska na liniji %d", line);
  61.         perror("Greska");
  62.         exit(EXIT_FAILURE);
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top