Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1252 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  pb reprogrammation du shell en appel systeme pour ls et les pipes

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

pb reprogrammation du shell en appel systeme pour ls et les pipes

n°253405
ddpforman
Posté le 25-11-2002 à 20:28:55  profilanswer
 

voila, je galére pour faire le ls
et les pipes: les redirections: faut faire des tubes...etc...
 
quelqu'un l'aurait il dans ses archives peut etre?

mood
Publicité
Posté le 25-11-2002 à 20:28:55  profilanswer
 

n°253528
Taz@PPC
saloperie de i=`expr $i + 1`;
Posté le 25-11-2002 à 21:54:46  profilanswer
 

tu veux pas pluto nous donner un bout de code?


---------------
du bon usage de rand [C] / [C++]
n°253665
matafan
Posté le 26-11-2002 à 01:22:22  profilanswer
 

popen ça fait tout ça tout seul :


POPEN(3)            Linux Programmer's Manual            POPEN(3)
 
 
 
NAME
       popen, pclose - process I/O
 
SYNOPSIS
       #include <stdio.h>
 
       FILE *popen(const char *command, const char *type);
 
       int pclose(FILE *stream);
 
DESCRIPTION
       The  popen()  function opens a process by creating a pipe,
       forking, and invoking the shell.  Since a pipe is by defi-
       nition  unidirectional, the type argument may specify only
       reading or writing, not both; the resulting stream is cor-
       respondingly read-only or write-only.
 
       The  command  argument  is  a pointer to a null-terminated
       string containing a shell command line.  This  command  is
       passed  to  /bin/sh  using the -c flag; interpretation, if
       any, is performed by the shell.  The mode  argument  is  a
       pointer  to  a null-terminated string which must be either
       `r' for reading or `w' for writing.
 
       The return value from popen() is  a  normal  standard  I/O
       stream  in  all  respects save that it must be closed with
       pclose() rather than fclose().  Writing to such  a  stream
       writes to the standard input of the command; the command's
       standard output is the same as that of  the  process  that
       called  popen(),  unless  this  is  altered by the command
       itself.  Conversely, reading  from  a  ``popened''  stream
       reads  the  command's  standard  output, and the command's
       standard input is the same as that  of  the  process  that
       called popen.
 
       Note  that  output  popen  streams  are  fully buffered by
       default.
 
       The pclose function waits for the  associated  process  to
       terminate  and  returns  the exit status of the command as
       returned by wait4.
 
RETURN VALUE
       The popen function returns NULL if the fork(2) or  pipe(2)
       calls fail, or if it cannot allocate memory.
 
       The  pclose function returns -1 if wait4 returns an error,
       or some other error is detected.
 
ERRORS
       The popen function does not set errno if memory allocation
       fails.  If the underlying fork() or pipe() fails, errno is
       set appropriately.  If the mode argument is  invalid,  and
       this condition is detected, errno is set to EINVAL.
 
       If  pclose()  cannot obtain the child status, errno is set
       to ECHILD.
 
CONFORMING TO
       POSIX.2
 
BUGS
       Since the standard input of a command opened  for  reading
       shares  its  seek  offset  with  the  process  that called
       popen(), if the original process has done a buffered read,
       the command's input position may not be as expected.  Sim-
       ilarly, the output from a command opened for  writing  may
       become  intermingled  with  that  of the original process.
       The latter can be  avoided  by  calling  fflush(3)  before
       popen.
 
       Failure to execute the shell is indistinguishable from the
       shell's failure to execute command, or an  immediate  exit
       of the command.  The only hint is an exit status of 127.
 
HISTORY
       A  popen()  and  a pclose() function appeared in Version 7
       AT&T UNIX.
 
SEE ALSO
       fork(2), sh(1), pipe(2), wait4(2),  fflush(3),  fclose(3),
       fopen(3), stdio(3), system(3)
 
 
 
 
BSD MANPAGE                 1998-05-07                   POPEN(3)
 

n°253921
ddpforman
Posté le 26-11-2002 à 13:20:25  profilanswer
 

merciiiiiiiiii beaucoup pour cette commande, c'est un peu ça qu'il fallait :-)  
trop merci :-) je vais regarder ça de plus prés
si vous avez d'autres détails: n'hesitez pas....  
 
