Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------
- // NAME: Daniel Kirov
- // CLASS: XI a
- // NUMBER: 10
- // PROBLEM: #1
- // FILE NAME: tail.c
- // FILE PURPOSE:
- // This is my only version of the homework in which we have to
- // implement tail.
- //---------------------------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdbool.h>
- #include <errno.h>
- #define BUFF_SIZE 420
- //--------------------------------------------
- // FUNCTION: openFile
- // a function to open a file and check for opening errors
- // PARAMETERS:
- // int descriptor - this is the index of the file i open
- // const char* filename - this the name of the file that has to be open
- //----------------------------------------------
- int openFile(const char* filename)
- {
- int descriptor = open(filename, O_RDONLY);
- int errsv = errno;
- const char message[100];
- strcpy(message, "tail: cannot open `");
- strcat(message, filename);
- strcat(message, "` for reading: No such file or directory");
- const char message_[100];
- strcpy(message_, "tail: cannot open ’");
- strcat(message_, filename);
- strcat(message_, "’ for reading: Permission denied");
- if(descriptor < 0)
- {
- if(errsv == ENOENT)
- {
- write(STDOUT_FILENO, message, strlen(message));
- }
- else if(errsv == EACCES)
- {
- write(STDOUT_FILENO, message_, strlen(message_));
- }
- return -1;
- }
- else
- {
- return descriptor;
- }
- }
- //--------------------------------------------
- // FUNCTION: lineCount
- // A function to count the new lines in a file
- // PARAMETERS:
- // int descriptor - this is the index of the file i open
- //----------------------------------------------
- int lineCount(int descriptor)
- {
- int status_read = 1;
- char buff[BUFF_SIZE];
- int i = 0;
- int lines = 0;
- while((status_read = read(descriptor, buff, BUFF_SIZE)) > 0)
- {
- for(i = 0; i < status_read; i++)
- {
- if(buff[i] == '\n')
- {
- lines++;
- }
- }
- }
- return lines;
- }
- //--------------------------------------------
- // FUNCTION: linesPrint
- // A function to print the last 10 lines in a file
- // PARAMETERS:
- // int descriptor - this is the index of the file i open
- // int lines - this is the number of the new lines in a file
- //----------------------------------------------
- void linesPrint(int descriptor,int lines)
- {
- int status_read = 1;
- char buff[BUFF_SIZE];
- bool printing = false;
- int lines2 = 0;
- int i = 0;
- lseek(descriptor, 0, SEEK_SET);
- if(lines > 10)
- {
- while((status_read = read(descriptor, buff, BUFF_SIZE)) > 0)
- {
- for(i = 0; i < status_read; i++)
- {
- if(printing)
- {
- write(STDOUT_FILENO, &buff[i], 1);
- }
- else
- {
- if(buff[i] == '\n')
- {
- lines2++;
- }
- if(lines2 == (lines - 9))
- {
- printing = true;
- }
- }
- }
- }
- }
- else
- {
- while((status_read = read(descriptor, buff, BUFF_SIZE)) > 0)
- {
- for(i = 0; i < status_read; i++)
- {
- write(STDOUT_FILENO, &buff[i], 1);
- }
- }
- }
- }
- //--------------------------------------------
- // FUNCTION: tail
- // A function to execute tail
- // PARAMETERS:
- // const char* filename - this the name of the file that has to be open
- //----------------------------------------------
- void tail(const char* filename)
- {
- int lines;
- int descriptor = openFile(filename);
- lines = lineCount(descriptor);
- linesPrint(descriptor, lines);
- close(descriptor);
- }
- int main(int argc, char *argv[] )
- {
- int i = 0;
- if(argc > 2)
- {
- for(i = 1; i < argc; i++)
- {
- if(argv[i] == '-')
- {
- int temp_file = open("temp_file.txt", O_RDONLY | O_CREAT | O_TRUNC);
- char new_buff[BUFF_SIZE];
- int status_read = 1;
- while(status_read > 0)
- {
- status_read = read(STDIN_FILENO, new_buff, BUFF_SIZE);
- int written = 0;
- int status_write;
- while(written < status_read)
- {
- status_write = write(temp_file, new_buff + written, status_read - written);
- written = written + status_read;
- }
- }
- tail(temp_file);
- }
- else
- {
- const char filename[100];
- strcpy(filename, "==> ");
- strcat(filename, argv[i]);
- strcat(filename, " <==\n");
- write(STDOUT_FILENO, filename, strlen(filename));
- tail(argv[i]);
- }
- }
- }
- else if(argc == 2)
- {
- tail(argv[1]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement