Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7.  
  8. int main(int argc, char** argv)
  9. {
  10. if(argc == 1)
  11. {
  12. puts("No valid argument!");
  13. printf("You are in %s", get_current_dir_name());
  14. exit(0);
  15. }
  16. int file = open(argv[1], O_RDONLY);
  17. int errornumber = errno;
  18. if(file == -1)
  19. {
  20. if(errornumber == 2)
  21. {
  22. puts("No such file in directory!");
  23. exit(0);
  24. }
  25. printf("errno %4d \n", errornumber);
  26. }
  27. else
  28. {
  29. struct stat fileCheck;
  30. stat(argv[1], &fileCheck);
  31.  
  32. if(S_ISREG(fileCheck.st_mode))
  33. {
  34. printFile(argv[1]);
  35. }
  36. if(S_ISDIR(fileCheck.st_mode))
  37. {
  38.  
  39. }
  40. }
  41. return 0;
  42. }
  43.  
  44. void printFile(char* filename)
  45. {
  46. char output[1024];
  47. FILE* file;
  48. size_t nread;
  49.  
  50. file = fopen(filename, "rb");
  51.  
  52. int counter = 0;
  53.  
  54. if(file)
  55. {
  56. fwrite(output, 1, 1024, stdout);
  57. fclose(file);
  58. }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement