projet-systeme-exploitation.../jobs.c

147 lines
2.6 KiB
C
Raw Normal View History

2021-04-20 16:12:44 +00:00
#include <stdio.h>
#include <stdlib.h>
2021-04-20 16:50:27 +00:00
#include <string.h>
2021-04-20 16:12:44 +00:00
#include "jobs.h"
2021-04-24 15:32:54 +00:00
int ajouter(list *l_ptr, int pid, char **seq)
2021-04-21 11:50:58 +00:00
{
2021-04-21 13:12:19 +00:00
cell *new_cell = malloc(sizeof(*new_cell));
2021-04-21 11:50:58 +00:00
2021-04-21 13:12:19 +00:00
char *cmd;
cmd = malloc(sizeof(char) * 256);
cmd[0] = '\0';
2021-04-21 12:01:28 +00:00
2021-04-21 13:12:19 +00:00
while (*seq)
{
strcat(cmd, *seq);
seq++;
strcat(cmd, " ");
}
2021-04-20 16:12:44 +00:00
2021-04-24 15:32:54 +00:00
new_cell->id = 1;
2021-04-21 13:12:19 +00:00
new_cell->pid = pid;
new_cell->cmd = cmd;
2021-04-24 15:32:54 +00:00
new_cell->next = NULL;
2021-04-20 16:12:44 +00:00
2021-04-24 15:32:54 +00:00
cell *cursor = *l_ptr;
if (cursor == NULL)
{
*l_ptr = new_cell;
}
else if (cursor->id > 1)
2021-04-21 13:12:19 +00:00
{
2021-04-24 15:32:54 +00:00
new_cell->next = cursor;
*l_ptr = new_cell;
2021-04-21 13:12:19 +00:00
}
else
{
2021-04-24 15:32:54 +00:00
while (cursor->next != NULL)
{
new_cell->id++;
if (new_cell->id < cursor->next->id)
{
new_cell->next = cursor->next;
cursor->next = new_cell;
break;
}
cursor = cursor->next;
}
if (cursor->next == NULL)
{
new_cell->id++;
cursor->next = new_cell;
}
2021-04-21 13:12:19 +00:00
}
2021-04-24 15:32:54 +00:00
return new_cell->id;
2021-04-20 16:12:44 +00:00
}
2021-04-21 11:50:58 +00:00
void supprimer(list *l_ptr, int pid)
{
2021-04-21 13:12:19 +00:00
cell *cursor = *l_ptr;
if (cursor->pid == pid)
2021-04-21 11:50:58 +00:00
{
2021-04-21 13:12:19 +00:00
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;
2021-04-20 16:12:44 +00:00
}
}
2021-04-21 11:50:58 +00:00
void afficher(list *l_ptr)
{
2021-04-21 13:12:19 +00:00
cell *cursor = *l_ptr;
while (cursor != NULL)
{
2021-04-24 15:32:54 +00:00
printf("[%d] %d : %s\n", cursor->id, cursor->pid, cursor->cmd);
2021-04-21 13:12:19 +00:00
cursor = cursor->next;
}
2021-04-20 16:12:44 +00:00
}
2021-04-21 11:50:58 +00:00
void initialiser(list *l_ptr)
{
2021-04-21 13:12:19 +00:00
*l_ptr = NULL;
2021-04-20 16:12:44 +00:00
}
2021-04-21 11:50:58 +00:00
void liberer(list *l_ptr)
{
2021-04-24 15:32:54 +00:00
cell *cursor2free;
cell *cursor = *l_ptr;
2021-04-21 13:12:19 +00:00
while (cursor != NULL)
2021-04-21 11:50:58 +00:00
{
2021-04-21 13:12:19 +00:00
cursor2free = cursor;
cursor = cursor->next;
free(cursor2free);
2021-04-20 16:12:44 +00:00
}
2021-04-21 13:12:19 +00:00
free(*l_ptr);
*l_ptr = NULL;
2021-04-21 12:15:14 +00:00
}
cell *trouver(list *l_ptr, int pid)
{
2021-04-21 13:12:19 +00:00
cell *cursor = *l_ptr;
2021-04-21 12:15:14 +00:00
2021-04-21 13:12:19 +00:00
while (cursor != NULL)
2021-04-21 12:15:14 +00:00
{
2021-04-21 13:12:19 +00:00
if (cursor->pid == pid)
{
return cursor;
}
cursor = cursor->next;
2021-04-21 12:15:14 +00:00
}
2021-04-24 15:32:54 +00:00
return NULL; // erreur
}
cell *trouver_id(list *l_ptr, int id)
{
cell *cursor = *l_ptr;
while (cursor != NULL)
{
if (cursor->id == id)
{
return cursor;
}
cursor = cursor->next;
}
2021-04-21 13:12:19 +00:00
return NULL; // erreur
2021-04-20 16:12:44 +00:00
}