elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recuerda que debes registrarte en el foro para poder participar (preguntar y responder)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  PRoblema MInishell
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: PRoblema MInishell  (Leído 4,572 veces)
antonazo211

Desconectado Desconectado

Mensajes: 1


Ver Perfil
PRoblema MInishell
« en: 22 Noviembre 2011, 15:41 pm »

hola, estoy implementando un código para la creación de una minishell en ubuntu, cuando al hacer 'make' en la terminal de ubuntu me aparecen estos warnings:
gcc -Wall -g   -c -o msh.o msh.c
msh.c: In function ‘num1’:
msh.c:95:17: warning: ‘argvv’ is used uninitialized in this function
msh.c:129:7: warning: ‘bg’ may be used uninitialized in this function
msh.c: In function ‘num2’:
msh.c:152:27: warning: ‘argvv’ may be used uninitialized in this function
msh.c:164:4: warning: ‘bg’ is used uninitialized in this function
msh.c: In function ‘num3’:
msh.c:184:27: warning: ‘argvv’ may be used uninitialized in this function
msh.c:212:4: warning: ‘bg’ is used uninitialized in this function
msh.c: In function ‘numa’:
msh.c:227:2: warning: ‘num_commands’ is used uninitialized in this function
msh.c:238:29: warning: ‘argvv’ may be used uninitialized in this function
msh.c:270:4: warning: ‘bg’ is used uninitialized in this function
gcc -Wall -g  -o msh parser.o scanner.o y.o msh.o


ESTE ES EL CÓDIGO IMPLEMENTADO:
/*-
 * msh.c
 *
 * Minishell C source
 * Show how to use "obtain_order" input interface function
 *
 * THIS FILE IS TO BE MODIFIED
 */
#include <stdlib.h>      /* Gestión de memoria dinámica, control de procesos y otros */
#include <stddef.h>      /* Se defienen algunos tipos especiales */
#include <stdio.h>      /* Entrada/Salida */
#include <sys/types.h>      /* Contiene construcciones que facilitan la obtención de información sobre los atrinbutos de los archivos */
#include <unistd.h>      /* Se define una función */
#include <sys/wait.h>      /* Define constantes para el uso de 'waitpid()' */
#include <fcntl.h>      /* Manipula el descriptor de fichero */
#include <dirent.h>      /* Incluye definiciones para directorios de entrada */
#include <string.h>      /* Contiene definicion de macros, constantes y tipos de utilidad para trabajar con cadenas de caracteres */

extern int obtain_order();      /* See parser.y for description */
void myls(char*);
void mycd(char*);
int num1();
int num2();
int num3();
int numa();

void myls(char * dir){
   
   if(dir==NULL){
      char *punt=".";
      dir=punt;
   }
   
   DIR *d;      
   d=opendir(dir);            /*Se encarga de abrir el directorio */
   struct dirent * fich;
   while((fich=readdir(d))!=NULL){    /*Se encarga de leer todos los elementos del directorio*/
      printf("%s\n",fich -> d_name);  
   }
   closedir(d);            /* Una vez leido todo cierra el directorio */
}
void mycd(char * dir){
   chdir(dir);              /* Cambia de directorio */
   char *path;
   char p[1024];
   path=p;
   path=getcwd(path,sizeof(p));        /* Indica el directorio en el que nos encontramos */
   printf("%s\n", path);         /* Imprime por pantalla el directorio en el que nos encontramos*/
}

int main(void){
   char ***argvv;
   int num_commands;
   char *filev[3];
   int bg;
   int ret;

   setbuf(stdout, NULL);         /* Unbuffered */
   setbuf(stdin, NULL);

   while (1){
      fprintf(stderr, "%s", "msh> ");   /* Prompt */
      ret = obtain_order(&argvv, filev, &bg);
      if (ret == 0) break;      /* EOF */
      if (ret == -1) continue;   /* Syntax error */
      num_commands = ret - 1;      /* Line */
      switch(num_commands){
      case 0:
            continue;   /* Empty line */
      break;
      case 1:
         num1();
      break;   
      case 2:
         num2();
         
      break;
      case 3:
         num3();
      break;
      default:
         numa();
      break;
      }
   }
return 0;
}

int num1(){
   char ***argvv;
   char *filev[3];
   int bg;
   int k;
   int pid;
   if(strcmp(argvv[0][0], "mycd")==0){      /* Comparamos para ver si son iguales */
      mycd(argvv[0][1]);
   }
   else{
      pid=fork();
      switch(pid){                        /* Creacion proceso hijo */   
         case 0:
            /*mandatos redirecciones simples*/                        
            if(filev[0]!=NULL){             /* Apuntara al nombre del fichero a utilizar en la redireccion de entrada en caso de que exista o NULL si no hay ninguna */
               int in=open(filev[0], O_RDONLY);   
               close(STDIN_FILENO);
               dup(in);
               close(in);
            }
            if(filev[1]!=NULL){            /* Apuntara al nombre del fichero a utilizar en la redireccion de salida en caso de que exista o NULL si no hay ninguna */
               int out=open(filev[1],O_CREAT|O_TRUNC| O_WRONLY,0777);   
               close(STDOUT_FILENO);
               dup(out);
               close(out);
            }
            if(filev[2]!=NULL){               /* Apuntara al nombre del fichero a utilizar en la redireccion de salida de error en caso de que exista o NULL si no hay ninguna */  
               int out=open(filev[2],O_CREAT|O_TRUNC| O_WRONLY,0777);
               close(STDERR_FILENO);
               dup(out);
               close(out);
            }
            if(strcmp(argvv
  • , "myls")==0){
               myls(argvv[0][1]);
               exit(0);
            }
            execvp(argvv[0][0],argvv[0]);
            break;
            /* Mandato en background*/
         default:
            if(bg==0){
               wait(&k);
            }
            break;
      }   
   }
return 0;
}

