tuamigo

Untitled

Oct 28th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6. #include <sys/stat.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9.  
  10. #define MAXENTRADA 2048
  11. #define FIN 1
  12. #define PID 2
  13. #define PWD 3
  14. #define CHDIR 4
  15. #define LIST 5
  16. #define DELETE 6
  17. #define DELTREE 7
  18.  
  19. /***********************************************************/
  20. void Cmd_pwd()
  21. {
  22.   char dir[MAXENTRADA];
  23.   if (getcwd(dir,MAXENTRADA)==NULL)
  24.     perror("imposible obtener directorio actual");
  25.   else
  26.     printf ("%s\n",dir);
  27. }
  28. /***********************************************************/
  29. void Cmd_chdir(char * dir)
  30. {
  31.   if (dir==NULL)
  32.     Cmd_pwd();
  33.   else if (chdir(dir)==-1)
  34.     perror ("Imposible cambiar de directorio");
  35. }
  36. /***********************************************************/
  37. void Cmd_pid(void)
  38. {
  39.   printf ("Pid de shell: %d, pid padre del shell: %d\n",getpid(),getppid());
  40. }
  41. /***********************************************************/
  42. void Cmd_list(char *entrada[], int numArg){
  43.      /* Variables */
  44.     char *dir;
  45.     char *tr;
  46.     DIR *dirp;
  47.     int buf;
  48.     char * directorio;
  49.     char ent[MAXENTRADA];
  50.     struct dirent *direntp;
  51.     struct stat estructura;
  52.     int l=0, r=0, h=0, i;
  53.     char ruta[MAXENTRADA] = ".";
  54.  
  55.     // procesar entrada del list
  56.     for (i=0; i<numArg; i++){
  57.         if (!strcmp(entrada[i], "-h")) h=1;
  58.         else if (strcmp(entrada[i], "-r")) r=1;
  59.         else if (strcmp(entrada[i], "-l")) l=1;
  60.         else strcpy(ruta, entrada[i]);
  61.     }
  62.         /* Abrimos el directorio */
  63.        
  64.          directorio=getcwd(ent,MAXENTRADA);    
  65.          dirp = opendir(directorio);
  66.        
  67.          if (dirp == NULL){
  68.          printf("Error: No se puede abrir el directorio\n");
  69.          exit(2);
  70.          } 
  71.          /* Leemos las entradas del directorio */
  72.                
  73.          while ( (direntp=readdir(dirp)) != NULL){
  74.                 stat(directorio,&estructura);
  75.             printf("\t%d \t%d \t%c \t%d \t%x \t%x \t%s\n",estructura.st_mode,estructura.st_nlink,estructura.st_uid,estructura.st_size,estructura.st_atime,estructura.st_mtime,direntp->d_name);
  76.            
  77.             printf( (S_ISDIR(estructura.st_mode)) ? "d" : "-");
  78.             printf( (estructura.st_mode & S_IRUSR) ? "r" : "-");
  79.             printf( (estructura.st_mode & S_IWUSR) ? "w" : "-");
  80.             printf( (estructura.st_mode & S_IXUSR) ? "x" : "-");
  81.             printf( (estructura.st_mode & S_IRGRP) ? "r" : "-");
  82.             printf( (estructura.st_mode & S_IWGRP) ? "w" : "-");
  83.             printf( (estructura.st_mode & S_IXGRP) ? "x" : "-");
  84.             printf( (estructura.st_mode & S_IROTH) ? "r" : "-");
  85.             printf( (estructura.st_mode & S_IWOTH) ? "w" : "-");
  86.             printf( (estructura.st_mode & S_IXOTH) ? "x" : "-");
  87.    
  88.         }
  89.          /* Cerramos el directorio */
  90.  
  91.          closedir(dirp);
  92.         printf("\n");
  93.  
  94.  
  95.    
  96. }
  97. /***********************************************************/
  98. void Cmd_delete(char *entrada){
  99.     FILE *fichero;
  100.  
  101.    fichero= fopen( entrada, "r" );  
  102.    if( fichero != NULL ){
  103.       fclose( fichero );
  104.       if( remove(entrada) == 0 )
  105.           printf( "     FICHERO BORRADO\n" );
  106.       else  
  107.         printf( "No pudo ser borrado\n" );
  108.    }
  109.    else   printf( "Fichero no encontrado\n" );
  110.  
  111. }
  112. /***********************************************************/
  113. void Cmd_deltree(char *entrada){
  114.  
  115.    
  116.     DIR *folder;
  117.     struct dirent *entry;
  118.     char  *nombre;
  119.  
  120.         Cmd_chdir(entrada);
  121.         folder = opendir(entrada);
  122.         while (entry = readdir(folder)){
  123.             if (strcmp(entry->d_name, ".") != 0 &&
  124.                strcmp(entry->d_name, "..") != 0)
  125.             {
  126.                 nombre = entry->d_name;
  127.  
  128.                 if (remove(nombre)==-1)
  129.                     Cmd_deltree(nombre);
  130.                 else
  131.                     Cmd_delete(nombre);
  132.                
  133.             }
  134.         }
  135.         closedir(folder);
  136.         chdir("..");
  137.         rmdir(entrada);
  138.  
  139. }
  140. /***********************************************************/
  141. int TrocearCadena(char * cadena, char * trozos[])
  142. {
  143.  int i=1;
  144.  if ((trozos[0]=strtok(cadena," \n\t"))==NULL)
  145.     return 0;
  146.  while ((trozos[i]=strtok(NULL," \n\t"))!=NULL)
  147.     i++;
  148.  return i;
  149. }
  150. /***********************************************************/
  151. int  CodigoEntrada (char *tr){
  152.     if (!strcmp(tr,"fin"))
  153.         return FIN;
  154.     if (!strcmp(tr,"quit"))
  155.         return FIN;
  156.     if (!strcmp(tr,"exit"))
  157.         return FIN;
  158.     if (!strcmp(tr,"pid"))
  159.         return PID;
  160.     if (!strcmp(tr,"pwd"))
  161.         return PWD;
  162.     if (!strcmp(tr,"chdir"))
  163.         return CHDIR;
  164.     if (!strcmp(tr,"list"))
  165.         return LIST;
  166.     if (!strcmp(tr,"delete"))
  167.         return DELETE;
  168.     if (!strcmp(tr,"deltree"))
  169.         return DELTREE;
  170. }
  171. /***********************************************************/
  172. void ProcesarEntrada (char *tr[], int numArg){
  173.     switch (CodigoEntrada(tr[0])){
  174.         case FIN: exit (0); break;
  175.         case PID:  Cmd_pid(); break;
  176.         case PWD:  Cmd_pwd();break;
  177.         case CHDIR: if (numArg > 1) Cmd_chdir(tr[1]); break;
  178.         case LIST: Cmd_list(tr, numArg); break;
  179.         case DELETE: if (numArg > 1) Cmd_delete(tr[1]); else printf("Faltan argumentos\n"); break;
  180.         case DELTREE: if (numArg > 1) Cmd_deltree(tr[1]); else printf("Faltan argumentos\n"); break;
  181.    
  182.     }
  183. }
  184. /***********************************************************/
  185. int main (int argc, char *argv[])
  186. {
  187.   char entrada[MAXENTRADA];
  188.   char *tr[MAXENTRADA/2];
  189.   int numArg;
  190.  
  191. while (1) {
  192.    printf ("terminal:~$ ");
  193.    fgets (entrada,MAXENTRADA,stdin);
  194.    numArg =TrocearCadena(entrada,tr);
  195.    if (numArg < 1)
  196.     continue;
  197.    ProcesarEntrada(tr, numArg);
  198.   }
  199. }
  200. /***********************************************************/
Advertisement
Add Comment
Please, Sign In to add comment