83 lines
1.5 KiB
C
83 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "list.h"
|
|
|
|
#define INIT_LIMIT 8
|
|
|
|
void init(list *l_ptr)
|
|
{
|
|
l_ptr->limit = INIT_LIMIT;
|
|
l_ptr->jobs = malloc(l_ptr->limit * sizeof(job));
|
|
if (l_ptr->jobs == NULL)
|
|
{
|
|
printf(stderr, "ERROR: malloc failed\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
job add(list *l_ptr, int pid, char **cmd, int state)
|
|
{
|
|
job new_job;
|
|
new_job.pid = pid;
|
|
new_job.cmd = cmd;
|
|
new_job.used = 1;
|
|
|
|
for (int i = 0; i < l_ptr->size; i++)
|
|
{
|
|
if (!l_ptr->jobs[i].used)
|
|
{
|
|
l_ptr->jobs[i] = new_job;
|
|
return new_job;
|
|
}
|
|
}
|
|
|
|
l_ptr->limit = l_ptr->limit * 2;
|
|
l_ptr->jobs = realloc(l_ptr->jobs, l_ptr->limit * sizeof(job));
|
|
if (l_ptr->jobs == NULL)
|
|
{
|
|
printf(stderr, "ERROR: realloc failed\n");
|
|
exit(1);
|
|
}
|
|
l_ptr->jobs[l_ptr->limit / 2] = new_job;
|
|
return new_job;
|
|
}
|
|
|
|
int is_empty(list l_ptr)
|
|
{
|
|
return l_ptr.size == 0;
|
|
}
|
|
|
|
job remove(list *l_ptr, int pid)
|
|
{
|
|
if (is_empty(*l_ptr))
|
|
{
|
|
printf(stderr, "ERROR: jobs is empty, cannot remove\n");
|
|
exit(1);
|
|
}
|
|
|
|
for (int i = 0; i < l_ptr->size; i++)
|
|
{
|
|
if (l_ptr->jobs[i].pid == pid)
|
|
{
|
|
job job = l_ptr->jobs[i];
|
|
job.used = 0;
|
|
return l_ptr->jobs[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
job top(list l_ptr)
|
|
{
|
|
return l_ptr.jobs[l_ptr.size - 1];
|
|
}
|
|
|
|
job get(list l_ptr, int index)
|
|
{
|
|
return l_ptr.jobs[index];
|
|
}
|
|
|
|
void free(list *l_ptr)
|
|
{
|
|
free(l_ptr->jobs);
|
|
l_ptr->jobs = NULL;
|
|
} |