Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Javier Sobrino González
- Javier Loureiro Pérez
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <time.h>
- #include <sys/types.h>
- #include <limits.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <sys/utsname.h>
- #include <pwd.h>
- #include <grp.h>
- #include <dirent.h>
- #include "ListaHistorial.h"
- #define MaxTrozos 512
- #define MaxHist 4096
- char linea[4096];
- char *trozos[MaxTrozos];
- int numtrozos;
- void ejecutarComando(char *linea);
- 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;
- }
- //Comandos del shell
- //Comando autores
- void cmdAutores(){
- char flagLogin= 1, flagNombre=1;
- if(numtrozos > 1 && strcmp(trozos[1], "-l") == 0)
- flagNombre= 0;
- if(numtrozos > 1 && strcmp(trozos[1], "-n") == 0)
- flagLogin = 0;
- if(flagLogin){
- printf("Login: j.loureirop\n");
- printf("Login: javier.sobrino\n");
- }
- if(flagNombre){
- printf("Nombre: Javier Loureiro\n");
- printf("Nombre: Javier Sobrino\n");
- }
- }
- //Comando Fin (sale del shell)
- void cmdFin(){
- exit(0);
- }
- //Comando Pid
- void cmdPid(){
- if(numtrozos == 1)
- printf("Pid del shell: %d\n", getpid());
- else if (numtrozos > 1)
- printf("Pid del padre del shell: %d\n", getppid());
- }
- //Comando Carpeta
- void cmdCarpeta(){
- char ruta[PATH_MAX];
- if(numtrozos == 1){
- if(getcwd(ruta, PATH_MAX) == NULL){
- perror("getcwd");
- return;
- }
- else{
- printf("%s\n", ruta);
- return;
- }
- }
- else if(numtrozos > 1){
- if (chdir(trozos[1]) == -1) {perror("chdir"); return;}
- else
- chdir(trozos[1]);
- }
- }
- //Comando comandoN
- void cmdComandoN(){
- int n;
- if(numtrozos == 1) {printf("Falta número de comando \n"); return;}
- n = atoi(trozos[1]);
- if(n < 0 || n >= histNumElementos()) {printf("Número de comando fuera de rango \n"); return;}
- else {
- printf(" %s",histElemento(n));
- }
- }
- //Comando fecha
- void cmdFecha(){
- struct tm *t;
- time_t tiempo;
- if(time(&tiempo) == -1) {perror("time"); return;}
- t = localtime(&tiempo);
- if(numtrozos > 1 && strcmp(trozos[1], "-d") == 0)
- printf("%d/%d/%d\n", t->tm_mday, t->tm_mon, t->tm_year+1900);
- else if(numtrozos > 1 && strcmp(trozos[1], "-h") == 0)
- printf("%d:%d:%d\n", t->tm_hour, t->tm_min, t->tm_sec);
- else{
- printf("%d:%d:%d\n", t->tm_hour, t->tm_min, t->tm_sec);
- printf("%d/%d/%d\n", t->tm_mday, t->tm_mon, t->tm_year+1900);
- }
- }
- //Comando historial
- void histImprimeN(){
- int i;
- for(i = 0; i <= trozos[1][1] - 48; i++){
- printf("%d->%s", i, histElemento(i));
- }
- }
- void cmdHist(){
- int i;
- if(numtrozos == 1){
- for(i = 0; i < histNumElementos() ; i++){
- printf("%d->%s", i, histElemento(i));
- }
- }
- else if(numtrozos > 1 && strcmp(trozos[1], "-c") == 0){
- histBorrar();
- return;
- }
- else if(numtrozos > 1)
- histImprimeN();
- }
- //Comando infosis
- void cmdInfosis(){
- struct utsname system;
- uname(&system);
- printf("%s (%s), OS: %s %s-%s\n", system.nodename, system.machine, system.sysname,
- system.release, system.version);
- }
- /*PRACTICA 1*/
- //Comando create
- void cmdCreate(){
- char ruta[PATH_MAX];
- if (numtrozos==1){
- getcwd(ruta, PATH_MAX);
- printf("%s\n", ruta);
- return;
- }else if(numtrozos==2){
- if(strcmp(trozos[1],"-f")==0){
- getcwd(ruta, PATH_MAX);
- printf("%s\n", ruta);
- return;
- }else{
- mkdir(trozos[1],0777);
- if(errno != 0){
- printf("Imposible crear: %s\n",strerror(errno));
- }
- }
- }else if(numtrozos==3){
- if(strcmp(trozos[1], "-f")==0){
- getcwd(ruta, PATH_MAX);
- strcat(ruta,"/");
- strcat(ruta,trozos[2]);
- FILE *fp;
- fp = fopen(trozos[2],"w");
- if(fp != NULL){
- fclose(fp);
- }
- if(errno != 0){
- printf("Imposible crear: %s\n",strerror(errno));
- }
- }
- }
- }
- //Comando borrar
- void cmdDelete(){
- int i;
- for(i=1;i<numtrozos;i++){
- if(errno != 0){
- printf("aaa %s\n",strerror(errno));
- }
- remove(trozos[i]);
- if(errno != 0){
- printf("%s\n",strerror(errno));
- }
- }
- }
- //
- void st_mode_to_str(mode_t st_mode, char *mode){
- mode[0]= ' '; //(S_ISDIR(fileStat.st_mode)) ? 'd' : '-';
- mode[1]= (st_mode & S_IRUSR) ? 'r' : '-';
- mode[2]= (st_mode & S_IWUSR) ? 'w' : '-';
- mode[3]= (st_mode & S_IXUSR) ? 'x' : '-';
- mode[4]= (st_mode & S_IRGRP) ? 'r' : '-';
- mode[5]= (st_mode & S_IWGRP) ? 'w' : '-';
- mode[6]= (st_mode & S_IXGRP) ? 'x' : '-';
- mode[7]= (st_mode & S_IROTH) ? 'r' : '-';
- mode[8]= (st_mode & S_IWOTH) ? 'w' : '-';
- mode[9]= (st_mode & S_IXOTH) ? 'x' : '-';
- mode[10]= 0;
- }
- //Comando stat
- void cmdStat(){
- struct stat *statbuf;
- statbuf = malloc(sizeof(struct stat));
- char ruta[PATH_MAX];
- int flagLong=0, flagAcc=0, flagLink=0; //flags para detectar las opciones que se pasan
- struct passwd *pws; //Id dispositivo
- struct group *grp; //Id grupo
- struct tm dtc, dta;
- //Introducen stat
- if (numtrozos == 1)
- cmdCarpeta();
- //Introducen stat (algo)
- if(numtrozos == 2){
- getcwd(ruta, PATH_MAX);
- strcat(ruta, "/");
- strcat(ruta, trozos[1]);
- if(lstat(ruta, statbuf)== -1){
- printf("Ha habido un error: %s\n", strerror(errno));
- }
- else{
- printf("%ld %s\n", statbuf->st_size, trozos[1]);
- }
- }
- if(numtrozos>2){
- //Obtenemos los flags que se pasan
- for(int i = 1; i < numtrozos && trozos[i][0] == '-' ; i++){
- if(strcmp(trozos[i], "-long") == 0) flagLong = 1;
- else if(strcmp(trozos[i],"-link") == 0) flagLink = 1;
- else if(strcmp(trozos[i], "-acc") == 0) flagAcc = 1;
- }
- getcwd(ruta, PATH_MAX);
- strcat(ruta, "/");
- strcat(ruta, trozos[numtrozos-1]);
- if(lstat(ruta, statbuf)== -1){
- printf("Ha habido un error: %s\n", strerror(errno));
- }
- else{
- pws = getpwuid(statbuf->st_uid);
- grp = getgrgid(statbuf->st_gid);
- dtc = *(gmtime(&statbuf->st_ctime));
- dta = *(gmtime(&statbuf->st_atime));
- if(flagAcc == 1 && flagLong==0){
- printf("%d/%d/%d-%d:%.2d ", dta.tm_year + 1900, dta.tm_mon+1, dta.tm_mday,
- dta.tm_hour+2, dta.tm_min);
- }
- if(flagLong==1){
- //strmode(st_mode, statbuf->st_mode);
- char mode[11];
- st_mode_to_str(statbuf->st_mode,mode);
- printf("%d/%d/%d-%d:%.2d %ld, ( %ld) %s %s %s ", dtc.tm_year + 1900, dtc.tm_mon+1, dtc.tm_mday,
- dtc.tm_hour+2, dtc.tm_min,statbuf->st_nlink, statbuf->st_ino, pws->pw_name, grp->gr_name, mode);
- }
- if(flagLink==1){
- //hacer links
- }
- printf("%ld %s\n", statbuf->st_size, trozos[numtrozos-1]);
- }
- }
- }
- //Stat 2 (para list)
- void cmdStat2(const char d_name[],int flagLong, int flagAcc, char ruta[], int REC){
- struct stat *statbuf;
- statbuf = malloc(sizeof(struct stat));
- struct passwd *pws; //Id dispositivo
- struct group *grp; //Id grupo
- struct tm dtc, dta;
- if(numtrozos>2){
- if(REC==1){
- if(lstat(ruta, statbuf)== -1){
- }
- else{
- pws = getpwuid(statbuf->st_uid);
- grp = getgrgid(statbuf->st_gid);
- dtc = *(gmtime(&statbuf->st_ctime));
- dta = *(gmtime(&statbuf->st_atime));
- if(flagAcc == 1 && flagLong==0){
- printf("%d/%d/%d-%d:%.2d ", dta.tm_year + 1900, dta.tm_mon+1, dta.tm_mday,
- dta.tm_hour+2, dta.tm_min);
- }
- if(flagLong==1){
- //strmode(st_mode, statbuf->st_mode);
- char mode[11];
- st_mode_to_str(statbuf->st_mode,mode);
- printf("%d/%d/%d-%d:%.2d %ld, ( %ld) %s %s %s ", dtc.tm_year + 1900, dtc.tm_mon+1, dtc.tm_mday,
- dtc.tm_hour+2, dtc.tm_min,statbuf->st_nlink, statbuf->st_ino, pws->pw_name, grp->gr_name, mode);
- }
- printf("%ld %s\n", statbuf->st_size, d_name);
- }
- }
- else if(REC==0){
- if(lstat(d_name, statbuf)== -1){
- }
- else{
- pws = getpwuid(statbuf->st_uid);
- grp = getgrgid(statbuf->st_gid);
- dtc = *(gmtime(&statbuf->st_ctime));
- dta = *(gmtime(&statbuf->st_atime));
- if(flagAcc == 1 && flagLong==0){
- printf("%d/%d/%d-%d:%.2d ", dta.tm_year + 1900, dta.tm_mon+1, dta.tm_mday,
- dta.tm_hour+2, dta.tm_min);
- }
- if(flagLong==1){
- //strmode(st_mode, statbuf->st_mode);
- char mode[11];
- st_mode_to_str(statbuf->st_mode,mode);
- printf("%d/%d/%d-%d:%.2d %ld, ( %ld) %s %s %s ", dtc.tm_year + 1900, dtc.tm_mon+1, dtc.tm_mday,
- dtc.tm_hour+2, dtc.tm_min,statbuf->st_nlink, statbuf->st_ino, pws->pw_name, grp->gr_name, mode);
- }
- printf("%ld %s\n", statbuf->st_size, d_name);
- }
- }
- }
- }
- void cmdListaREC(const char *dirname,int fun, int flagHid, int flagLong){
- if(fun==0){
- DIR* dir = opendir(dirname);
- struct dirent* dirent;
- dirent = readdir(dir);
- char path[100];
- if (dir == NULL) {
- return;
- }
- printf("************%s\n",dirname);
- while (dirent != NULL) {
- strcpy(path, dirname);
- strcat(path, "/");
- strcat(path, dirent->d_name);
- if(dirent->d_name[0] != '.' || flagHid == 1){
- if(flagLong==1){
- cmdStat2(dirent->d_name,1,1,path,1);
- }else{
- printf("%hhd %s\n", dirent->d_type, dirent->d_name);
- }
- }
- dirent = readdir(dir);
- }
- closedir(dir);
- dir = opendir(dirname);
- if (dir == NULL) {
- return;
- }
- dirent = readdir(dir);
- while (dirent != NULL) {
- if (dirent->d_type == DT_DIR && strcmp(dirent->d_name, ".") != 0 && strcmp(dirent->d_name, "..") != 0 && (dirent->d_name[0] != '.'|| flagHid==1)){
- char path[100];
- strcpy(path, dirname);
- strcat(path, "/");
- strcat(path, dirent->d_name);
- cmdListaREC(path,0,flagHid,flagLong);
- }
- dirent = readdir(dir);
- }
- closedir(dir);
- }
- else if (fun==1){
- DIR* dir = opendir(dirname);
- struct dirent* dirent;
- dirent = readdir(dir);
- if (dir == NULL) {
- return;
- }
- while (dirent != NULL) {
- if (dirent->d_type == DT_DIR && strcmp(dirent->d_name, ".") != 0 && strcmp(dirent->d_name, "..") != 0 && (dirent->d_name[0] != '.' || flagHid ==1)) {
- char path[100];
- printf("************%s/%s\n",dirname,dirent->d_name);
- strcpy(path, dirname);
- strcat(path, "/");
- strcat(path, dirent->d_name);
- cmdListaREC(path,1,flagHid,flagLong);
- printf("************%s\n",dirname);
- }
- dirent = readdir(dir);
- }
- closedir(dir);
- dir = opendir(dirname);
- if (dir == NULL) {
- return;
- }
- dirent = readdir(dir);
- while (dirent != NULL) {
- char path[100];
- strcpy(path, dirname);
- strcat(path, "/");
- strcat(path, dirent->d_name);
- if(dirent->d_name[0] != '.' || flagHid == 1){
- if(flagLong==1){
- cmdStat2(dirent->d_name,1,1,path,1);
- }else{
- printf("%hhd %s\n", dirent->d_type, dirent->d_name);
- }
- }
- dirent = readdir(dir);
- }
- closedir(dir);
- }
- }
- //Comando list
- void cmdList(){
- char ruta[PATH_MAX];
- int flagHid=0,flagLong=0, flagAcc=0, flagReca=0, flagRecb=0; //flags para detectar las opciones que se pasan
- DIR *d;
- struct dirent *dirent;
- getcwd(ruta, PATH_MAX);
- strcat(ruta, "/");
- strcat(ruta, trozos[numtrozos-1]);
- if(numtrozos==1)
- cmdCarpeta();
- if(numtrozos==2){
- if((d=opendir(ruta)) == NULL){perror("opendir"); return;}
- printf("************%s\n",trozos[numtrozos-1]);
- while((dirent = readdir(d))!= NULL){
- if(dirent->d_name[0] != '.'){
- printf("%s\n", dirent->d_name);
- }
- }
- }
- if(numtrozos>2){
- //Obtenemos los flags que se pasan
- for(int i = 1; i < numtrozos && trozos[i][0] == '-' ; i++){
- if(strcmp(trozos[i],"-hid")==0) flagHid = 1;
- else if(strcmp(trozos[i], "-long") == 0) flagLong = 1;
- else if(strcmp(trozos[i], "-acc") == 0) flagAcc = 1;
- else if(strcmp(trozos[i], "-reca") == 0) flagReca = 1;
- else if(strcmp(trozos[i], "-recb") == 0) flagRecb = 1;
- }
- if((d=opendir(ruta)) == NULL){perror("opendir"); return;}
- if(flagAcc==1 && flagLong==0 && flagHid==0){
- printf("************%s\n",trozos[numtrozos-1]);
- while((dirent = readdir(d))!= NULL){
- if(dirent->d_name[0] != '.'){
- cmdStat2(dirent->d_name,0,1,ruta,0);
- }
- }
- }
- if(flagAcc==1 && flagLong==0 && flagHid==1){
- printf("************%s\n",trozos[numtrozos-1]);
- while((dirent = readdir(d))!= NULL){
- cmdStat2(dirent->d_name,0,1,ruta,0);
- }
- }
- if(flagHid==1 && flagLong==0 && flagAcc==0 && flagReca==0 && flagRecb==0){
- printf("************%s\n",trozos[numtrozos-1]);
- while((dirent = readdir(d))!= NULL){
- printf("%s\n", dirent->d_name);
- }
- }
- if(flagLong==1 && flagHid == 0 && flagAcc==0 && flagReca==0 && flagRecb==0){
- printf("************%s\n",trozos[numtrozos-1]);
- while((dirent = readdir(d))!= NULL){
- if(dirent->d_name[0] != '.'){
- cmdStat2(dirent->d_name,1,0,ruta,0);
- }
- }
- }
- if(flagLong==1 && flagHid == 1 && flagReca ==0 && flagRecb==0){
- printf("************%s\n",trozos[numtrozos-1]);
- while((dirent = readdir(d))!= NULL){
- getcwd(ruta, PATH_MAX);
- strcat(ruta, "/");
- strcat(ruta, trozos[numtrozos-1]);
- cmdStat2(ruta,1,0,ruta,0);
- }
- }
- if(flagReca==1){
- cmdListaREC(trozos[numtrozos-1],0,flagHid, flagLong);
- }else if(flagRecb==1){
- cmdListaREC(trozos[numtrozos-1],1,flagHid, flagLong);
- }
- }
- }
- //Comando ayuda
- void cmdAyuda() {
- if (numtrozos == 1) {
- printf("'ayuda cmd' donde cmd es uno de los siguientes comandos:\n"
- "fin salir bye fecha pid autores hist comando carpeta infosis ayuda\n");
- } else if (numtrozos > 1 && strcmp(trozos[1], "fin") == 0) {
- printf("fin \tTermina la ejecucion del shell\n");
- } else if (numtrozos > 1 && strcmp(trozos[1], "salir") == 0) {
- printf("salir \tTermina la ejecucion del shell\n");
- } else if (numtrozos > 1 && strcmp(trozos[1], "bye") == 0) {
- printf("bye \tTermina la ejecucion del shell\n");
- } else if (numtrozos > 1 && strcmp(trozos[1], "fecha") == 0) {
- printf("fecha [-d|.h\tMuestra la fecha y o la hora actual\n");
- } else if (numtrozos > 1 && strcmp(trozos[1], "pid") == 0) {
- printf("pid [-p]\tMuestra el pid del shell o de su proceso padre\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "autores") == 0) {
- printf("autores [-n|-l]\tMuestra los nombres y logins de los autores\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "hist") == 0) {
- printf("hist [-c|-N]\tMuestra el historico de comandos, con -c lo borra\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "comando") == 0) {
- printf("comando [-N]\tRepite el comando N (del historico)\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "carpeta") == 0) {
- printf("carpeta [dir]\tCambia (o muestra) el directorio actual del shell\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "carpeta") == 0) {
- printf("infosis \tMuestra informacion de la maquina donde corre el shell\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "ayuda") == 0) {
- printf("ayuda [cmd]\tMuestra ayuda sobre los comandos\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "create") == 0) {
- printf("create [-f] [name] Crea un directorio o un fichero (-f)\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "stat") == 0) {
- printf("stat [-long][-link][-acc] name1 name2 .. lista ficheros;\n-long: listado largo\n-acc: acesstime\n-link: si es enlace simbolico, el path contenido\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "list") == 0) {
- printf("list [-reca] [-recb] [-hid][-long][-link][-acc] n1 n2 .. lista contenidos de directorios\n-hid: incluye los ficheros ocultos\n-reca: recursivo (antes)\n-recb: recursivo (despues)\nresto parametros como stat\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "delete") == 0) {
- printf("delete [name1 name2 ..] Borra ficheros o directorios vacios\n");
- }else if (numtrozos > 1 && strcmp(trozos[1], "deltree") == 0) {
- printf("deltree [name1 name2 ..] Borra ficheros o directorios no vacios recursivamente\n");
- }
- }
- struct cm_entrada{
- char *cm_nombre;
- void (*cm_fun)();
- };
- struct cm_entrada cm_tabla[] = {
- {"autores", cmdAutores},
- {"ayuda", cmdAyuda},
- {"bye", cmdFin},
- {"carpeta", cmdCarpeta},
- {"comando", cmdComandoN},
- {"create", cmdCreate},
- {"delete", cmdDelete},
- {"fecha", cmdFecha},
- {"fin", cmdFin},
- {"hist", cmdHist},
- {"infosis", cmdInfosis},
- {"pid", cmdPid},
- {"salir", cmdFin},
- {"stat", cmdStat},
- {"list", cmdList},
- };
- void ejecutarComando(char *linea){
- int i;
- char *copialinea = strdup(linea);
- numtrozos = TrocearCadena(copialinea, trozos);
- if(numtrozos == 0) {free(copialinea); return;}
- for( i=0; ; i++){
- if(cm_tabla[i].cm_nombre == NULL){
- printf("%s: comando no reconocido\n", trozos[0]);
- free(copialinea);
- break;
- }
- if(strcmp(cm_tabla[i].cm_nombre, trozos[0]) == 0){
- cm_tabla[i].cm_fun();
- free(copialinea);
- break;
- }
- }
- }
- int main(){
- while(1){
- printf("@>"); //Prompt
- if( fgets(linea, 4096, stdin) == NULL ){
- exit(0);
- }
- ejecutarComando(linea);
- histInsert(linea);
- }
- for(int i=0; i <= nhist; i++){
- free(Hist[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement