Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Thème 5 - système: Gestion de fichiers
- ## Exo 5.1
- `fopen` n'est pas une primitive systeme
- les fonctons de bibl passent par primitives syst. ex fopen apelle open.
- leur difference est que elles apportent des differentes nouvelles fonctionalités. Peuvent etre utile en terme de perf;
- ## Exo 5.2
- ```c++
- int faux (char *nom){
- FILE *fp ;
- int c ;
- fp = open (nom, "r") ;
- read (fp, &c, 1) ;
- fclose (fp) ;
- return c;
- }
- ```
- - FILE\* est un pointeur de structure `FILE` or open renvoi un `int`.
- - `open` n'a pas "r" comme argument.
- ## Exo 5.3/5.4
- ```cpp
- void myCP(char * src, char * dest){
- int fd_src, fd_dest;
- //open files
- if((fd_src = open(src,O_RDONLY)) == -1){
- perror("Error: oped fd_src");
- exit(EXIT_FAILURE);
- }
- if((fd_dest = open(dest,O_WRONLY | O_CREAT | O_TRUNC,0666)) == -1){
- perror("Error: fd_src open");
- close(fd_src);
- exit(EXIT_FAILURE);
- }
- //copy
- /**
- * @details size of bytes which will be copied | any impact on algo
- */
- uint size = 0;
- char buffer[BUF_SIZE];
- int bytes = BUF_SIZE;
- while ((bytes = read(fd_src,&buffer,bytes)) > 0) {
- size += bytes;
- int written;
- char* pbuffer = buffer;
- while ((written = write(fd_dest,pbuffer,bytes)) != bytes){
- bytes -= written;
- pbuffer += written;
- }
- }
- if(bytes == 0){
- printf("Copying ended successfully, copied [%d] of bytes", size);
- return;
- }
- if(bytes == -1){
- perror("Error: on copy > read");
- exit(EXIT_FAILURE);
- }
- return;
- }
- ```
- ## Exo 5.5
- ```cpp
- void mygetchar(){
- char buffer;
- int bytes = 1;
- while ((bytes = read(STDIN_FILENO,&buffer,bytes)) > 0){
- int written = 0;
- char* pbuffer = buffer;
- while ((written = write(STDOUT_FILENO,&pbuffer,bytes)) != bytes){
- bytes -= written;
- pbuffer += written;
- }
- }
- }
- char mygetchar(){
- char buffer;
- int readed = 0;
- if((readed = read(STDIN_FILENO,&buffer,1)) >= 0){
- // dprintf(STDOUT_FILENO,"[%d : %c]\t",readed,buffer);
- switch (readed) {
- case 0:
- return EOF;
- break;
- default:
- return buffer;
- break;
- }
- }
- perror("Error on read");
- exit(EXIT_FAILURE);
- }
- int main() {
- char c;
- while((c = mygetchar()) != EOF ){
- write(STDIN_FILENO,&c,1);
- }
- return 0;
- }
- ```
- ## Exo 5.6
- ```c++
- char my_getChar(){
- struct buffer{
- char c_buf[1024];
- int buffer_size;
- int cur_pos;
- };
- static struct buffer buffer = {0};
- char result = '\n';
- int read_bytes;
- if(buffer.buffer_size == 0){
- if((read_bytes = read(STDIN_FILENO,&buffer.c_buf,1024 )) > 0){
- buffer.buffer_size = read_bytes;
- buffer.cur_pos = 0;
- std::cout << "Buffer_size : " << buffer.buffer_size << " Curr_pos : " << buffer.cur_pos << '\n';
- }
- }
- if(buffer.buffer_size > 0){
- result = buffer.c_buf[buffer.cur_pos];
- buffer.buffer_size--;
- buffer.cur_pos = (buffer.cur_pos+1) % 1024;
- return result;
- }
- return result;
- }
- ```
- ## Exo 5.8
- **Get group & user name**
- ```c++
- #include<pwd.h>
- #include<grp.h>
- #include<sys/stat.h>
- struct stat info;
- stat(filename,&info);
- struct passwd *pw = getpwuid(info.st_uid);
- struct group *gr = getgrgid(info.st_gid);
- // if pw != 0, pw->pw_name contains the user name
- // if gr != 0, gr->gr_name contains the group name
- ```
- **st_mtime to string**
- ```c++
- time_t t = mystat.st_mtime;
- struct tm lt;
- localtime_r(&t, <);
- char timbuf[80];
- strftime(timbuf, sizeof(timbuf), "%c", <);
- // #################################################
- struct tm *tm;
- char buf[200];
- /* convert time_t to broken-down time representation */
- tm = localtime(&t);
- /* format time days.month.year hour:minute:seconds */
- strftime(buf, sizeof(buf), "%d.%m.%Y %H:%M:%S", tm);
- printf("%s\n", buf);
- ```
- ```c++
- void printLine(struct data *data) {
- struct group *g = getgrgid(data->gid);;
- struct passwd *passwd = getpwuid(data->uid);
- struct tm lt;
- localtime_r(&data->mtime,<);
- char timebuf[80];
- strftime(timebuf,sizeof(timebuf),"%c",<);
- printf("%c", data->ftype);
- for (int i = 0; i < 9; ++i) {
- printf("%c", data->accessmode[i]);
- }
- printf(" %d %s %s", data->links,(passwd)?passwd->pw_name: NULL,(g)?g->gr_name:NULL);
- printf(" %5d", data->size);
- printf(" %s",timebuf);
- printf(" %20s", data->name);
- printf("\n");
- }
- void my_ls() {
- DIR *dir;
- struct dirent *dent;
- struct stat stbuf;
- dir = opendir(".");
- if (!dir) {
- perror("Error: opendir");
- exit(EXIT_FAILURE);
- }
- struct data data = {0};
- while ((dent = readdir(dir)) != NULL) {
- // remove . & ..
- if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
- continue;
- }
- if (stat(dent->d_name, &stbuf)) {
- closedir(dir);
- perror("Error: Stat");
- }
- //file type
- data.ftype = "-d"[S_ISDIR(stbuf.st_mode)];
- //file perms
- data.uid = stbuf.st_uid;
- data.gid = stbuf.st_gid;
- data.links = stbuf.st_nlink;
- data.size = stbuf.st_size;
- {
- int perms[] = {S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IROTH};
- for (int i = 0; i < 9; ++i) {
- int index = i % 3;
- switch (index) {
- case 0:
- data.accessmode[i] = "-r"[(stbuf.st_mode & perms[i]) == perms[i]];
- break;
- case 1:
- data.accessmode[i] = "-w"[(stbuf.st_mode & perms[i]) == perms[i]];
- break;
- case 2:
- data.accessmode[i] = "-x"[(stbuf.st_mode & perms[i]) == perms[i]];
- break;
- default:
- data.accessmode[i] = '-';
- break;
- }
- }
- }
- data.mtime = stbuf.st_mtime;
- //file name
- data.name = dent->d_name;
- printLine(&data);
- // printf(":> %c %d %20s\n","df"[data.ftype],data.links,dent->d_name);
- }
- closedir(dir);
- }
- ```
- ## Exo 5.9
- ```c++
- /*Exercice 5.9O
- * On désire implémenter une nouvelle version de la librairie standard d’entrées/sorties à l’aide des primitivessystèmes
- * .1. Donnez une définition du type FICHIER. N’oubliez de prévoir la bufferisation des entrées/sorties
- * .2. Programmez la fonctionmy_open, analogue àfopen. Pour simplifier, on ne considérera que les modesd’ouverture"r"et"w"
- * .3. Reprenez l’exercice 5.6 pour programmermy_getc, analogue à getc
- * .4. Programmez la fonctionmy_putc, analogue àputc(qui bufferise en sortie de la même manière quegetcbufferise en entrée)
- * .5. Programmez la fonctionmy_close, analogue àfclose
- * .6. Pour tester, écrivez une fonctionmainqui ouvre deux fichierstotoettataen lecture, puis qui lit enboucle 1 caractère dans chacun des deux fichiers et les affiche sur la sortie standard. Votre boucle s’ar-rêtera dès que vous rencontrez la première fin de fichier.
- */
- typedef struct Fichier {
- int fd;
- int buff_size;
- int cur_buffPos;
- char buff[BUFF_SIZE];
- char (*my_getchar)(Fichier *fichier);
- void (*my_putchar)(char ch,Fichier *fichier);
- } FICHIER;
- void my_close(FICHIER *file) {
- close(file->fd);
- free(file);
- }
- char my_getChar(FICHIER *file) {
- char value = '\n';
- //readi if buff_size == 0
- if (file->buff_size == 0) {
- if (int readSuccess = read(file->fd, file->buff, BUFF_SIZE) >= 0) {
- file->buff_size = readSuccess;
- file->cur_buffPos = 0;
- } else {
- perror("Error on read:");
- my_close(file);
- exit(EXIT_FAILURE);
- }
- }
- //return value at buff position;
- if (file->buff_size > 0) {
- value = file->buff[file->cur_buffPos];
- file->buff_size--;
- file->cur_buffPos++;
- return value;
- }
- return value;
- }
- void my_putChar(char ch,FICHIER *file) {
- static bool isBuffer_full = false;
- if (isBuffer_full) {
- if (int written = write(file->fd,&file->buff[file->cur_buffPos] , 1) == -1){
- perror("Error: write on file");
- my_close(file);
- exit(EXIT_FAILURE);
- }
- file->buff_size--;
- file->cur_buffPos++;
- if(file->buff_size == 0){
- isBuffer_full = !isBuffer_full;
- }
- }
- if (!isBuffer_full) {
- //read
- file->cur_buffPos = 0;
- file->buff[file->cur_buffPos] = ch;
- file->buff_size++;
- if(file->buff_size == BUFF_SIZE){
- isBuffer_full = !isBuffer_full;
- return;
- }
- file->cur_buffPos++;
- }
- }
- FICHIER *my_open(char *filePath, char mode) {
- if (!filePath) {
- perror("Error: file path name is NULL");
- exit(EXIT_FAILURE);
- }
- FICHIER *file = NULL;
- int fd = 0;
- switch (mode) {
- case 'r':
- fd = open(filePath, O_RDONLY);
- break;
- case 'w':
- fd = open(filePath, O_WRONLY | O_CREAT | O_TRUNC);
- break;
- default:
- printf("Unknown mode use :> r → for read; w → for write ");
- exit(EXIT_FAILURE);
- break;
- }
- file = (FICHIER *) calloc(1, sizeof(*file));
- if (!file) {
- close(fd);
- perror("Error: on allocating space for file");
- exit(EXIT_FAILURE);
- }
- if (fd == -1) {
- perror("Error: on opening file");
- free(file);
- exit(EXIT_FAILURE);
- }
- //struct init part
- file->fd = fd;
- file->buff_size = BUFF_SIZE;
- file->my_getchar = my_getChar;
- file->my_putchar = my_putChar;
- //end struct init part
- return file;
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement