1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
Copyright (c) 2024, Lance Borden
All rights reserved.
This software is licensed under the BSD 3-Clause License.
You may obtain a copy of the license at:
https://opensource.org/licenses/BSD-3-Clause
Redistribution and use in source and binary forms, with or without
modification, are permitted under the conditions stated in the BSD 3-Clause
License.
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "lush.h"
#include <linux/limits.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_LINES 512
static char *get_history_path() {
uid_t uid = getuid();
struct passwd *pw = getpwuid(uid);
if (!pw) {
perror("retrieve home dir");
return NULL;
}
size_t path_length = strlen(pw->pw_dir) + strlen("/.lush_history") + 1;
char *path = malloc(path_length);
if (!path) {
perror("malloc");
return NULL;
}
strcpy(path, pw->pw_dir);
strcat(path, "/.lush_history");
return path;
}
char *lush_get_past_command(int pos) {
char *path = get_history_path();
if (access(path, F_OK) != 0) {
// file does not exist
perror("history file check");
free(path);
return NULL;
}
// read file from the bottom
FILE *fp = fopen(path, "r");
if (!fp) {
perror("opening file");
free(path);
return NULL;
}
char *line = (char *)malloc(1024 * sizeof(char));
if (line == NULL) {
perror("malloc");
free(path);
return NULL;
}
int curr_line = 0;
while (fgets(line, 1024, fp)) {
if (curr_line == pos) {
fclose(fp);
return line;
}
curr_line++;
}
fclose(fp);
free(path);
return line;
}
void lush_push_history(const char *line) {
char *path = get_history_path();
// check if the file exists and create if not
if (access(path, F_OK) != 0) {
FILE *fp = fopen(path, "w");
if (fp == NULL) {
perror("creating file");
} else {
fclose(fp);
}
}
FILE *fp = fopen(path, "r");
if (fp == NULL) {
perror("opening file");
free(path);
return;
}
// Count lines
int line_count = 0;
char buffer[1024]; // Adjust buffer size as needed
while (fgets(buffer, sizeof(buffer), fp)) {
line_count++;
}
// Allocate memory for file content
fseek(fp, 0, SEEK_SET);
char **lines = (char **)malloc(line_count * sizeof(char *));
if (lines == NULL) {
perror("malloc");
fclose(fp);
free(path);
return;
}
// Read lines into memory
int index = 0;
while (fgets(buffer, sizeof(buffer), fp)) {
lines[index] = strdup(buffer); // Allocate and copy each line
if (lines[index] == NULL) {
perror("strdup");
while (index > 0)
free(lines[--index]);
free(lines);
free(path);
fclose(fp);
return;
}
index++;
}
fclose(fp);
// Open file for writing
fp = fopen(path, "w");
if (fp == NULL) {
perror("opening file");
for (int i = 0; i < index; i++)
free(lines[i]);
free(lines);
free(path);
return;
}
// Write the new line
fprintf(fp, "%s\n", line);
// Write the last MAX_LINES lines
int total_lines = line_count < MAX_LINES ? line_count : MAX_LINES;
for (int i = 0; i < total_lines; i++) {
fprintf(fp, "%s", lines[i]);
free(lines[i]); // Free each line after writing
}
// Clean up
free(lines);
free(path);
fclose(fp);
}
|