LCOV - code coverage report
Current view: top level - src - builtin_commands.c (source / functions) Hit Total Coverage
Test: report.info Lines: 36 44 81.8 %
Date: 2024-01-02 12:43:31 Functions: 6 6 100.0 %

          Line data    Source code
       1             : #include "builtin_commands.h"
       2             : 
       3          68 : int is_builtin_command(const Command *command, const char *expected)
       4             : {
       5          68 :     return strcmp(command->command, expected) == 0;
       6             : }
       7             : 
       8           2 : void cd(const char *path)
       9             : {
      10           2 :     if (chdir(path) != 0)
      11             :     {
      12           0 :         perror("cd");
      13             :     }
      14           2 : }
      15             : 
      16           2 : void pwd()
      17             : {
      18             :     char cwd[MAX_INPUT_LENGTH];
      19           2 :     if (getcwd(cwd, sizeof(cwd)) == NULL)
      20             :     {
      21           0 :         perror("pwd");
      22             :     }
      23             :     else
      24             :     {
      25           2 :         printf("%s\n", cwd);
      26             :     }
      27           2 : }
      28             : 
      29           2 : int exit_shell(CommandSequence *sequence, int exit_code)
      30             : {
      31           2 :     if (sequence != NULL)
      32             :     {
      33           2 :         free_command_sequence(sequence);
      34             :     }
      35           2 :     free_aliases();
      36           2 :     exit(exit_code);
      37             :     return exit_code;
      38             : }
      39             : 
      40           6 : void echo(const Command *command)
      41             : {
      42          19 :     for (int i = 0; i < command->num_arguments; i++)
      43             :     {
      44          13 :         printf("%s ", command->arguments[i]);
      45             :     }
      46           6 :     printf("\n");
      47           6 : }
      48             : 
      49          20 : int execute_builtin_command(const Command *command, CommandSequence *sequence)
      50             : {
      51          20 :     if (is_builtin_command(command, "cd"))
      52             :     {
      53           2 :         if (command->num_arguments == 1)
      54             :         {
      55           2 :             cd(command->arguments[0]);
      56           2 :             return IS_BUILTIN_COMMAND;
      57             :         }
      58           0 :         perror("Usage: cd <directory>");
      59             :     }
      60          18 :     else if (is_builtin_command(command, "pwd"))
      61             :     {
      62           2 :         if (command->num_arguments == 0)
      63             :         {
      64           2 :             pwd();
      65           2 :             return IS_BUILTIN_COMMAND;
      66             :         }
      67           0 :         perror("Usage: pwd");
      68             :     }
      69          16 :     else if (is_builtin_command(command, "exit"))
      70             :     {
      71           2 :         if (command->num_arguments == 0)
      72             :         {
      73           2 :             exit_shell(sequence, EXIT_SUCCESS);
      74           0 :             return IS_BUILTIN_COMMAND;
      75             :         }
      76           0 :         perror("Usage: exit");
      77             :     }
      78          14 :     else if (is_builtin_command(command, "echo"))
      79             :     {
      80           6 :         if (command->num_arguments >= 1)
      81             :         {
      82           6 :             echo(command);
      83           6 :             return IS_BUILTIN_COMMAND;
      84             :         }
      85           0 :         perror("Usage: echo <message>");
      86             :     }
      87             :     else
      88             :     {
      89           8 :         return IS_NOT_BUILTIN_COMMAND;
      90             :     }
      91             : 
      92           0 :     return EXIT_FAILURE;
      93             : }

Generated by: LCOV version 1.14