1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| /* ----------------------------------------------------------------------------
* TS 3GI1
* ----------------------------------------------------------------------------
* Fonction : Operations sur les fichiers (affichage en majuscules ou en
* minuscules) d'un fichier.
* ----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define STDOUT 1
/* ----------------------------------------------------------------------------
* my_tolower ()
* ----------------------------------------------------------------------------
* Role : Retourne un caractere en minuscule
* ----------------------------------------------------------------------------
*/
int my_tolower (int c)
{
return tolower (c);
}
/* ----------------------------------------------------------------------------
* my_toupper ()
* ----------------------------------------------------------------------------
* Role : Retourne un caractere en majucule
* ----------------------------------------------------------------------------
*/
int my_toupper (int c)
{
return toupper (c);
}
/* ----------------------------------------------------------------------------
* printf_file ()
* ----------------------------------------------------------------------------
* Role : affiche un fichier (soit en minuscule, soit en majuscule)
* ----------------------------------------------------------------------------
*/
void printf_file (char *pathname, int (*fonction) (int))
{
int file_descriptor;
char tmp;
file_descriptor = open (pathname, O_RDONLY, NULL);
if (file_descriptor == -1)
{
printf ("No such file or directory\n");
}
else
{
while (read (file_descriptor, &tmp, sizeof (char)))
{
tmp = fonction (tmp);
write (STDOUT, &tmp, sizeof (char));
}
close (file_descriptor);
}
}
int main (int argc, char **argv)
{
switch (argc)
{
case 1:
case 2:
printf ("Not enough arguments\n");
break;
case 3:
if (strcmp (argv[1], "-m") == 0)
{
printf_file (argv[2], &my_tolower);
}
else if (strcmp (argv[1], "-M") == 0)
{
printf_file (argv[2], &my_toupper);
}
else
{
printf ("Invalide argument\n");
}
break;
default:
printf ("Too much arguments\n");
}
return EXIT_SUCCESS;
} |
Partager