Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <sys/types.h>
- #define MAXENTRADA 2048
- #define FIN 1
- #define PID 2
- #define PWD 3
- #define CHDIR 4
- #define LIST 5
- #define DELETE 6
- #define DELTREE 7
- /***********************************************************/
- void Cmd_pwd()
- {
- char dir[MAXENTRADA];
- if (getcwd(dir,MAXENTRADA)==NULL)
- perror("imposible obtener directorio actual");
- else
- printf ("%s\n",dir);
- }
- /***********************************************************/
- void Cmd_chdir(char * dir)
- {
- if (dir==NULL)
- Cmd_pwd();
- else if (chdir(dir)==-1)
- perror ("Imposible cambiar de directorio");
- }
- /***********************************************************/
- void Cmd_pid(void)
- {
- printf ("Pid de shell: %d, pid padre del shell: %d\n",getpid(),getppid());
- }
- /***********************************************************/
- void Cmd_list(char *entrada[], int numArg){
- /* Variables */
- char *dir;
- char *tr;
- DIR *dirp;
- int buf;
- char * directorio;
- char ent[MAXENTRADA];
- struct dirent *direntp;
- struct stat estructura;
- int l=0, r=0, h=0, i;
- char ruta[MAXENTRADA] = ".";
- // procesar entrada del list
- for (i=0; i<numArg; i++){
- if (!strcmp(entrada[i], "-h")) h=1;
- else if (strcmp(entrada[i], "-r")) r=1;
- else if (strcmp(entrada[i], "-l")) l=1;
- else strcpy(ruta, entrada[i]);
- }
- /* Abrimos el directorio */
- directorio=getcwd(ent,MAXENTRADA);
- dirp = opendir(directorio);
- if (dirp == NULL){
- printf("Error: No se puede abrir el directorio\n");
- exit(2);
- }
- /* Leemos las entradas del directorio */
- while ( (direntp=readdir(dirp)) != NULL){
- stat(directorio,&estructura);
- 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);
- printf( (S_ISDIR(estructura.st_mode)) ? "d" : "-");
- printf( (estructura.st_mode & S_IRUSR) ? "r" : "-");
- printf( (estructura.st_mode & S_IWUSR) ? "w" : "-");
- printf( (estructura.st_mode & S_IXUSR) ? "x" : "-");
- printf( (estructura.st_mode & S_IRGRP) ? "r" : "-");
- printf( (estructura.st_mode & S_IWGRP) ? "w" : "-");
- printf( (estructura.st_mode & S_IXGRP) ? "x" : "-");
- printf( (estructura.st_mode & S_IROTH) ? "r" : "-");
- printf( (estructura.st_mode & S_IWOTH) ? "w" : "-");
- printf( (estructura.st_mode & S_IXOTH) ? "x" : "-");
- }
- /* Cerramos el directorio */
- closedir(dirp);
- printf("\n");
- }
- /***********************************************************/
- void Cmd_delete(char *entrada){
- FILE *fichero;
- fichero= fopen( entrada, "r" );
- if( fichero != NULL ){
- fclose( fichero );
- if( remove(entrada) == 0 )
- printf( " FICHERO BORRADO\n" );
- else
- printf( "No pudo ser borrado\n" );
- }
- else printf( "Fichero no encontrado\n" );
- }
- /***********************************************************/
- void Cmd_deltree(char *entrada){
- DIR *folder;
- struct dirent *entry;
- char *nombre;
- Cmd_chdir(entrada);
- folder = opendir(entrada);
- while (entry = readdir(folder)){
- if (strcmp(entry->d_name, ".") != 0 &&
- strcmp(entry->d_name, "..") != 0)
- {
- nombre = entry->d_name;
- if (remove(nombre)==-1)
- Cmd_deltree(nombre);
- else
- Cmd_delete(nombre);
- }
- }
- closedir(folder);
- chdir("..");
- rmdir(entrada);
- }
- /***********************************************************/
- int TrocearCadena(char * cadena, char * trozos[])
- {
- int i=1;
- if ((trozos[0]=strtok(cadena," \n\t"))==NULL)
- return 0;
- while ((trozos[i]=strtok(NULL," \n\t"))!=NULL)
- i++;
- return i;
- }
- /***********************************************************/
- int CodigoEntrada (char *tr){
- if (!strcmp(tr,"fin"))
- return FIN;
- if (!strcmp(tr,"quit"))
- return FIN;
- if (!strcmp(tr,"exit"))
- return FIN;
- if (!strcmp(tr,"pid"))
- return PID;
- if (!strcmp(tr,"pwd"))
- return PWD;
- if (!strcmp(tr,"chdir"))
- return CHDIR;
- if (!strcmp(tr,"list"))
- return LIST;
- if (!strcmp(tr,"delete"))
- return DELETE;
- if (!strcmp(tr,"deltree"))
- return DELTREE;
- }
- /***********************************************************/
- void ProcesarEntrada (char *tr[], int numArg){
- switch (CodigoEntrada(tr[0])){
- case FIN: exit (0); break;
- case PID: Cmd_pid(); break;
- case PWD: Cmd_pwd();break;
- case CHDIR: if (numArg > 1) Cmd_chdir(tr[1]); break;
- case LIST: Cmd_list(tr, numArg); break;
- case DELETE: if (numArg > 1) Cmd_delete(tr[1]); else printf("Faltan argumentos\n"); break;
- case DELTREE: if (numArg > 1) Cmd_deltree(tr[1]); else printf("Faltan argumentos\n"); break;
- }
- }
- /***********************************************************/
- int main (int argc, char *argv[])
- {
- char entrada[MAXENTRADA];
- char *tr[MAXENTRADA/2];
- int numArg;
- while (1) {
- printf ("terminal:~$ ");
- fgets (entrada,MAXENTRADA,stdin);
- numArg =TrocearCadena(entrada,tr);
- if (numArg < 1)
- continue;
- ProcesarEntrada(tr, numArg);
- }
- }
- /***********************************************************/
Advertisement
Add Comment
Please, Sign In to add comment