C-Shell
A simple shell written in C.
Loading...
Searching...
No Matches
history.c File Reference
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "history.h"

Functions

void add_to_history (char *line)
 Adds a line to the history.
 
void clear_history ()
 Clears the history.
 
const struct history_listget_history ()
 Gets the history list.
 
void print_history (int n)
 Prints the last n commands in the history.
 
int read_history_from_file ()
 
int write_history_to_file ()
 
int history (int nargs, char **args)
 Print the history of commands.
 

Variables

const char *const history_file = ".chistory"
 
struct history_list h_list
 
int opterr
 
int optind
 
int optopt
 
char * optarg
 

Function Documentation

◆ add_to_history()

void add_to_history ( char * line)

Adds a line to the history.

Parameters
lineThe command to add.

allocate memory and deep copy

free the memory

allocate memory and deep copy

◆ clear_history()

void clear_history ( )

Clears the history.

free the memory

◆ get_history()

const struct history_list * get_history ( )

Gets the history list.

Returns
Pointer to the history list.

◆ history()

int history ( int nargs,
char ** args )

Print the history of commands.

If no option is specified, prints the last 5 commands.

Options:

  • -c - clear the history
  • -r - read the history file and write to the history list
  • -w - write the history list to the history file

◆ print_history()

void print_history ( int n)

Prints the last n commands in the history.

Parameters
nThe number of commands to print.

◆ read_history_from_file()

int read_history_from_file ( )

◆ write_history_to_file()

int write_history_to_file ( )

Variable Documentation

◆ h_list

struct history_list h_list

◆ history_file

const char* const history_file = ".chistory"

◆ optarg

char* optarg
extern

◆ opterr

int opterr
extern

◆ optind

int optind

◆ optopt

int optopt