projet-systeme-exploitation.../jobs.c
2021-04-21 15:12:19 +02:00

115 lines
1.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jobs.h"
void ajouter(list *l_ptr, int pid, int id, char **seq, int state)
{
cell *new_cell = malloc(sizeof(*new_cell));
char *cmd;
cmd = malloc(sizeof(char) * 256);
cmd[0] = '\0';
while (*seq)
{
strcat(cmd, *seq);
seq++;
strcat(cmd, " ");
}
new_cell->pid = pid;
new_cell->id = id;
new_cell->cmd = cmd;
new_cell->state = state;
if (*l_ptr == NULL)
{
new_cell->next = NULL;
}
else
{
new_cell->next = *l_ptr;
}
*l_ptr = new_cell;
}
void supprimer(list *l_ptr, int pid)
{
cell *cursor = *l_ptr;
if (cursor->pid == pid)
{
cell *cursor2free = cursor;
*l_ptr = cursor->next;
free(cursor2free);
}
else
{
while (cursor->next != NULL)
{
if (cursor->next->pid == pid)
{
break;
}
else
{
cursor = cursor->next;
}
}
cell *cursor_next = cursor->next->next;
free(cursor->next);
cursor->next = cursor_next;
}
}
void afficher(list *l_ptr)
{
cell *cursor = *l_ptr;
while (cursor != NULL)
{
printf("[%d] %d\n", cursor->id, cursor->pid);
cursor = cursor->next;
}
}
void initialiser(list *l_ptr)
{
*l_ptr = NULL;
}
void liberer(list *l_ptr)
{
cell* cursor2free;
cell* cursor = *l_ptr;
while (cursor != NULL)
{
cursor2free = cursor;
cursor = cursor->next;
free(cursor2free);
}
free(*l_ptr);
*l_ptr = NULL;
}
cell *trouver(list *l_ptr, int pid)
{
cell *cursor = *l_ptr;
while (cursor != NULL)
{
if (cursor->pid == pid)
{
return cursor;
}
cursor = cursor->next;
}
return NULL; // erreur
}