Commit fa2172be authored by Matthew Burket's avatar Matthew Burket
Browse files

First draft of the file operations

parent 659d2512
Loading
Loading
Loading
Loading
Loading
+28 −3
Original line number Diff line number Diff line
@@ -3,15 +3,40 @@
//

#include "file_operations.h"
#include "string.h"

/// This method gets the last
///
/// \param fp
/// \param path
/// \param output
/// \return
int get_last_line(FILE fp, char **output) {
int get_last_line(char *path, char *output) {
    char *currentLine = malloc(BUFFSIZE);
    char *lastLine = malloc(BUFFSIZE);
    FILE *fp = fopen(path, "r");
    while (fgets(currentLine, BUFFSIZE, fp)) {
        lastLine = strcpy(lastLine, currentLine);
    }
    strcpy(output, lastLine);
    free(lastLine);
    free(currentLine);


}
int get_specific_line(FILE fp, char **output) {
int get_specific_line(char *path, char *output, int finalLine) {
    char *currentLine = malloc(BUFFSIZE);
    char *lastLine = malloc(BUFFSIZE);
    FILE *fp = fopen(path, "r");
    int line = 1;
    while (fgets(currentLine, BUFFSIZE, fp)) {
        lastLine = strcpy(lastLine, currentLine);
        if (line == finalLine) {
            break;
        }
        line++;
    }
    strcpy(output, lastLine);
    free(lastLine);
    free(currentLine);

}
+5 −2
Original line number Diff line number Diff line
@@ -6,7 +6,10 @@
#define COMS352_SHELL_FILE_OPERATIONS_H

#include <stdio.h>
#include <stdlib.h>

int get_last_line(FILE fp, char **output);
int get_specific_line(FILE fp, char **output);
#define BUFFSIZE (sizeof(char) * 5000)

int get_last_line(char *path, char *output);
int get_specific_line(char *path, char *output, int finalLine);
#endif //COMS352_SHELL_FILE_OPERATIONS_H
+2 −1
Original line number Diff line number Diff line
@@ -5,10 +5,11 @@
#include <stdlib.h>

#include "tokenize.h"
#include "file_operations.h"

#define TRUE 0
#define READ 0
#define BUFFSIZE (sizeof(char) * 5000)


int main() {
    // Setup Some Vars