-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec12.c
295 lines (230 loc) · 7.84 KB
/
spec12.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "header.h"
//DONE SPEC12
/*
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>*/
// Define a structure to hold process information
/*
struct ProcessInfo {
int pid;
char state;
char cmd[256];
};
*/
// Custom comparison function for qsort to sort by command name
int compareByCmdName(const void *a, const void *b) { //for sorting
return strcmp(((struct ProcessInfo *)a)->cmd, ((struct ProcessInfo *)b)->cmd);
}
void activities() {
DIR *dir;
struct dirent *ent;
int fd_self, fd;
char *tty;
char cmd[256], tty_self[256], path[256], stat_path[256];
FILE *file;
struct ProcessInfo processInfos[256]; // Assuming a maximum of 256 processes
int numProcesses = 0;
dir = opendir("/proc"); ///proc directory to access information about running processes
fd_self = open("/proc/self/fd/0", O_RDONLY);
sprintf(tty_self, "%s", ttyname(fd_self));// terminal device of the current process
while ((ent = readdir(dir)) != NULL) {//iterate through entries in the /proc directory
if (isdigit(ent->d_name[0])) {
strcpy(path, "/proc/");
strcat(path, ent->d_name); //
strcat(path, "/fd/0");
fd = open(path, O_RDONLY);
tty = ttyname(fd);
if (tty && strcmp(tty, tty_self) == 0) { // checks if it matches the terminal device of the current process.
strcpy(stat_path, "/proc/");
strcat(stat_path, ent->d_name);//process ID (PID) in string form
strcat(stat_path, "/stat");
file = fopen(stat_path, "r");//proc/[pid]/stat
int pid;
char state;
fscanf(file, "%d %s %c", &pid, cmd, &state);
// Store process information in the array
processInfos[numProcesses].pid = pid;
processInfos[numProcesses].state = state;
strcpy(processInfos[numProcesses].cmd, cmd);
numProcesses++;
fclose(file); //file descriptors are closed.
}
close(fd);// descriptors are closed.
}
}
closedir(dir); //directory are closed.
close(fd_self);
// qSort the processInfos array lexicographically by command name
qsort(processInfos, numProcesses, sizeof(struct ProcessInfo), compareByCmdName);
// Print the sorted list
printf("PID\tSTATE\tCMD\n");
for (int i = 0; i < numProcesses; i++) {
const char *state_str ;
if(processInfos[i].state=='R'){state_str="Running";}
else if(processInfos[i].state=='T'){state_str="Stopped";}
if(processInfos[i].state=='S'){state_str="Running";} //Sleeping
//printf("%d:\t%s\t-%s\n", processInfos[i].pid, processInfos[i].cmd,state_str );
printf("%d:\t%s\t-%s\n", processInfos[i].pid, processInfos[i].cmd, state_str );
}
return ;
}
/*
struct ProcessInfo {
int pid;
char state;
char cmd[256];
};
// Custom comparison function for qsort to sort by command name
int compareByCmdName(const void *a, const void *b) {
return strcmp(((struct ProcessInfo *)a)->cmd, ((struct ProcessInfo *)b)->cmd);
}
void activites() {
DIR *dir;
struct dirent *ent;
int fd_self, fd;
char *tty;
char cmd[256], tty_self[256], path[256], stat_path[256];
FILE *file;
struct ProcessInfo processInfos[256]; // Assuming a maximum of 256 processes
int numProcesses = 0;
dir = opendir("/proc");
fd_self = open("/proc/self/fd/0", O_RDONLY);
sprintf(tty_self, "%s", ttyname(fd_self));
while ((ent = readdir(dir)) != NULL) {
if (isdigit(ent->d_name[0])) {
sprintf(path, "/proc/%s/fd/0", ent->d_name);
fd = open(path, O_RDONLY);
tty = ttyname(fd);
if (tty && strcmp(tty, tty_self) == 0) {
sprintf(stat_path, "/proc/%s/stat", ent->d_name);
file = fopen(stat_path, "r");
int pid;
char state;
fscanf(file, "%d %s %c", &pid, cmd, &state);
// Store process information in the array
processInfos[numProcesses].pid = pid;
processInfos[numProcesses].state = state;
strcpy(processInfos[numProcesses].cmd, cmd);
numProcesses++;
fclose(file);
}
close(fd);
}
}
closedir(dir);
close(fd_self);
// Sort the processInfos array lexicographically by command name
qsort(processInfos, numProcesses, sizeof(struct ProcessInfo), compareByCmdName);
// Print the sorted list
printf("PID\tSTATE\tCMD\n");
for (int i = 0; i < numProcesses; i++) {
const char *state_str = ( processInfos[i].state == 'R') ? "Running" : "Stopped";
printf("%d:\t%s\t-%s\n", processInfos[i].pid, processInfos[i].cmd,state_str );
}
return ;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
char* shell_name=NULL;
// Function to find the shell name
char* findShellName() {
//char* shell_name = NULL;
// Get the current process ID
pid_t my_pid = getpid();
// Execute the 'ps' command and capture its output
FILE* ps_output = popen("ps -p $PPID -o comm=", "r");
if (ps_output != NULL) {
char buffer[256];
if (fgets(buffer, sizeof(buffer), ps_output) != NULL) {
// Remove trailing newline character
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
}
shell_name = strdup(buffer); // Duplicate the string
}
pclose(ps_output);
}
//return shell_name;
}
////
void func() {
pid_t my_pid = getpid();
pid_t parent_pid = getppid();
pid_t group_pid = getpgrp();
pid_t session_pid = getsid(0); // 0 means the current process
printf("Process ID: %d\n", my_pid);
printf("Parent Process ID: %d\n", parent_pid);
printf("Process Group ID: %d\n", group_pid);
printf("Session ID: %d\n", session_pid);
// Get the directory path for the /proc directory
char proc_dir[256];
snprintf(proc_dir, sizeof(proc_dir), "/proc/%d/exe", my_pid);
// Execute the 'ps' command and capture its output
printf("List of process names spawned by your shell (sorted lexicographically):\n");
FILE *ps_output = popen("ps -eo comm", "r");
if (ps_output == NULL) {
perror("popen");
return ;
}
// Skip the header line
char buffer[256];
fgets(buffer, sizeof(buffer), ps_output);
// Create an array to store the process names
char process_names[1024][256];
int num_processes = 0;
// Read and store the process names
while (fgets(buffer, sizeof(buffer), ps_output) != NULL) {
char process_name[256];
sscanf(buffer, "%s", process_name);
// Check if the process name starts with the shell's name
if (strstr(process_name, shell_name) != NULL) {
strcpy(process_names[num_processes], process_name);
num_processes++;
}
}
// Close the 'ps' command output
pclose(ps_output);
// Sort and print the process names
for (int i = 0; i < num_processes; i++) {
for (int j = i + 1; j < num_processes; j++) {
if (strcmp(process_names[i], process_names[j]) > 0) {
char temp[256];
strcpy(temp, process_names[i]);
strcpy(process_names[i], process_names[j]);
strcpy(process_names[j], temp);
}
}
}
for (int i = 0; i < num_processes; i++) {
printf("%s\n", process_names[i]);
}
return ;
}
////
void activites(){ //int main()
findShellName();
if (shell_name != NULL) {
printf("Shell Name: %s\n", shell_name);
// Free allocated memory
free(shell_name);
} else {
printf("Failed to determine shell name.\n");
}
func();
}
*/