pour le LS (c'est le ls -al) :  
j'ai ça  :  
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <dirent.h>
  8. #include <error.h>
  9. #include <time.h>
  10. #include <pwd.h>
  11. #include <grp.h>
  12. #define TAILLE_CHAINE 256
  13. #define TRUE 1
  14. #define FALSE 0
  15. char *convertir_en_date(time_t *d)
  16. {
  17.   int i;
  18.   int cpt = 0;
  19.   char *date1 = (char *) malloc (TAILLE_CHAINE * sizeof(char));
  20.   char *date2 = (char *) malloc (TAILLE_CHAINE * sizeof(char));
  21.   date1 = ctime(d);
  22.   for (i=4; i<(strlen(date1)-9); i++)
  23.     {
  24.       date2[i-4] = date1[i];
  25.       cpt++;
  26.     }
  27.   date2[cpt+1] = '\0';
  28.   return date2;
  29. }
  30. char estRep(char *nom){
  31. DIR *dir; 
  32. if (dir=opendir(nom)){
  33.     printf("d" );
  34.     closedir(dir);
  35.   }
  36.   else printf("-" );
  37. }
  38. void lister_rep(){
  39.   DIR *dir;
  40.   struct dirent *sd;
  41.   struct stat *buf;
  42.   struct passwd *pwd;
  43.   struct group *grp;
  44.   char *rep;
  45.   unsigned long int taille1 = 0;
  46.   unsigned long int taille2 = 0;
  47.   //recuperatiopn du nom du repertoire courant
  48.   rep = (char *) malloc (TAILLE_CHAINE * sizeof(char));
  49.   rep = getcwd(rep,TAILLE_CHAINE);
  50.  
  51.   //ouverture du repertoire
  52.   dir = opendir(rep);
  53.   if (dir != NULL)
  54.     {
  55.       buf = (struct stat *) malloc(sizeof(struct stat));
  56.      
  57.       //tant qu on a des entrees dans le repertoire
  58.       while ((sd=readdir(dir))!=NULL){
  59.        
  60.         //recuperation du nom du fichier ou du repertoire sur lequel est positionne le flux
  61.         lstat(sd->d_name,buf);
  62.        
  63.         if (5) //((strcmp(sd->d_name,"." ))
  64.           //&&  
  65.           //(strcmp(sd->d_name,".." )))
  66.           {
  67.             estRep(sd->d_name);   
  68.             fprintf (stdout,buf->st_mode & S_IRUSR? "r":"-" );
  69.             fprintf (stdout,buf->st_mode & S_IWUSR? "w":"-" );
  70.             fprintf (stdout,buf->st_mode & S_IXUSR? "x":"-" );
  71.            
  72.             fprintf (stdout,buf->st_mode & S_IRGRP? "r":"-" );
  73.             fprintf (stdout,buf->st_mode & S_IWGRP? "w":"-" );
  74.             fprintf (stdout,buf->st_mode & S_IXGRP? "x":"-" );
  75.            
  76.             fprintf (stdout,buf->st_mode & S_IROTH? "r":"-" );
  77.             fprintf (stdout,buf->st_mode & S_IWOTH? "w":"-" );
  78.             fprintf (stdout,buf->st_mode & S_IXOTH? "x":"-" );
  79.            
  80.             fprintf (stdout," %d ",buf->st_nlink);
  81.             pwd = getpwuid(buf->st_uid);
  82.             fprintf (stdout,"%s ",pwd->pw_name);
  83.             grp = getgrgid(buf->st_gid);
  84.             fprintf (stdout,"%s ",grp->gr_name);
  85.             fprintf (stdout,"%lu ",buf->st_size);
  86.             fprintf (stdout,"%s ",convertir_en_date(&(buf->st_mtime)));
  87.             fprintf(stdout,"%s\n",sd->d_name);
  88.             if (buf->st_size % 1024)
  89.               { taille1 = (buf->st_size / 1024) +1;}
  90.             else { taille1 = buf->st_size / 1024;}
  91.             taille2 += taille1;
  92.           }
  93.         else
  94.           {
  95.             if (strcmp(sd->d_name,".." ))
  96.               {
  97.                 if (buf->st_size % 1024)
  98.                   { taille1 = (buf->st_size / 1024) +1;}
  99.                 else { taille1 = buf->st_size / 1024;}
  100.                 taille2 += taille1;
  101.               }
  102.           }
  103.       }
  104.       fprintf (stdout,"taille : %lu\n",taille2);
  105.     }
  106.   //fermeture du flux
  107.   closedir(dir);
  108. }
  109. int main (int argc,char *argv[]){
  110.  
  111.   lister_rep();
  112.   exit(0);
  113.  
  114. }


 
voila, ça fait peut etre beaucoup la
mais en fait, j'obtiens pas qq chose de terrible...
il n'y aurait pas les sources dans linux? caché dans un répertoire... ?
 
merci d'avance!!!


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  pb reprogrammation du shell en appel systeme pour ls et les pipes

 

Sujets relatifs
integration au shellintegration au shell
faire appel a une fonction qui se trouve dans un autre fichier ![VBA] Pile d'appel
pb de poinreur lors d'appel de fonctionsFonction en Shell...
[Shell Script] Rapatrier automatiquement un fichier via FTPsystème de stats
Projet en Shell - Help !!![Shell - Batch] je connais rien en batch !
Plus de sujets relatifs à : pb reprogrammation du shell en appel systeme pour ls et les pipes


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR