-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_files.cpp
More file actions
220 lines (167 loc) · 4 KB
/
list_files.cpp
File metadata and controls
220 lines (167 loc) · 4 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdbool.h>
#include <dirent.h>
#include <ncurses.h>
//functions
static const int asizeh = 256; /* max number of characters in a file - same as in struct dirent - do not change */
static int getdirfiles(int vplace, char directory[], char database[][asizeh]);
static int getdirnum(char directory[]);
static int sortalpha(const void *p, const void *q);
static void program(char *array, bool parent, bool child);
static void printmatches(const int num_progs, char database[][asizeh]);
static void startwin();
//string vars
char uprompt[] = "Search for program: ";
char *string = NULL;
char *selprog;
size_t mem = 0, charsize = 0;
int ch = EOF;
//path direcotires
char pwd1[] = "/usr/bin";
char pwd2[] = "/usr/local/bin";
//structs
typedef struct {
int term_rows;
int term_cols;
} WinProps;
WinProps *wp;
typedef struct timespec timespec;
int main() {
const int avsize1 = getdirnum(pwd1);
const int avsize2 = getdirnum(pwd2);
const int avtotal = avsize1 + avsize2;
char list[avtotal][asizeh];
getdirfiles(0, pwd1, list);
getdirfiles(avsize1, pwd2, list);
qsort(list, avtotal, asizeh, sortalpha);
startwin();
wp = (WinProps *) malloc(sizeof(int));
getmaxyx(stdscr, wp->term_rows, wp->term_cols);
printw(uprompt);
move(1,0);
int i;
int linetot = wp->term_rows + 4;
for (i = 5; i < linetot; i++) {
char temp[asizeh];
strcpy(temp, list[i]);
printw("%s\n", temp);
}
move(0, strlen(uprompt));
while ((ch = getch()) != ('\n')) {
switch (ch) {
case 127: {
if (charsize > 0) {
mvdelch(0, strlen(uprompt) + charsize - 1);
string[--charsize] = 0;
move(1,0);
clrtobot();
printmatches(avtotal, list);
}
break;
}
default: {
printw("%c", ch);
move(1,0);
clrtobot();
if (mem <= charsize) {
mem += sizeof(char);
string = (char *) realloc(string, mem);
}
string[charsize++] = ch;
printmatches(avtotal, list);
}
}
}
endwin();
program(selprog, true, true);
return 0;
}
void startwin() {
initscr();
noecho();
cbreak();
}
void printmatches(const int num_progs, char finaldata[][asizeh]) {
int i;
int selines = 1;
for (i = 5; i < num_progs; i++){
char test[asizeh];
if ((strncmp(finaldata[i], string, strlen(string)) == 0) && (selines < wp->term_rows)) {
if(selines == 1) {
selprog = NULL;
selprog = (char *) malloc(256 * sizeof(char));
strcpy(selprog, finaldata[i]);
}
strcpy(test, finaldata[i]);
selines++;
printw("%s\n", test);
}
else continue;
}
move(0, charsize + strlen(uprompt));
}
int sortalpha(const void *p, const void *q) {
const char *cp = (const char *) p;
const char *cq = (const char *) q;
return strcasecmp(cp, cq);
}
int getdirfiles(int vplace, char directory[], char database[][asizeh])
{
DIR *current = opendir(directory);
struct dirent *dirstc;
if (current == NULL) {
printf("error");
return 1;
}
while ((dirstc = readdir(current)) != NULL) {
strcpy(database[vplace], dirstc->d_name);
vplace++;
}
closedir(current);
return 0;
}
int getdirnum(char directory[]) {
int i;
struct dirent *dirstc;
DIR *current = opendir(directory);
if (current == NULL) printf("error");
for(i = 1; (dirstc = readdir(current)) != NULL; i++);
closedir(current);
return i;
}
void program(char *array, bool parent, bool child) {
if (parent == true) {
int parentpid = getpid();
printf("parent process number is: %i\n", parentpid);
}
if (fork() == 0) {
if (child == false) {
int out = open("/dev/null", O_WRONLY);
dup2(out, 1);
dup2(out, 2);
close(out);
}
else {
int childpid = getpid();
printf("child process number is: %i\n", childpid);
}
char *stcmd = (char *) "st";
char *cmd[] = { stcmd, array, (char *)0 };
execvp(cmd[0], cmd);
exit(0);
}
else {
timespec t;
t.tv_sec = 0;
t.tv_nsec = 500000;
nanosleep(&t, &t);
printf("Successfully launched\n");
}
}