LCOV - code coverage report
Current view: top level - src - alias.c (source / functions) Coverage Total Hit
Test: report.info Lines: 100.0 % 42 42
Test Date: 2025-02-11 09:58:02 Functions: 100.0 % 7 7

            Line data    Source code
       1              : #include "alias.h"
       2              : 
       3              : alias_t aliases[MAX_ALIAS];
       4              : int alias_count = 0;
       5              : 
       6          143 : int set_alias(const char *name, const char *command)
       7              : {
       8          143 :     if (alias_count >= MAX_ALIAS)
       9            1 :         return -1;
      10              : 
      11          142 :     if(get_alias_index(name) != -1)
      12            1 :         unset_alias(name);
      13              : 
      14          142 :     aliases[alias_count].name = strdup(name);
      15          142 :     aliases[alias_count].command = strdup(command);
      16              : 
      17          142 :     alias_count++;
      18              : 
      19          142 :     return 0;
      20              : }
      21              : 
      22            3 : int unset_alias(const char *name)
      23              : {
      24            3 :     int index = get_alias_index(name);
      25            3 :     if (index == -1)
      26            1 :         return -1;
      27              : 
      28            2 :     free_if_needed(aliases[index].name);
      29            2 :     free_if_needed(aliases[index].command);
      30              : 
      31            3 :     for (int i = index; i < alias_count - 1; i++)
      32            1 :         aliases[i] = aliases[i + 1];
      33              : 
      34            2 :     alias_count--;
      35              : 
      36            2 :     return 0;
      37              : }
      38              : 
      39            6 : char *get_alias_command(const char *name)
      40              : {
      41           11 :     for (int i = 0; i < alias_count; i++)
      42              :     {
      43           10 :         if (strcmp(aliases[i].name, name) == 0)
      44            5 :             return strdup(aliases[i].command);
      45              :     }
      46              : 
      47            1 :     return NULL;
      48              : }
      49              : 
      50            6 : int get_alias_count()
      51              : {
      52            6 :     return alias_count;
      53              : }
      54              : 
      55          362 : int get_alias_index(const char *name)
      56              : {
      57         8502 :     for (int i = 0; i < alias_count; i++)
      58              :     {
      59         8145 :         if (strcmp(aliases[i].name, name) == 0)
      60            5 :             return i;
      61              :     }
      62              : 
      63          357 :     return -1;
      64              : }
      65              : 
      66            4 : int get_alias_name_by_index(int index, char *name)
      67              : {
      68            4 :     if (index < 0 || index >= alias_count)
      69            1 :         return -1;
      70              : 
      71            3 :     strcpy(name, aliases[index].name);
      72              : 
      73            3 :     return 0;
      74              : }
      75              : 
      76            3 : void free_aliases()
      77              : {
      78          135 :     for (int i = 0; i < alias_count; i++)
      79              :     {
      80          132 :         free_if_needed(aliases[i].name);
      81          132 :         free_if_needed(aliases[i].command);
      82              :     }
      83              : 
      84            3 :     alias_count = 0;
      85            3 : }
        

Generated by: LCOV version 2.0-1