Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. //--------------------------------------------
  2. // NAME: Daniel Kirov
  3. // CLASS: XI a
  4. // NUMBER: 10
  5. // PROBLEM: #1
  6. // FILE NAME: tail.c
  7. // FILE PURPOSE:
  8. // This is my only version of the homework in which we have to
  9. // implement tail.
  10. //---------------------------------------------
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <stdbool.h>
  19. #include <errno.h>
  20. #define BUFF_SIZE 420
  21.  
  22. //--------------------------------------------
  23. // FUNCTION: openFile
  24. // a function to open a file and check for opening errors
  25. // PARAMETERS:
  26. // int descriptor - this is the index of the file i open
  27. // const char* filename - this the name of the file that has to be open
  28. //----------------------------------------------
  29. int openFile(const char* filename)
  30. {
  31. int descriptor = open(filename, O_RDONLY);
  32. int errsv = errno;
  33.  
  34. const char message[100];
  35. strcpy(message, "tail: cannot open `");
  36. strcat(message, filename);
  37. strcat(message, "` for reading: No such file or directory");
  38.  
  39.  
  40. const char message_[100];
  41. strcpy(message_, "tail: cannot open ’");
  42. strcat(message_, filename);
  43. strcat(message_, "’ for reading: Permission denied");
  44.  
  45. if(descriptor < 0)
  46. {
  47. if(errsv == ENOENT)
  48. {
  49. write(STDOUT_FILENO, message, strlen(message));
  50. }
  51. else if(errsv == EACCES)
  52. {
  53. write(STDOUT_FILENO, message_, strlen(message_));
  54. }
  55. return -1;
  56. }
  57. else
  58. {
  59. return descriptor;
  60. }
  61. }
  62. //--------------------------------------------
  63. // FUNCTION: lineCount
  64. // A function to count the new lines in a file
  65. // PARAMETERS:
  66. // int descriptor - this is the index of the file i open
  67. //----------------------------------------------
  68. int lineCount(int descriptor)
  69. {
  70. int status_read = 1;
  71. char buff[BUFF_SIZE];
  72. int i = 0;
  73. int lines = 0;
  74.  
  75.  
  76. while((status_read = read(descriptor, buff, BUFF_SIZE)) > 0)
  77. {
  78. for(i = 0; i < status_read; i++)
  79. {
  80. if(buff[i] == '\n')
  81. {
  82. lines++;
  83. }
  84. }
  85. }
  86. return lines;
  87. }
  88. //--------------------------------------------
  89. // FUNCTION: linesPrint
  90. // A function to print the last 10 lines in a file
  91. // PARAMETERS:
  92. // int descriptor - this is the index of the file i open
  93. // int lines - this is the number of the new lines in a file
  94. //----------------------------------------------
  95. void linesPrint(int descriptor,int lines)
  96. {
  97. int status_read = 1;
  98. char buff[BUFF_SIZE];
  99. bool printing = false;
  100. int lines2 = 0;
  101. int i = 0;
  102.  
  103. lseek(descriptor, 0, SEEK_SET);
  104.  
  105. if(lines > 10)
  106. {
  107. while((status_read = read(descriptor, buff, BUFF_SIZE)) > 0)
  108. {
  109. for(i = 0; i < status_read; i++)
  110. {
  111. if(printing)
  112. {
  113. write(STDOUT_FILENO, &buff[i], 1);
  114. }
  115. else
  116. {
  117. if(buff[i] == '\n')
  118. {
  119. lines2++;
  120. }
  121. if(lines2 == (lines - 9))
  122. {
  123. printing = true;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. else
  130. {
  131. while((status_read = read(descriptor, buff, BUFF_SIZE)) > 0)
  132. {
  133. for(i = 0; i < status_read; i++)
  134. {
  135. write(STDOUT_FILENO, &buff[i], 1);
  136. }
  137. }
  138. }
  139.  
  140.  
  141. }
  142. //--------------------------------------------
  143. // FUNCTION: tail
  144. // A function to execute tail
  145. // PARAMETERS:
  146. // const char* filename - this the name of the file that has to be open
  147. //----------------------------------------------
  148. void tail(const char* filename)
  149. {
  150. int lines;
  151. int descriptor = openFile(filename);
  152. lines = lineCount(descriptor);
  153. linesPrint(descriptor, lines);
  154. close(descriptor);
  155. }
  156.  
  157. int main(int argc, char *argv[] )
  158. {
  159. int i = 0;
  160.  
  161. if(argc > 2)
  162. {
  163. for(i = 1; i < argc; i++)
  164. {
  165. if(argv[i] == '-')
  166. {
  167. int temp_file = open("temp_file.txt", O_RDONLY | O_CREAT | O_TRUNC);
  168. char new_buff[BUFF_SIZE];
  169. int status_read = 1;
  170.  
  171. while(status_read > 0)
  172. {
  173. status_read = read(STDIN_FILENO, new_buff, BUFF_SIZE);
  174. int written = 0;
  175. int status_write;
  176. while(written < status_read)
  177. {
  178. status_write = write(temp_file, new_buff + written, status_read - written);
  179. written = written + status_read;
  180. }
  181. }
  182. tail(temp_file);
  183. }
  184. else
  185. {
  186. const char filename[100];
  187. strcpy(filename, "==> ");
  188. strcat(filename, argv[i]);
  189. strcat(filename, " <==\n");
  190. write(STDOUT_FILENO, filename, strlen(filename));
  191. tail(argv[i]);
  192. }
  193. }
  194. }
  195. else if(argc == 2)
  196. {
  197. tail(argv[1]);
  198. }
  199.  
  200. return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement