From f6254d0cf979631b7ce6a1738ce3a0847f21c581 Mon Sep 17 00:00:00 2001 From: KaneRoot Date: Thu, 3 Feb 2011 13:47:29 +0100 Subject: [PATCH] files added --- README | 0 chrono.pl | 26 ++++ daemon.pl | 17 +++ eko.c | 30 +++++ eko2.c | 28 +++++ enMajuscule.pl | 40 ++++++ exemple.c | 24 ++++ exoPointeurs.c | 14 +++ getpwent.pl | 20 +++ getpwent2.pl | 8 ++ humanornot.pl | 26 ++++ humanornot2.pl | 21 ++++ lePlusGros.pl | 45 +++++++ main.c | 312 +++++++++++++++++++++++++++++++++++++++++++++++ matrice1.c | 56 +++++++++ matrice2.c | 66 ++++++++++ max.pl | 16 +++ maxcourt.pl | 15 +++ minmax.c | 50 ++++++++ monswap.c | 20 +++ morceau_miroir.c | 49 ++++++++ multi_chrono.pl | 36 ++++++ nbmots.pl | 11 ++ silent_cmp.pl | 29 +++++ squeeze.c | 37 ++++++ strdup2.c | 52 ++++++++ swapp.c | 20 +++ testargs.c | 15 +++ tout.c | 85 +++++++++++++ tube.pl | 14 +++ wc.pl | 15 +++ wordcount.c | 40 ++++++ 32 files changed, 1237 insertions(+) mode change 100644 => 100755 README create mode 100755 chrono.pl create mode 100755 daemon.pl create mode 100755 eko.c create mode 100755 eko2.c create mode 100755 enMajuscule.pl create mode 100755 exemple.c create mode 100755 exoPointeurs.c create mode 100755 getpwent.pl create mode 100755 getpwent2.pl create mode 100755 humanornot.pl create mode 100755 humanornot2.pl create mode 100755 lePlusGros.pl create mode 100755 main.c create mode 100755 matrice1.c create mode 100755 matrice2.c create mode 100755 max.pl create mode 100755 maxcourt.pl create mode 100755 minmax.c create mode 100755 monswap.c create mode 100755 morceau_miroir.c create mode 100755 multi_chrono.pl create mode 100755 nbmots.pl create mode 100755 silent_cmp.pl create mode 100755 squeeze.c create mode 100755 strdup2.c create mode 100755 swapp.c create mode 100755 testargs.c create mode 100755 tout.c create mode 100755 tube.pl create mode 100755 wc.pl create mode 100755 wordcount.c diff --git a/README b/README old mode 100644 new mode 100755 diff --git a/chrono.pl b/chrono.pl new file mode 100755 index 0000000..5f8878e --- /dev/null +++ b/chrono.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl -w +use strict; + +my $nbsec = 0; +sub seconde +{ + alarm(1); + $nbsec += 1; +} +sub inter +{ + print "\n".$nbsec." secondes ! PID : ".$$; +} +sub arret +{ + print "\nNombre de secondes : ".$nbsec." Arret de : ".$$." (chrono.pl)"; + exit(0); +} + +$SIG{"ALRM"} = "seconde"; +$SIG{"INT"} = "inter"; +$SIG{"QUIT"} = "arret"; + +alarm(1); +print "\nDébut " . $$ . " !"; +while(1){} diff --git a/daemon.pl b/daemon.pl new file mode 100755 index 0000000..e361d3a --- /dev/null +++ b/daemon.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl -w +use strict; + +my $pid = fork(); + +if($pid == 0) +{ + while (1) + { + print "On se détache\n" ; + sleep(1); + } +} +else +{ + exit; +} diff --git a/eko.c b/eko.c new file mode 100755 index 0000000..2403e28 --- /dev/null +++ b/eko.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +int main( int argc, char **argv) +{ +// register ne sert plus de nos jours + register int i, nflg; + nflg = 0; +// Le flag "n" sert à rajouter un saut de ligne + + if ( argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n') + { + nflg++; + argc--; + argv++; + } + for(i = 1 ; i < argc ; i++) + { + fputs (argv[i], stdout); + if ( i < argc - 1 ) + putchar(' '); + } + if ( nflg == 0 ) + putchar('\n'); + + exit(0); +} + diff --git a/eko2.c b/eko2.c new file mode 100755 index 0000000..c98c884 --- /dev/null +++ b/eko2.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +int main( int argc, char **argv) +{ +// register ne sert plus de nos jours + register nflg; + nflg = 0; +// Le flag "n" sert à rajouter un saut de ligne + argv++; + if ( argc > 1 && **argv == '-' && argv[0][1] == 'n') + { + nflg++; + argv++; + } + while(argv[0] != NULL) + { + fputs (*(argv), stdout); + putchar(' '); + argv++; + } + if ( nflg == 0 ) + putchar('\n'); + + exit(0); +} diff --git a/enMajuscule.pl b/enMajuscule.pl new file mode 100755 index 0000000..49bd08b --- /dev/null +++ b/enMajuscule.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl -w +# communication par tube (fonction PIPE qui retourne +# un descripteur pour lire et un 2ième our écrire) +# le fils envoie le résultat de ls -l +# le père récupère le résultat et le met en majuscules + +use IO::Handle; + # chargement du package contenant autoflush() +system("clear"); + +pipe ( ENTREE, SORTIE ); + # création du tube avec les 2 decripteurs +# pipe (d1, d2) : +# d1 : descripteur pour lire ce qui est en sortie du tube +# d2 : descripteur pour écrire (envoyer dans le tube) + +SORTIE->autoflush(1); + # pour rendre disponible immédiatement tout caractère contenu dans SORTIE + +$pid = fork(); +if ($pid == 0) { # proc. fils (PRODUCTEUR) + # fermeture du tube en lecture + close (ENTREE); + + open (COMMANDE, "ls -l |"); + while () + print SORTIE $_; + + close (COMMANDE); + close (SORTIE); +} +else { # proc. père (CONSOMMATEUR) + # fermeture du tube non-utilisé + close (SORTIE); + while () + print uc($_); + + close (ENTREE); + exit(0); +} diff --git a/exemple.c b/exemple.c new file mode 100755 index 0000000..3b31553 --- /dev/null +++ b/exemple.c @@ -0,0 +1,24 @@ +#include +#include +#include + +int multiple(int, int); + +int main( int argc, char **argv) +{ + if(argc < 3) + { perror("Usage : ./truc.out nombre nombre\n"); return 1; } + + int var1 = atoi(argv[1]); + int var2 = atoi(argv[2]); + + int truc = multiple(var1,var2); + + printf("Mon multiple : %d %s \n", truc, "Salut !"); + return 0; +} + +int multiple(int a, int b) +{ + return a * b; +} diff --git a/exoPointeurs.c b/exoPointeurs.c new file mode 100755 index 0000000..467334f --- /dev/null +++ b/exoPointeurs.c @@ -0,0 +1,14 @@ +/* manipulation de pointeurs */ + +#include + +main() +{ + char *c[] = { "ENTER","NEW","POINT","FIRST" }; + char **cp[] = { c+3, c+2, c+1, c }; + char ***cpp = cp; + + printf("\n%s", **++cpp ); + printf("%s ", *--*++cpp + 3 ); + printf("%s\n", *cpp[-2] + 3 ); +} diff --git a/getpwent.pl b/getpwent.pl new file mode 100755 index 0000000..8f3bfbd --- /dev/null +++ b/getpwent.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl -w + +use strict; +my $user; +my @util; +my @human; +my @inhuman; + +setpwent(); + +while(@util=getpwent()) +{ + push @human, $util[0] if ($util[-1] =~ /sh$/); + push @inhuman, $util[0] if ! ($util[-1] =~ /sh$/); +} + +endpwent(); + +print "\U\nThese accounts use human shells: \E\n@human\n"; +print "\U\nThese accounts use human shells: \E\n@inhuman\n"; diff --git a/getpwent2.pl b/getpwent2.pl new file mode 100755 index 0000000..7ce0453 --- /dev/null +++ b/getpwent2.pl @@ -0,0 +1,8 @@ +#!/usr/bin/perl -w + +my $user; +my $util; +my @human; +my @inhuman; + + diff --git a/humanornot.pl b/humanornot.pl new file mode 100755 index 0000000..0c27d26 --- /dev/null +++ b/humanornot.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl -w + +use strict ; +my $user ; +my @util ; +my @human ; +my @inhuman ; + +foreach $user (`getent passwd`) +{ + @util = split(':', $user); + if ( $util[-1] =~ /sh$/ ) + { + push @human, $util[0]; + } + else + { + push @inhuman, $util[0]; + } +} + +$"=', '; + +print "\UThese accounts use human shells: \E\n@human\n"; +print "\UThese accounts use inhuman shells: \E\n@inhuman\n"; + diff --git a/humanornot2.pl b/humanornot2.pl new file mode 100755 index 0000000..c5d944d --- /dev/null +++ b/humanornot2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl -w + +use strict; +my $user; +my @util; +my @human; +my @inhuman; + +open(USERS,"getent passwd |") || die "$!"; + +while() +{ + @util = split(':', $_); + push @human, $util[0] if (/sh$/); + push @inhuman, $util[0] if ! (/sh$/); +} + +close USERS; + +print "\U\nThese accounts use human shells: \E\n@human\n"; +print "\U\nThese accounts use human shells: \E\n@inhuman\n"; diff --git a/lePlusGros.pl b/lePlusGros.pl new file mode 100755 index 0000000..df77289 --- /dev/null +++ b/lePlusGros.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl -w +# leplusGros.pl +# donne le nom du fichier ordinaire localisé ds +# (& directement subordonné) le plus GROS (en taille) +# on se base sur le champ size (pas le nbre de +#blocs alloués !) +use strict; +use File::stat; +# déclaration des variables +my $rep ; +# répertoire en argument +my $size ; +my $max_size = 0 ; +# initialisation du max +my $heavy_file_name ; +# nom du fichier le + lourd +my @files ; # liste des objets du répertoire +my $file ; +my $count_file = 0 ; + +if ( @ARGV != 1) { + print "usage : $0 \n" ; exit (-1) ; +} +$rep = $ARGV[0] ; + +die "repertoire inconnu ou pas en droit de lecture !" unless -d $rep and -r $rep ; + + +# récupérer la liste des objets du répertoire +# (fichiers "cachés" compris) +@files = glob "$rep/* $rep/.*" ; +foreach $file(@files) +{ + my $sb=stat($file); + $size=$sb->size; + print $file," fait ", $size, " octets\n"; + if($size > $max_size) + { + print "C'est lui le plus gros !\n"; + $heavy_file_name=$file; + $max_size=$size; + } + $count_file++; +} +print "Le fichier le plus gros est $heavy_file_name et fait $max_size octets\n"; diff --git a/main.c b/main.c new file mode 100755 index 0000000..a0d82be --- /dev/null +++ b/main.c @@ -0,0 +1,312 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define HOST "10.8.0.10" +#define USER "vpsm" +#define PASS "bla" +#define BASE "vpsm" + +int my_atoi (char * str ) +{ + int res=atoi(str); + free(str); + return res; +} + +char **my_explode(char *str, char separator) +{ + char **res = NULL; + int nbstr = 1; + int len; + int from = 0; + int i; + int j; + + res = (char **) malloc(sizeof (char *)); + len = strlen(str); + for (i = 0; i <= len; ++i) + { + if ((i == len) || (str[i] == separator)) + { + res = (char **) realloc(res, ++nbstr * sizeof (char *)); + res[nbstr - 2] = (char *) malloc((i - from + 1) * sizeof (char)); + for (j = 0; j < (i - from); ++j) + res[nbstr - 2][j] = str[j + from]; + res[nbstr - 2][i - from] = '\0'; + from = i + 1; + ++i; + } + } + res[nbstr - 1] = NULL; + return res; +} +/** + * The va_list method for str() above + **/ + +char *vstr(char *format, va_list args) +{ + char *buffer = NULL; + int va_size = 256; + /* Parse the format and generate a proper string output */ + do + { + va_size *= 2; + buffer = (char*)realloc(buffer, va_size); + memset(buffer, 0, va_size); + } while (vsnprintf(buffer, va_size, format, args) < 0); + + /* Trim any white space at the end */ + char *output = (char*)malloc(strlen(buffer) + 1); + strcpy(output, buffer); + free(buffer); + return output; +} +/** + * Method to produce a malloc()'d char*, similar to printf() + **/ +char *str(char *format, ...) +{ + va_list args; + va_start(args,format); + char *ret = vstr(format, args); + va_end(args); + return ret; +} +// Fonction pour logger un message dans le fichier de logs +void log_message(char * message) +{ + FILE *file; + char infotime[80]; + time_t rawtime; + struct tm * timeinfo; + time ( &rawtime ); + timeinfo = localtime ( &rawtime ); + strftime (infotime,80,"[%d/%m/%y - %X] ",timeinfo); + if((file = fopen("log.txt","a"))) // Ouverture du fichier en append + { + message=str("%s%s\n",infotime,message); + fwrite(message,sizeof(char),strlen(message),file); + } + free(message); + fclose(file); +} +int do_query(char * query) +{ + int res=-1; + MYSQL mysql; + mysql_init(&mysql); + mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"option"); + if(mysql_real_connect(&mysql,HOST,USER,PASS,BASE,0,NULL,0)) + { + if(mysql_query(&mysql,query)) + { + res=1; + } + } + mysql_close(&mysql); + free(query); + return res; +} +void update_status() +{ + + int vmid=0; + int status=0; + FILE *process; + process = popen("{ vzlist && qm list; } | grep -vi 'status' | tr -s ' ' | cut -d ' ' -f 2,4 | sort -gk1 | sed 's/running/1/' | sed 's/stopped/2/'","r"); + char data[50]; + char **line; + int vmids[30]; + int vmstatus[30]; + int i=0; + char* tstatus[3]; + tstatus[0]=""; + tstatus[1]="Actif"; + tstatus[2]="Éteind"; + while(fgets(data,50,process)) + { + line=my_explode(data,' '); + vmid=atoi(line[0]); + status=atoi(line[1]); + vmids[i]=vmid; + vmstatus[i]=status; + //printf("%d is %d \n",vmid,status); + i++; + free(line[0]); + free(line[1]); + free(line); + } + fclose(process); + int length=i; + MYSQL mysql; + mysql_init(&mysql); + mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"option"); + mysql_real_connect(&mysql,HOST,USER,PASS,BASE,0,NULL,0); + mysql_query(&mysql,"SELECT vmid,status FROM vps ORDER BY vmid"); + MYSQL_RES *result=NULL; + MYSQL_ROW row; + result=mysql_use_result(&mysql); + i=0; + //printf("test database : \n"); + while((i/dev/stdout",command),"r"); + char data[100]; + char **line; + while(fgets(data,100,process)) + { + line=my_explode(data,'\n'); + log_message(str("%s: %s",command, line[0])); + free(line[0]); + free(line); + } + int res=pclose(process); + return res; +} +void exec_task(char * command,int id) +{ + do_query(str("UPDATE tasks SET status=3 WHERE id=%d",id)); + log_message(str("[Tâche %d] Exécution de : %s",id,command)); + int res=exec_process(command); + if(res) // Si commande échouée + { + log_message(str("[Tâche %d] Une Erreur est survenue",id)); + do_query(str("UPDATE tasks SET status=2 WHERE id=%d",id)); + } + else + { + //exec_process("php update.php"); + update_status(); + log_message(str("[Tâche %d] Exécution Terminée",id)); + do_query(str("UPDATE tasks SET status=1 WHERE id=%d",id)); + } + free(command); + +} + +int continuer=1; + +void sighandler(int signum) +{ + continuer=0; + log_message(str("Signal %d attrapé, Fermeture...",signum)); + exit(0); +} + +int main() +{ + + struct sigaction act; + + act.sa_handler = sighandler; + sigfillset(&act.sa_mask); + act.sa_flags = 0; + sigaction(15, &act, NULL); + sigaction(11, &act, NULL); + + char* vctype[3]; // Type du contrôlleur de VPS + vctype[0]=""; // Rien + vctype[1]="vzctl"; // OpenVZ + vctype[2]="qm"; // KVM + char* ttype[4]; // Type de la commande (tâche) + ttype[0]=""; //Rien + ttype[1]="start"; // Démarrage de la VM + ttype[2]="stop"; // Éteindre la VM + ttype[3]="shutdown"; // Éteindre la VM par ACPI + MYSQL mysql; + mysql_init(&mysql); + mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"option"); + int id=0; + int vid=0; + int type=0; + int status=0; + int vtype=0; + unsigned long *lengths; + if(mysql_real_connect(&mysql,HOST,USER,PASS,BASE,0,NULL,0)) + { + log_message("Connexion réussie"); + // Création du daemon + int p=fork(); + if(p!=0) // Si c'est le parent + { + // On détache... + printf("VPSMd: Démarrage et détachement de la console...\n"); + exit(0); + } + else + { + // On commence le daemon + log_message("Daemon démarré"); + while(continuer) + { + // exec_process("php update.php"); + update_status(); + // Rercherche des tâches à effectuer (status 0 : à traiter) + mysql_query(&mysql,"SELECT t.id,t.vid,t.type,t.status,v.type AS vtype FROM tasks t JOIN vps v ON t.vid=v.vmid WHERE t.status=0"); + MYSQL_RES *result=NULL; + MYSQL_ROW row; + + result=mysql_use_result(&mysql); + while((row=mysql_fetch_row(result))) + { + lengths=mysql_fetch_lengths(result); + id=my_atoi(str("%.*s", (int) lengths[0], row[0])); + vid=my_atoi(str("%.*s", (int) lengths[1], row[1])); + type=my_atoi(str("%.*s", (int) lengths[2], row[2])); + status=my_atoi(str("%.*s", (int) lengths[3], row[3])); + vtype=my_atoi(str("%.*s", (int) lengths[4], row[4])); + log_message(str("Tâche numéro %d pour le VPS numero %d, de type %d, statut %d",id,vid,type,status)); + exec_task(str("%s %s %d",vctype[vtype],ttype[type],vid),id); + } + mysql_free_result(result); + sleep(3); + } + } + mysql_close(&mysql); + log_message("Connexion fermée"); + log_message("Fermeture du daemon"); + } + else + { + log_message("Erreur de connexion ! Fermeture du daemon"); + } + return 0; +} diff --git a/matrice1.c b/matrice1.c new file mode 100755 index 0000000..29d156e --- /dev/null +++ b/matrice1.c @@ -0,0 +1,56 @@ +/* programme matrice1.c */ +/* tableau de dimension 2 version STATIQUE */ + +#include +#include +#define NB_LIGNES_MAX 10 +#define NB_COLS_MAX 10 + +void lire_mat( int mat[NB_LIGNES_MAX][NB_COLS_MAX], int nbl, int nbc ) +{ + // nbl, nbc: resp. nb de lignes et colonnes effectif + int i, j; + for ( i = 0 ; i < nbl ; i++ ) { + for ( j = 0 ; j < nbc ; j++ ) { + printf( "[%d][%d] : ", i, j ); + scanf( "%d", &mat[i][j] ); + } + } +} + +void affich_mat( int mat[NB_LIGNES_MAX][NB_COLS_MAX], int nbl, int nbc ) +{ + // nbl, nbc: resp. nb de lignes et colonnes effectif + int i, j; + printf( " " ); + for ( i = 0 ; i < nbc ; i++ ) + printf( " %4d", i ); + printf( "\n " ); + for ( i = 0 ; i < nbc ; i++ ) + printf( "-----" ); + printf( "\n" ); + + for ( i = 0 ; i < nbl ; i++ ) { + printf( " %2d |", i ); + for ( j = 0 ; j < nbc ; j++ ) { + printf( " %4d", mat[i][j] ); + } + printf( "\n" ); + } + printf( "\n" ); +} + +main() +{ int t[NB_LIGNES_MAX][NB_COLS_MAX]; + int n,m ; + printf("Nb de lignes ? "); scanf("%d", &n); + printf("Nb de colonnes ? "); scanf("%d", &m); + if ( n > NB_LIGNES_MAX || m > NB_COLS_MAX) { + puts("erreur, nombre depassant la taille tableau"); + exit(1); + } + lire_mat(t,n,m); + affich_mat(t, n,m) ; + +} + diff --git a/matrice2.c b/matrice2.c new file mode 100755 index 0000000..323f75b --- /dev/null +++ b/matrice2.c @@ -0,0 +1,66 @@ +#include +#include + +int** lire_mat( int nbl, int nbc ) { + + int i,j; + int **t; + t = (int **) malloc(nbl * sizeof(int*)); + for(i=0;i0) + free(mat[i]) ; + if (nbl >0) + free(mat) ; +} + +main() +{ + int **t ; + int n,m ; + printf("Nb de lignes ? "); scanf("%d", &n); + printf("Nb de colonnes ? "); scanf("%d", &m); + + t = lire_mat(n,m); + + affich_mat(t, n,m) ; + + liberer(t,n,m); + // affich_mat(t, n,m) ; + return 0; +} + + diff --git a/max.pl b/max.pl new file mode 100755 index 0000000..04a33d6 --- /dev/null +++ b/max.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl -w + +sub max{ +my $max_actuel = shift @_ ; + +foreach my $e (@_) +{ + if ($e > $max_actuel) + { + $max_actuel = $e; + } +} +return $max_actuel; + +} + diff --git a/maxcourt.pl b/maxcourt.pl new file mode 100755 index 0000000..e07a537 --- /dev/null +++ b/maxcourt.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl -w + +sub max{ + my $max_actuel = shift @_; + + foreach (@_) + { + if ($_ > $max_actuel) + { + $max_actuel = $_; + } + } +$max_actuel; +} + diff --git a/minmax.c b/minmax.c new file mode 100755 index 0000000..786d617 --- /dev/null +++ b/minmax.c @@ -0,0 +1,50 @@ +#include +#include +#include + +typedef struct +{ + char * min; + char * max; +} MINMAX; + +void chaineminmax ( char **t, MINMAX *m); + +int main(int argc, char **argv) +{ + argc--; + argv++; + MINMAX m; + char **t = malloc((argc+1) * sizeof(char *)); + int i = 0; + while(i < argc) + { + t[i] = argv[i]; + i++; + } + t[i] = NULL; + + chaineminmax(t,&m); + printf("Il y a désormais dans \"m\", min = %s et max = %s\n",m.min,m.max); +} + +void chaineminmax ( char **t, MINMAX *m) +{ + char *mmin = *t; + char *mmax = *t; + t++; + while(*t) + { + if(strcmp(*t,mmax) > 0) + mmax = *t; + if(strcmp(*t,mmin) < 0) + mmin = *t; + t++; + } + m->min = malloc(strlen(mmin+5) * sizeof(char)); + m->max = malloc(strlen(mmax+5) * sizeof(char)); + strcpy(m->min,mmin); + strcpy(m->max,mmax); + strcat(m->min, ".min\0"); + strcat(m->max, ".max\0"); +} diff --git a/monswap.c b/monswap.c new file mode 100755 index 0000000..49e4c01 --- /dev/null +++ b/monswap.c @@ -0,0 +1,20 @@ +#include +#include +#include +void swap(char**,char**); + +int main( int argc, char **argv) +{ + char * a = "Coucou"; + char * b = "Pas Coucou"; + printf("a : %s || b : %s\n",a,b); + swap(&a,&b); + printf("a : %s || b : %s\n",a,b); + +} +void swap(char **a, char **b) +{ + char * temp = *a; + *a = *b; + *b = temp; +} diff --git a/morceau_miroir.c b/morceau_miroir.c new file mode 100755 index 0000000..b5da557 --- /dev/null +++ b/morceau_miroir.c @@ -0,0 +1,49 @@ +#include + +int lirech1( char* s, int n ) +{ + int i, c; + + i = 0; + + while ( i < n && ( c = getchar() ) != '\n' ) + s[i++] = c; + + s[i] = '\0'; + + if ( c != '\n' ) + while ( getchar() != '\n' ); + + return i; +} + +void miroir( char *s, char *t) +{ + +/* A VOUS ! +regardez la version miroir2 MAIS ... +obligation d'employer les notations POINTEURS (t++,s++ ou s--, *t, *s) + + +*/ +} + +/* miroir, avec notation "classique" t[i] +void miroir2( char *s, char *t) +{ + int l,i; + l= strlen(t); + for (i=0; i) +{ + @tab_mots = split (/\W+/); + $nbmots += $#tab_mots + 1 ; # $nbmots += @tab_mots +} + +print "nb de mots : $nbmots \n"; diff --git a/silent_cmp.pl b/silent_cmp.pl new file mode 100755 index 0000000..5877eff --- /dev/null +++ b/silent_cmp.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl -w +use strict; + +exit(1) unless @ARGV==2; + +my $f1 = shift @ARGV; +my $f2 = shift; +my $buffer1; +my $buffer2; +my $SIZE = 8; +my $lec1; +my $lec2; + +exit(2) unless -f $f1 and -r $f1 and -f $f2 and -r $f2; + +open(f1,"<$f1") || exit(2); +open(f2,"<$f2") || exit(2); + +do +{ + $lec1 = sysread(f1,$buffer1,$SIZE); + $lec2 = sysread(f2,$buffer2,$SIZE); + + exit(1) unless $buffer1 eq $buffer2; +}while( $lec1 == $lec2 and $lec1 > 0 ); +exit(1) unless $lec1 == $lec2; + +close f1; +close f2; diff --git a/squeeze.c b/squeeze.c new file mode 100755 index 0000000..668f5a2 --- /dev/null +++ b/squeeze.c @@ -0,0 +1,37 @@ +#include +#include +#include + +void squeeze( char *s, char *t, char c ); + +int main(int argc, char **argv) +{ + char s[10]; + char t[10]; + scanf("%s",s); + squeeze(s,t,'a'); + printf("%s\n",t); +} + +void squeeze( char *s , char *t, char c) +{ + int i = 0; + int k = 0; + while( s[i] != '\n' && s[i] != '\0' ) + { + if (s[i] != c && k < strlen(t)) + { + t[k] = s[i]; + k++; + } + i++; + } + t[k]='\0'; +} + +void miroir ( char *s, char *t) +{ + while( *s != '\0') + { + } +} diff --git a/strdup2.c b/strdup2.c new file mode 100755 index 0000000..b9a9f17 --- /dev/null +++ b/strdup2.c @@ -0,0 +1,52 @@ +// strdupAvecBug !! + +#include +#include +#include + +char *strdup2(char *) ; // prototype + +main() +{ + char *t1 = "hello how are you ?" ; + char t2[20] ; + char *r ; + + strcpy(t2, "TORTUE") ; + + r = (char *) strdup(t1) ; // appel à la fonction existante + printf( "%s\n ", r ); + printf( "%d\n ", strlen(r) ); + + r = ( char *) strdup(t2) ; + printf( "%s\n ", r ); + printf( "%d\n ", strlen(r) ); + + // ========================= + // and now the same thing with personal function strdup2 !! + + printf( "%s\n ", "Resultats avec STRDUP2" ); + r = strdup2(t1) ; + printf( "%s\n ", r ); + printf( "%d\n ", strlen(r) ); + + r = strdup2(t2) ; + printf( "%s\n ", r ); + printf( "%d\n ", strlen(r) ); + +} + +char *strdup2(char *s) +{ + char *r ; + int n = strlen(s); + r = (char* )malloc( (n + 1) * sizeof(char) ); + while(*s) + { + *r = *s ; + r++; + s++; + } + *r='\0'; + return r-n; +} diff --git a/swapp.c b/swapp.c new file mode 100755 index 0000000..b0ad92c --- /dev/null +++ b/swapp.c @@ -0,0 +1,20 @@ +#include + +void swapp( char **p1, char **p2) +{ +/bin/bash: q : commande introuvable + *p1 = *p2; + *p2 = w; + +} + +main() +{ +char *a="vacances"; +char *b="vendredi" ; + +printf( "\na pointe sur %s b pointe sur %s", a,b ); +swapp(&a,&b) ; +printf( "\napres echange: a pointe sur %s b pointe sur %s\n", a,b ); + +} diff --git a/testargs.c b/testargs.c new file mode 100755 index 0000000..562ddf8 --- /dev/null +++ b/testargs.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +int main( int argc, char **argv, char ** envp) +{ + while(*envp != NULL) + { + if(strstr(*envp, "GDMSESSION") != NULL) + printf("%s\n", *envp); + *envp++; + } +} + diff --git a/tout.c b/tout.c new file mode 100755 index 0000000..85936b7 --- /dev/null +++ b/tout.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +void miroir ( char *s, char *t); // on donne s et t sera le miroir +void squeeze( char *s , char *t, char c); // on donne s et t sera la chaine sans le caractère c +int lirechl(char *s, int n); // lire un nombre de caractères donné par n +int palindrome(char *); // 1 si vrai 0 sinon +void minuscule(char *); // met en minuscule la chaîne +int main(int argc, char **argv) +{ + if ( argc != 2 ) + { + printf("Usage: %s sentence \n", *argv); + exit(2); + } + char *s = argv[1]; + char t[strlen(s)]; + miroir(s,t); + printf("%s :: %s\n",s,t); + + int rep = palindrome(s); + if(rep==1) + printf("Cette phrase est un palindrome\n"); + else + printf("Cette phrase n'est pas un palindrome\n"); +} +void miroir ( char *s, char *t) +{ + int i=0, taille = strlen(s); + t[taille] = '\0'; + taille--; + while((taille - i) >= 0) + { + t[taille-i]=s[i]; + i++; + } +} +int palindrome(char *s) +{ + int i=0,res = 1; + char t[strlen(s)]; + squeeze(s,t,' '); + minuscule(t); + while(i) +{ + print ; + $nb++; +} +close(LS); +$nb--; # à cause de la première ligne qui indique le total +print "nb d'objets : $nb\n"; diff --git a/wc.pl b/wc.pl new file mode 100755 index 0000000..0fea423 --- /dev/null +++ b/wc.pl @@ -0,0 +1,15 @@ +#!/usr/bin/perl -w + +use strict; + +die "un argument svp ! " if !defined ($ARGV[0]); + +my $i=0; +open(P,"< $ARGV[0]") || die "erreur ouverture $!"; + +while (

) +{ + $i++; +} +close(P); +print "nb de lignes = $i \n"; diff --git a/wordcount.c b/wordcount.c new file mode 100755 index 0000000..bff3162 --- /dev/null +++ b/wordcount.c @@ -0,0 +1,40 @@ +#include + +int lirechl(char *s, int n); + +int lirechl(char *s, int n) +{ + int i, c; + i=0; + while( i < n && ( c = getchar() ) != '\n' ) + s[i++] = c ; + + s[i] = '\0'; + + if ( c != '\n' ) + while ( getchar() != '\n' ); + + return i; +} + +int main() +{ + char string[10]; + int bla = lirechl(&string[0], 7); + printf("Nombre de caracteres jusqu'a saut de ligne : %d \n",bla); +} +void squeeze( char *s , char *t, char c) +{ + int i = 0; + int k = 0; + while( s[i] != '\n' && s[i] != '\0' ) + { + if (s[i] != c && k < strlen(t)) + { + t[k] = s[i]; + k++; + } + i++; + } + t[k]='\0'; +}