Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6.  
  7. int main() {
  8.  
  9. int fd[2];
  10.  
  11. if(pipe(fd) == -1) {
  12. perror("Pipe failed");
  13. return 1;
  14. }
  15.  
  16. int id;
  17. char password[256];
  18.  
  19. if (fork() == 0) {
  20.  
  21. dup2(fd[0],STDIN_FILENO);
  22. dup2(fd[0],STDIN_FILENO);
  23. dup2(fd[1],STDOUT_FILENO);
  24.  
  25. execlp("./validate", "./validate", (char*)NULL);
  26. perror("Exec error!");
  27. exit(0);
  28.  
  29. } else {
  30.  
  31. printf("Introduza o ID do User: ");
  32. scanf("%d", &id);
  33.  
  34. getchar();
  35.  
  36. printf("Introduza a Password: ");
  37. fgets(password, 255, stdin);
  38.  
  39. close(fd[0]);
  40. write(fd[1], &id, sizeof(int));
  41. write(fd[1], password, sizeof(password));
  42.  
  43. int status;
  44. wait(&status);
  45.  
  46. status = WEXITSTATUS(status);
  47.  
  48. if (status == 3) {
  49. printf("No such user\n");
  50. } else if (status == 2) {
  51. printf("Invalid password\n");
  52. } else if (status == 1) {
  53. printf("Password verified\n");
  54. }
  55.  
  56. }
  57.  
  58. return 0;
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement