-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.c
73 lines (69 loc) · 1.87 KB
/
jobs.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
#include "headers.h"
char **split_line_null(char *line)
{
int buffersize = 4096, position = 0;
char **tokens = malloc(buffersize * sizeof(char *)), *token;
if (tokens == NULL)
{
fprintf(stderr, "Memory Error: Buffer can't be allocated\n");
exit(EXIT_FAILURE);
}
token = strtok(line, "\0");
while (token != NULL)
{
tokens[position++] = token;
token = strtok(NULL, "\0");
}
tokens[position] = NULL;
return tokens;
}
int jobs(char **args)
{
extern int num_bg;
for (int i = 0; i < num_bg; ++i)
{
int id = background_process_pid_order[i], ret;
printf("[%d] ", i + 1);
char a[1024], b[1024], c[1024], filename[1024], procname[2048];
sprintf(filename, "/proc/%d/stat", id);
FILE *f = fopen(filename, "r");
if (f)
{
fscanf(f, "%s %s %s", a, b, c);
fclose(f);
}
else
{
perror("Error opening the proc file. Try again.");
return 1;
}
if (!strcmp(c, "T"))
printf("Stopped ");
else
printf("Running ");
sprintf(filename, "/proc/%d/cmdline", id);
f = fopen(filename, "r");
if (f)
{
fscanf(f, "%s", procname);
printf("%s ", procname);
// while (fscanf(f, "%s", procname) != EOF)
// {
// char **temp = split_line_null(procname);
// int t = 0;
// while (temp[t] != NULL)
// printf("%s ", temp[t++]);
// }
// printf("%s ", procname);
// char **temp = split_line_null(procname);
fclose(f);
}
else
{
perror("Error opening the proc file. Try again.");
return 1;
}
printf("[%d]\n", id);
}
return 1;
}