Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. //------------------------------------------------------------------------
  2. // NAME: Radoslav Hubenov
  3. // CLASS: XIa
  4. // NUMBER: 22
  5. // PROBLEM: #1
  6. // FILE NAME: main.c (unix file name)
  7. // FILE PURPOSE:
  8. // Main файлът ми, който свързва всички останали файлове.
  9. // Програмата реализира стандартната UNIX команда tail.
  10. //------------------------------------------------------------------------
  11.  
  12. #include "tail.h"
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include "read_stdin.h"
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20.  
  21.  
  22. int main(int argc, char* argv[]){
  23. char left_point[5] = "==> ";
  24. char right_point[5] = " <==\n";
  25. if(argc == 1){
  26. read_stdin();
  27. return 0;
  28. }
  29. for(int i = 1; i < argc; i++){
  30. if(!strcmp(argv[i], "-")){// Allow the user to write to stdin if '-' is one of the arguments
  31. read_stdin();
  32. continue;
  33. }
  34. if( access( argv[i], F_OK ) == -1 ) { //Check if file is opened succesfuly for error handling
  35. write(STDERR_FILENO, "tail: ", 6);
  36. write(STDERR_FILENO, "cannot open '", 13);
  37. write(STDERR_FILENO, argv[i], strlen(argv[i]));
  38. write(STDERR_FILENO, "' for reading: ", 15);
  39. perror("");
  40. continue;
  41. }
  42. if(argc > 2){ // Printing the name of the file if more than one file is opened
  43. write(STDOUT_FILENO, &left_point, strlen(left_point));
  44. write(STDOUT_FILENO, argv[i], strlen(argv[i]));
  45. write(STDOUT_FILENO, &right_point, strlen(right_point));
  46. }
  47. tail(argv[i]);
  48. if(i != argc - 1){
  49. write(STDOUT_FILENO, "\n", 1);
  50. }
  51. }
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement