files added
This commit is contained in:
parent
23f58eac40
commit
f6254d0cf9
26
chrono.pl
Executable file
26
chrono.pl
Executable file
@ -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){}
|
17
daemon.pl
Executable file
17
daemon.pl
Executable file
@ -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;
|
||||
}
|
30
eko.c
Executable file
30
eko.c
Executable file
@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
28
eko2.c
Executable file
28
eko2.c
Executable file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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);
|
||||
}
|
40
enMajuscule.pl
Executable file
40
enMajuscule.pl
Executable file
@ -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 (<COMMANDE>)
|
||||
print SORTIE $_;
|
||||
|
||||
close (COMMANDE);
|
||||
close (SORTIE);
|
||||
}
|
||||
else { # proc. père (CONSOMMATEUR)
|
||||
# fermeture du tube non-utilisé
|
||||
close (SORTIE);
|
||||
while (<ENTREE>)
|
||||
print uc($_);
|
||||
|
||||
close (ENTREE);
|
||||
exit(0);
|
||||
}
|
24
exemple.c
Executable file
24
exemple.c
Executable file
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
14
exoPointeurs.c
Executable file
14
exoPointeurs.c
Executable file
@ -0,0 +1,14 @@
|
||||
/* manipulation de pointeurs */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
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 );
|
||||
}
|
20
getpwent.pl
Executable file
20
getpwent.pl
Executable file
@ -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";
|
8
getpwent2.pl
Executable file
8
getpwent2.pl
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
my $user;
|
||||
my $util;
|
||||
my @human;
|
||||
my @inhuman;
|
||||
|
||||
|
26
humanornot.pl
Executable file
26
humanornot.pl
Executable file
@ -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";
|
||||
|
21
humanornot2.pl
Executable file
21
humanornot2.pl
Executable file
@ -0,0 +1,21 @@
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
use strict;
|
||||
my $user;
|
||||
my @util;
|
||||
my @human;
|
||||
my @inhuman;
|
||||
|
||||
open(USERS,"getent passwd |") || die "$!";
|
||||
|
||||
while(<USERS>)
|
||||
{
|
||||
@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";
|
45
lePlusGros.pl
Executable file
45
lePlusGros.pl
Executable file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/perl -w
|
||||
# leplusGros.pl <rep>
|
||||
# donne le nom du fichier ordinaire localisé ds <rep>
|
||||
# (& 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 <repertoire >\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";
|
312
main.c
Executable file
312
main.c
Executable file
@ -0,0 +1,312 @@
|
||||
#include <stdio.h>
|
||||
#include <mysql/mysql.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#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<length) && (row=mysql_fetch_row(result)))
|
||||
{
|
||||
unsigned long *lengths=mysql_fetch_lengths(result);
|
||||
vmid=my_atoi(str("%.*s", (int) lengths[0], row[0]));
|
||||
status=my_atoi(str("%.*s", (int) lengths[1], row[1]));
|
||||
//printf("i = %d, vmid=%d, status=%d, dvmid=%d, vstatus=%d\n",i,vmid,status,vmids[i],vmstatus[i]);
|
||||
while(i<length && vmids[i]<vmid)
|
||||
{
|
||||
//log_message(str("VPS %d non trouvé dans la base, passage au VPS %d",vmids[i],vmids[i+1]));
|
||||
i++;
|
||||
}
|
||||
if(vmids[i]==vmid)
|
||||
{
|
||||
if(vmstatus[i]!=status)
|
||||
{
|
||||
log_message(str("Changement d'état du VPS %d à %s",vmid,tstatus[vmstatus[i]]));
|
||||
do_query(str("UPDATE vps SET status=%d WHERE vmid=%d",vmstatus[i],vmid));
|
||||
//printf("%d status updated to %d",vmid,vmstatus[i]);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
else if (status==1)
|
||||
{
|
||||
log_message(str("Statut du VPS %d non récupéré, passage a État %s",vmid,tstatus[2]));
|
||||
do_query(str("UPDATE vps SET status=2 WHERE vmid=%d",vmid));
|
||||
//printf("vps %d not found, setting to 2",vmid);
|
||||
}
|
||||
|
||||
}
|
||||
mysql_free_result(result);
|
||||
mysql_close(&mysql);
|
||||
}
|
||||
int exec_process(char * command)
|
||||
{
|
||||
FILE * process = popen(str("%s &>/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;
|
||||
}
|
56
matrice1.c
Executable file
56
matrice1.c
Executable file
@ -0,0 +1,56 @@
|
||||
/* programme matrice1.c */
|
||||
/* tableau de dimension 2 version STATIQUE */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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) ;
|
||||
|
||||
}
|
||||
|
66
matrice2.c
Executable file
66
matrice2.c
Executable file
@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int** lire_mat( int nbl, int nbc ) {
|
||||
|
||||
int i,j;
|
||||
int **t;
|
||||
t = (int **) malloc(nbl * sizeof(int*));
|
||||
for(i=0;i<nbl;i++)
|
||||
t[i] = malloc(nbc * sizeof(int));
|
||||
for(i=0;i<nbl;i++){
|
||||
for(j=0;j<nbc;j++){
|
||||
printf("Ligne %2d Colonne %2d : ",i,j);
|
||||
scanf("%d", &t[i][j]);
|
||||
}
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
void affich_mat( int** mat, int nbl, int nbc ) {
|
||||
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" );
|
||||
}
|
||||
|
||||
void liberer(int** mat, int nbl, int nbc) {
|
||||
int i;
|
||||
for ( i = 0 ; i < nbl ; i++ )
|
||||
if (nbc >0)
|
||||
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;
|
||||
}
|
||||
|
||||
|
16
max.pl
Executable file
16
max.pl
Executable file
@ -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;
|
||||
|
||||
}
|
||||
|
15
maxcourt.pl
Executable file
15
maxcourt.pl
Executable file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
sub max{
|
||||
my $max_actuel = shift @_;
|
||||
|
||||
foreach (@_)
|
||||
{
|
||||
if ($_ > $max_actuel)
|
||||
{
|
||||
$max_actuel = $_;
|
||||
}
|
||||
}
|
||||
$max_actuel;
|
||||
}
|
||||
|
50
minmax.c
Executable file
50
minmax.c
Executable file
@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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");
|
||||
}
|
20
monswap.c
Executable file
20
monswap.c
Executable file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
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;
|
||||
}
|
49
morceau_miroir.c
Executable file
49
morceau_miroir.c
Executable file
@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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<l; i++)
|
||||
s[i] = t[l-i-1] ;
|
||||
s[l] = '\0' ;
|
||||
}
|
||||
*/
|
||||
|
||||
main()
|
||||
{
|
||||
char a[21], b[21];
|
||||
lirech1( a, 20 );
|
||||
printf( "\nchaine originale : %s\n", a );
|
||||
miroir( b, a );
|
||||
printf( "\nchaine miroir : %s\n", b );
|
||||
}
|
36
multi_chrono.pl
Executable file
36
multi_chrono.pl
Executable file
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/perl -w
|
||||
use strict;
|
||||
|
||||
my @fils;
|
||||
print "Mon PID :". $$ ."\n ";
|
||||
|
||||
sub nouveau
|
||||
{
|
||||
my $pid = fork();
|
||||
if($pid==0)
|
||||
{
|
||||
exec("perl chrono.pl");
|
||||
}
|
||||
else
|
||||
{
|
||||
push(@fils,$pid);
|
||||
print "nouveau chrono : $pid \n";
|
||||
print "liste des fils : @fils \n";
|
||||
}
|
||||
}
|
||||
sub demandeTemps
|
||||
{
|
||||
print "On transmet SIGINT à tous les chronos";
|
||||
kill 2, @fils;
|
||||
}
|
||||
sub arret
|
||||
{
|
||||
print "On transmet SIGQUIT à tous les chronos";
|
||||
kill 3, @fils;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$SIG{"TERM"} = "nouveau";
|
||||
$SIG{"INT"} = "demandeTemps"; # INT = kill 2 = CTRL C
|
||||
$SIG{"QUIT"} = "arret"; # QUIT = kill 3
|
||||
while(1){}
|
11
nbmots.pl
Executable file
11
nbmots.pl
Executable file
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
$nbmots = 0 ;
|
||||
|
||||
while (<>)
|
||||
{
|
||||
@tab_mots = split (/\W+/);
|
||||
$nbmots += $#tab_mots + 1 ; # $nbmots += @tab_mots
|
||||
}
|
||||
|
||||
print "nb de mots : $nbmots \n";
|
29
silent_cmp.pl
Executable file
29
silent_cmp.pl
Executable file
@ -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;
|
37
squeeze.c
Executable file
37
squeeze.c
Executable file
@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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')
|
||||
{
|
||||
}
|
||||
}
|
52
strdup2.c
Executable file
52
strdup2.c
Executable file
@ -0,0 +1,52 @@
|
||||
// strdupAvecBug !!
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
|
||||
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;
|
||||
}
|
20
swapp.c
Executable file
20
swapp.c
Executable file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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 );
|
||||
|
||||
}
|
15
testargs.c
Executable file
15
testargs.c
Executable file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
|
||||
int main( int argc, char **argv, char ** envp)
|
||||
{
|
||||
while(*envp != NULL)
|
||||
{
|
||||
if(strstr(*envp, "GDMSESSION") != NULL)
|
||||
printf("%s\n", *envp);
|
||||
*envp++;
|
||||
}
|
||||
}
|
||||
|
85
tout.c
Executable file
85
tout.c
Executable file
@ -0,0 +1,85 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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<strlen(t))
|
||||
{
|
||||
if(t[i]!=t[strlen(t)-i-1])
|
||||
res=0;
|
||||
i++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
void squeeze( char *s , char *t, char c)
|
||||
{
|
||||
while(*s)
|
||||
{
|
||||
if( *s != c)
|
||||
{
|
||||
*t = *s;
|
||||
t++;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
*t = '\0';
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
void minuscule(char * s)
|
||||
{
|
||||
while(*s++ && (*s = tolower(*s)));
|
||||
}
|
14
tube.pl
Executable file
14
tube.pl
Executable file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
use strict;
|
||||
my $nb=0;
|
||||
open(LS,"ls -l |") || die "$!";
|
||||
|
||||
while (<LS>)
|
||||
{
|
||||
print ;
|
||||
$nb++;
|
||||
}
|
||||
close(LS);
|
||||
$nb--; # à cause de la première ligne qui indique le total
|
||||
print "nb d'objets : $nb\n";
|
15
wc.pl
Executable file
15
wc.pl
Executable file
@ -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 (<P>)
|
||||
{
|
||||
$i++;
|
||||
}
|
||||
close(P);
|
||||
print "nb de lignes = $i \n";
|
40
wordcount.c
Executable file
40
wordcount.c
Executable file
@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
|
||||
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';
|
||||
}
|
Loading…
Reference in New Issue
Block a user