tuamigo

Untitled

Oct 28th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 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){
  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.  
  53. // procesar entrada del list
  54.  
  55. if (!strcmp(entrada,"-l")| !strcmp(entrada,"-r") | !strcmp(entrada,"-h")){
  56. /* Abrimos el directorio */
  57.  
  58. directorio=getcwd(ent,MAXENTRADA);
  59. dirp = opendir(directorio);
  60.  
  61. if (dirp == NULL){
  62. printf("Error: No se puede abrir el directorio\n");
  63. exit(2);
  64. }
  65. /* Leemos las entradas del directorio */
  66.  
  67. while ( (direntp=readdir(dirp)) != NULL){
  68. stat(directorio,&estructura);
  69. 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);
  70.  
  71. printf( (S_ISDIR(estructura.st_mode)) ? "d" : "-");
  72. printf( (estructura.st_mode & S_IRUSR) ? "r" : "-");
  73. printf( (estructura.st_mode & S_IWUSR) ? "w" : "-");
  74. printf( (estructura.st_mode & S_IXUSR) ? "x" : "-");
  75. printf( (estructura.st_mode & S_IRGRP) ? "r" : "-");
  76. printf( (estructura.st_mode & S_IWGRP) ? "w" : "-");
  77. printf( (estructura.st_mode & S_IXGRP) ? "x" : "-");
  78. printf( (estructura.st_mode & S_IROTH) ? "r" : "-");
  79. printf( (estructura.st_mode & S_IWOTH) ? "w" : "-");
  80. printf( (estructura.st_mode & S_IXOTH) ? "x" : "-");
  81.  
  82. }
  83. /* Cerramos el directorio */
  84.  
  85. closedir(dirp);
  86. printf("\n");
  87.  
  88. }
  89.  
  90.  
  91. }
  92. /***********************************************************/
  93. void Cmd_delete(char *entrada){
  94. FILE *fichero;
  95.  
  96. fichero= fopen( entrada, "r" );
  97. if( fichero != NULL ){
  98. fclose( fichero );
  99. if( remove(entrada) == 0 )
  100. printf( " FICHERO BORRADO\n" );
  101. else
  102. printf( "No pudo ser borrado\n" );
  103. }
  104. else printf( "Fichero no encontrado\n" );
  105.  
  106. }
  107. /***********************************************************/
  108. void Cmd_deltree(char *entrada){
  109.  
  110.  
  111. DIR *folder;
  112. struct dirent *entry;
  113. char *nombre;
  114.  
  115. Cmd_chdir(entrada);
  116. folder = opendir(entrada);
  117. while (entry = readdir(folder)){
  118. if (strcmp(entry->d_name, ".") != 0 &&
  119. strcmp(entry->d_name, "..") != 0)
  120. {
  121. nombre = entry->d_name;
  122.  
  123. if (remove(nombre)==-1)
  124. Cmd_deltree(nombre);
  125. else
  126. Cmd_delete(nombre);
  127.  
  128. }
  129. }
  130. closedir(folder);
  131. chdir("..");
  132. rmdir(entrada);
  133.  
  134. }
  135. /***********************************************************/
  136. int TrocearCadena(char * cadena, char * trozos[])
  137. {
  138. int i=1;
  139. if ((trozos[0]=strtok(cadena," \n\t"))==NULL)
  140. return 0;
  141. while ((trozos[i]=strtok(NULL," \n\t"))!=NULL)
  142. i++;
  143. return i;
  144. }
  145. /***********************************************************/
  146. int CodigoEntrada (char *tr){
  147. if (!strcmp(tr,"fin"))
  148. return FIN;
  149. if (!strcmp(tr,"quit"))
  150. return FIN;
  151. if (!strcmp(tr,"exit"))
  152. return FIN;
  153. if (!strcmp(tr,"pid"))
  154. return PID;
  155. if (!strcmp(tr,"pwd"))
  156. return PWD;
  157. if (!strcmp(tr,"chdir"))
  158. return CHDIR;
  159. if (!strcmp(tr,"list"))
  160. return LIST;
  161. if (!strcmp(tr,"delete"))
  162. return DELETE;
  163. if (!strcmp(tr,"deltree"))
  164. return DELTREE;
  165. }
  166. /***********************************************************/
  167. void ProcesarEntrada (char *tr[], int numArg){
  168. switch (CodigoEntrada(tr[0])){
  169. case FIN: exit (0); break;
  170. case PID: Cmd_pid(); break;
  171. case PWD: Cmd_pwd();break;
  172. case CHDIR: if (numArg > 1) Cmd_chdir(tr[1]); else printf("Faltan argumentos\n"); break;
  173. case LIST: if (numArg > 1) Cmd_list(tr[1]); else printf("Faltan argumentos\n"); break;
  174. case DELETE: if (numArg > 1) Cmd_delete(tr[1]); else printf("Faltan argumentos\n"); break;
  175. case DELTREE: if (numArg > 1) Cmd_deltree(tr[1]); else printf("Faltan argumentos\n"); break;
  176.  
  177. }
  178. }
  179. /***********************************************************/
  180. int main (int argc, char *argv[])
  181. {
  182. char entrada[MAXENTRADA];
  183. char *tr[MAXENTRADA/2];
  184. int numArg;
  185.  
  186. while (1) {
  187. printf ("terminal:~$ ");
  188. fgets (entrada,MAXENTRADA,stdin);
  189. numArg =TrocearCadena(entrada,tr);
  190. if (numArg < 1)
  191. continue;
  192. ProcesarEntrada(tr, numArg);
  193. }
  194. }
  195. /***********************************************************/
Advertisement
Add Comment
Please, Sign In to add comment