int num2(){
   char ***argvv;
   int bg;
   int k;
   int pid1, pid2;
   int tb;
   int fd[2];
   tb=pipe(fd);
   pid1=fork();
   if(pid1==0){   
      close(fd[0]);
      close(STDOUT_FILENO);
      dup(fd[1]);
      close(fd[1]);   
      execvp(argvv[0][0],argvv[0]);
   }
   pid2=fork();
   if(pid2==0){
      close(fd[1]);
      close(STDIN_FILENO);
      dup(fd[0]);
      close(fd[0]);
      execvp(argvv[1][0],argvv[1]);
   }
   close(fd[0]);
   close(fd[1]);  
   if(bg==0){         
      wait(&k);
      wait(&k);
   }
return 0;
}
int num3(){
   char ***argvv;
   int bg;
   int k;
   int pid1, pid2, pid3;
   int tb, tb1;
   int fd1[2];
   tb1=pipe(fd1);
   pid1=fork();
   if(pid1==0){
      close (fd1[0]);   
      close(STDOUT_FILENO);
      dup(fd1[1]);
      close(fd1[1]);   
      execvp(argvv[0][0],argvv[0]);   
   }
   int fd2[2];
   tb=pipe(fd2);
   pid2=fork();
   if (pid2==0){
      close(fd1[1]);
      close(fd2[0]);
      close(STDOUT_FILENO);
      dup(fd2[1]);
      close(STDIN_FILENO);
      dup(fd1[0]);
      close(fd2[1]);
      close(fd1[0]);
      execvp(argvv[1][0],argvv[1]);
   }
   close(fd1[0]);
   close(fd1[1]);
   pid3=fork();
   if(pid3==0){
      close(fd2[1]);
      close(STDIN_FILENO);
      dup(fd2[0]);
      close(fd2[0]);
      execvp(argvv[2][0],argvv[2]);
   }
   close(fd2[0]);  
   close(fd2[1]);
   if(bg==0){         
      wait(&k);
      wait(&k);
      wait(&k);
   }
return 0;
}

int numa(){
   char ***argvv;
   int num_commands;
   int bg;
   int k;
   int tb, tb2;
   int i;
   int fd[num_commands][2];
   int pid[num_commands];
   for(i=0;i<num_commands;i++){
      if(i==0){
         tb=pipe(fd[0]);
         pid=fork();
         if(pid==0){
            close(fd
  • );
            close(STDOUT_FILENO);
            dup(fd[1]);
            close(fd[1]);                  
            execvp(argvv
  • ,argvv);
         }
      }
      else if(i<num_commands-1){
         tb2=pipe(fd);
         close(fd[i-1][1]);
         pid=fork();
         if(pid==0){
            close(fd
  • );
            close(STDOUT_FILENO);
            dup(fd[1]);
            close(fd[1]);
            close(STDIN_FILENO);
            dup(fd[i-1][0]);
            close(fd[i-1][0]);
            execvp(argvv
  • ,argvv);      
         }
         close(fd[i-1][0]);/*cerrar canal entrada tuberia del padre*/
      }
      else{
         pid=fork();
         if(pid==0){
            close(fd[i-1][1]);
            close(STDIN_FILENO);
            dup(fd[i-1][0]);
            close(fd[i-1][0]);
            execvp(argvv
  • , argvv);
         }
         close(fd[i-1][0]);
         close(fd[i-1][1]);
      }   
   }
   if(bg==0){
      for(i=0;i<num_commands;i++){
         wait(&k);
      }
   }
return 0;   
}


« Última modificación: 22 Noviembre 2011, 15:45 pm por antonazo211 » En línea

n3w

Desconectado Desconectado

Mensajes: 87


¿Libertad? ¿Para qué?


Ver Perfil
Re: PRoblema MInishell
« Respuesta #1 en: 22 Noviembre 2011, 23:06 pm »

Haces un :
Código:
char ***argvv;
y

Código:
int bg;


Y luego llamas a :
Código:
ret = obtain_order(&argvv, filev, &bg);

Sin haber reservado memoria o inicializar las variables a algún valor.


En línea

n3w

Desconectado Desconectado

Mensajes: 87


¿Libertad? ¿Para qué?


Ver Perfil
Re: PRoblema MInishell
« Respuesta #2 en: 22 Noviembre 2011, 23:08 pm »

Te has parado a leer ese código? lo digo porque hay funciones como num1() o num2() en los que repites las variables ,no me he parado mucho.
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Problema BlueZScanner y problema de conexión
Hacking Mobile
Kasswed 3 6,301 Último mensaje 6 Mayo 2006, 22:04 pm
por Gospel
Duda minishell
Programación C/C++
RADIACTIVO 4 2,160 Último mensaje 6 Abril 2014, 10:16 am
por RADIACTIVO
Programación de Minishell
Programación C/C++
depresion100 2 1,714 Último mensaje 16 Abril 2016, 16:09 pm
por HardForo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines