tuamigo

Untitled

Oct 10th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.03 KB | None | 0 0
  1. --- main.c
  2. +++ main.c
  3. @@ -3,20 +3,22 @@
  4.  #include <string.h>
  5.  #include "commands.h"
  6.  
  7. -void getArgList(char (*argList)[255]);
  8. -char rutaActual[255];
  9. +int getArgList(char (*argList)[255]);
  10.  
  11.  int main(int argc, char* argv[])
  12.  {
  13.     char argList[20][255];
  14. +   int argCount;
  15.  
  16. -   strcpy(rutaActual, "hola");
  17. +   setcwd(); // asiganamos ruta actual
  18. +   uname = getenv("USER"); // FIXME: esto devuelve un puntero a char que fue creado localmente, no desaparecerá?
  19. +   host = getenv("HOSTNAME");
  20.  
  21.     while (1)
  22.     {
  23. -       getArgList(argList);
  24. +       argCount = getArgList(argList);
  25.  
  26. -       // TEST BEGIN: imprimimos los argumentos
  27. +       /*// TEST BEGIN: imprimimos los argumentos
  28.         int i = 0;
  29.         while (argList[i][0] != '\0')
  30.         {
  31. @@ -24,35 +26,51 @@
  32.             i++;
  33.         }
  34.         printf("\n");
  35. -       // TEST END
  36. +       // TEST END*/
  37.  
  38.         // Ejecutamos acciones
  39. -       if (!strcmp(argList[0], ""))
  40. +       if (argCount == 0)
  41.             continue;
  42.         if (!strcmp(argList[0], "quit") || !strcmp(argList[0], "exit") || !strcmp(argList[0], "fin"))
  43.             break;
  44.         if (!strcmp(argList[0], "pid"))
  45.         {
  46.             pid(argList);
  47. -           break;
  48. +           continue;
  49. +       }
  50. +       if (!strcmp(argList[0], "pwd"))
  51. +       {
  52. +           pwd(argList);
  53. +           continue;
  54. +       }
  55. +       if (!strcmp(argList[0], "chdir"))
  56. +       {
  57. +           changedir(argList, argCount);
  58. +           continue;
  59. +       }
  60. +       if (!strcmp(argList[0], "list"))
  61. +       {
  62. +           list(argList, argCount);
  63. +           continue;
  64.         }
  65.  
  66. -
  67. -       // TODO: Print error, command not found
  68. +       printf("shell: %s: comando no encontrado...\n", argList[0]);
  69.     }
  70.     return 0;
  71.  }
  72.  
  73. -void getArgList(char (*argList)[255])
  74. +int getArgList(char (*argList)[255])
  75.  {
  76.      char buffer[256];
  77. -    printf("[%s]$ ", rutaActual); // print prompt
  78. +    printf("[%s@%s %s]$ ", uname, host, rutaActual); // print prompt
  79.      fgets(buffer, 255, stdin);
  80.  
  81.      char* arg;
  82.      int i = 0;
  83.      arg = strtok(buffer, "\n \t");
  84.      if (arg) strcpy(argList[i++], arg);
  85. -    while ((arg = strtok(NULL, "\n \t")))
  86. +    while ((arg = strtok(NULL, "\n \t")) && i <= 20)
  87.          strcpy(argList[i++], arg);
  88. +
  89. +    return i;
  90.  }
Advertisement
Add Comment
Please, Sign In to add comment