diff --git a/Serie1C.txt b/Serie1C.txt old mode 100644 new mode 100755 index 84cfde465f59bb7048541102f59fd9fcbce3e872..309419c99542a8a9480f4c43743eb404e1242be2 --- a/Serie1C.txt +++ b/Serie1C.txt @@ -4,4 +4,5 @@ a) p = addresse de n , pp = addresse de p b) *p = 15 c) *pp = adresse de n d) **pp = 15 -e) \ No newline at end of file +e) ajout +test diff --git a/exo1.c b/exo1.c index 7afa836dee8601baa12573ebbe787c7fced02c23..7f8d15ec4a708bda2de59482b94a8e79094c2f57 100644 --- a/exo1.c +++ b/exo1.c @@ -1,6 +1,6 @@ #include <stdio.h> -long n = 15; +long n = 15L; long *p = &n; long **pp = &p; diff --git a/exo3.c b/exo3.c index 8ddbfa8c7b76cfc7683f29545fcd126fb95e5f0c..48e15b451085be918738e0d0f6bd77da82453fc9 100644 --- a/exo3.c +++ b/exo3.c @@ -14,8 +14,6 @@ char* f(SpaceReplace* str){ for(int i=0;i<length;i++){ if(str->s[i]==' '){ str->s[i]=str->r; - - } new_str[length-i-1]=str->s[i]; } diff --git a/exo4 b/exo4 index d05ad831074fea4f5c2209fb3cf10801730784bc..c1bab392bc1d14ea766b16fd943eae3b8f6fe04d 100755 Binary files a/exo4 and b/exo4 differ diff --git a/exo4.c b/exo4.c index 7b276ac6b7cf0fb724f51cfe1b9bc7166e494698..1fd93d830301d66dd6541c4aa11b9a59b408011e 100644 --- a/exo4.c +++ b/exo4.c @@ -7,6 +7,7 @@ int main(int argc,char **argv){ if(argc!=5){ printf("Usage: 'label' <n (nombre itérations)> <x (nombre décimal)> <y (nombre entier) >\n"); + return EXIT_FAILURE; } else{ printf("%s\n",argv[1]); @@ -17,4 +18,5 @@ int main(int argc,char **argv){ printf("%f^%d = %f\n",x,(y+i),pow(x,(y+i))); } } + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/folder2/test.txt b/folder2/test.txt new file mode 100644 index 0000000000000000000000000000000000000000..6b65221774e1fe5d8db58da3993f3f34abc50caf --- /dev/null +++ b/folder2/test.txt @@ -0,0 +1 @@ +voici un fichier pour tester ultra cp diff --git a/sysTracer b/sysTracer new file mode 100755 index 0000000000000000000000000000000000000000..a9559e2c49bd956f9efce6091527f1f2d8489afd Binary files /dev/null and b/sysTracer differ diff --git a/sysTracer.c b/sysTracer.c new file mode 100644 index 0000000000000000000000000000000000000000..dd6a71cc26565cf0c1a90f727b4c05198327feeb --- /dev/null +++ b/sysTracer.c @@ -0,0 +1,18 @@ +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> + +int main(int argc,char **argv){ + FILE * p ; + int c; + p = fopen(argv[1], "rt"); + while(1) { + c = fgetc(p); + if( feof(p) ) { + break ; + } + printf("%c", c); + } + fclose(p); + return 0; +} \ No newline at end of file diff --git a/ultra-cp b/ultra-cp new file mode 100755 index 0000000000000000000000000000000000000000..c44e602d96e2f458329464742e7e03d5a36d342f Binary files /dev/null and b/ultra-cp differ diff --git a/ultra-cp.c b/ultra-cp.c new file mode 100644 index 0000000000000000000000000000000000000000..70a0503dfb3059732530962735b1be4900d7b248 --- /dev/null +++ b/ultra-cp.c @@ -0,0 +1,379 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> +#include <dirent.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <time.h> +#include <string.h> +#include <limits.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> + + +// struct stat{ +// dev_t st_dev; //device ID +// ino_t st_ino; //i-node number +// mode_t st_mode; //protection and type +// nlink_t st_nlink; //number of hard links +// uid_t st_uid; //user ID of owner +// gid_t st_gid; //group ID of owner +// dev_t st_rdev; //device type (if special file) +// off_t st_size; //total size, in bytes +// blksize_t st_blksize; //blocksize for filesystem I/O +// blkcnt_t st_blocks; //number of 512B blocks +// time_t st_atime; //time of last access +// time_t st_mtime; //time of last modification +// time_t st_ctime; //time of last change +// }; + +// struct dirent { /* dirent.h */ +// ino_t d_ino; /* inode number */ +// off_t d_off; /* opaque value used to get next dirent (do not use) */ +// unsigned short d_reclen; /* length of this record */ +// unsinged char d_type; /* type of file; not supported by all file systems */ +// char d_name[256]; /* filename (NULL terminated), sometimes d_name[0] */ +// }; + +static void get_permissions(mode_t mods, char * perms){ + int octal[20],i=0; + while(mods!=0){ + octal[i++]=mods%8; + mods/=8; + } + for(int j=3;j>0;j--){ + perms[3-j]=octal[j-1]; + } +} + +static void print_permissions(char * perms){ + char rwx[10]=""; + for(int i=0;i<3;i++){ + if(perms[i]>=4){ + strcat(rwx,"r"); + perms[i]-=4; + } + else{strcat(rwx,"-");} + if(perms[i]>=2){ + strcat(rwx,"w"); + perms[i]-=2; + } + else{strcat(rwx,"-");} + if(perms[i]==1){ + strcat(rwx,"x"); + } + else{strcat(rwx,"-");} + } + printf("%s ",rwx); +} + +static void norm_size(off_t size){ + int tmp=size; + int cpt=0; + do{ + tmp/=10; + cpt++; + }while (tmp!=0); + + for (int i=0; i<8-cpt;i++){ + printf(" "); + } + printf("%ld ",size); +} + +static void print_info(char* perms,off_t size,char* mod,char* name){ + print_permissions(perms); + norm_size(size); + printf("%s %s\n", mod, name); +} + +static void ls_dir(char* path){ + struct stat stats; + struct tm time; + char perms[3]; + char last_mod[40]; + + if(lstat(path,&stats)){ + fprintf(stderr, "cannot stat '%s': %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + + if(S_ISDIR(stats.st_mode)){ + DIR *dir = opendir(path); + + struct dirent *entry; + char *d_name; + + if (! dir) { + fprintf(stderr, "Cannot open directory '%s': %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + + while( (entry=readdir(dir))!=NULL){ + d_name=entry->d_name; + + char new_path[PATH_MAX]=""; + strcpy(new_path,path); + + char name[PATH_MAX]=""; + + strcpy(name,path); + strcat(name,entry->d_name); + + if(strcmp(d_name, "..") != 0 && strncmp(d_name, ".",1) != 0){ + if(lstat(name,&stats)){ + fprintf(stderr, "cannot stat '%s': %s\n", d_name, strerror(errno)); + exit(EXIT_FAILURE); + } + + get_permissions(stats.st_mode, perms); + + localtime_r(&stats.st_mtime, &time); + + + if (strftime(last_mod,40,"%c",&time) == 0) { + fprintf(stderr, "strftime returned 0"); + exit(EXIT_FAILURE); + } + + + if(entry->d_type & DT_DIR){ + printf("d"); + print_info(perms,stats.st_size,last_mod,name); + + strcat(new_path,d_name); + strcat(new_path,"/"); + + ls_dir(new_path); + } + + else if(entry->d_type & DT_REG){ + printf("-"); + print_info(perms,stats.st_size,last_mod,name); + } + + else if(entry->d_type & DT_LNK){ + printf("l"); + print_info(perms,stats.st_size,last_mod,name); + } + } + } + if(closedir(dir)){ + fprintf(stderr, "Cannot close directory '%s': %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + } + else{ + if(lstat(path,&stats)){ + fprintf(stderr, "cannot stat '%s': %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + + get_permissions(stats.st_mode, perms); + localtime_r(&stats.st_mtime, &time); + if (strftime(last_mod,40,"%c",&time) == 0) { + fprintf(stderr, "strftime returned 0"); + exit(EXIT_FAILURE); + } + printf("-"); + print_info(perms,stats.st_size,last_mod,path); + } +} + +static mode_t file_perms_to_modet(char * perm){ + mode_t mode=0; + + if (perm[0] == 'r') + mode |= 0400; + if (perm[1] == 'w') + mode |= 0200; + if (perm[2] == 'x') + mode |= 0100; + if (perm[3] == 'r') + mode |= 0040; + if (perm[4] == 'w') + mode |= 0020; + if (perm[5] == 'x') + mode |= 0010; + if (perm[6] == 'r') + mode |= 0004; + if (perm[7] == 'w') + mode |= 0002; + if (perm[8] == 'x') + mode |= 0001; + + return mode; +} + +static void get_permissions2(char * rwx,char * perms){ + // char* rwx[9]=""; + for(int i=0;i<3;i++){ + if(perms[i]>=4){ + strcat(rwx,"r"); + perms[i]-=4; + } + else{strcat(rwx,"-");} + if(perms[i]>=2){ + strcat(rwx,"w"); + perms[i]-=2; + } + else{strcat(rwx,"-");} + if(perms[i]==1){ + strcat(rwx,"x"); + } + else{strcat(rwx,"-");} + } +} + +static void backup(int size, char** argv){ + struct stat stats; + char perms[3]; + + for(int i=1;i<size;i++){ + + if(lstat(argv[i],&stats)){ + fprintf(stderr, "cannot stat '%s': %s\n", argv[i], strerror(errno)); + exit(EXIT_FAILURE); + } + + if(S_ISDIR(stats.st_mode)){ + if(strstr(argv[size],argv[i])==NULL){ + + } + } + + else if(S_ISREG(stats.st_mode)){ + + char buff; + char path[PATH_MAX]=""; + char rwx[9]=""; + int f1,f2; + + strcat(path,argv[size]); + strcat(path,argv[i]); + + get_permissions(stats.st_mode,perms); + get_permissions2(rwx,perms); + printf("%s\n",rwx); + mode_t mods=file_perms_to_modet(rwx); + printf("%d\n",mods); + + f1=open(argv[i],O_RDONLY); + if(f1==-1){ + fprintf(stderr, "can't open1 '%s': %s\n", argv[i], strerror(errno)); + exit(EXIT_FAILURE); + } + + f2=open(path,O_WRONLY | O_CREAT); + if(f2==-1){ + fprintf(stderr, "can't open2 '%s': %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + + if(chmod(path,mods)){ + fprintf(stderr, "can't modify permissions for %s : %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + + while(read(f1, &buff, 1)) + { + write(f2, &buff, 1); + } + + if(close(f1)){ + fprintf(stderr, "Cannot close file '%s': %s\n", argv[i], strerror(errno)); + exit(EXIT_FAILURE); + } + + if(close(f2)){ + fprintf(stderr, "Cannot close file '%s': %s\n", path, strerror(errno)); + exit(EXIT_FAILURE); + } + } + + else if(S_ISLNK(stats.st_mode)){ + + } + } +} + + +int main(int argc, char** argv){ + if(argc==2){ + ls_dir(argv[1]); + } + else if(argc>2){ + backup(argc-1,argv); + } + else{ + printf("Usage: ultra-cp [folder] for listing \n ultra-cp [folder1] [folder2] [folder...] [foldern] [destination] for backup\n"); + } + return 0; +} + + + +// #include <stdlib.h> +// #include <stdio.h> +// #include <sys/types.h> +// #include <string.h> //snprintf +// #include <errno.h> +// #include <dirent.h> +// #include <limits.h> //PATH_MAX + +// static void list_dir (const char * dir_name){ + +// DIR *d = opendir(dir_name); +// struct dirent *entry; +// const char *d_name; //nom d'une entrée + +// //En cas d'erreur d'ouverture +// if (! d) { +// fprintf(stderr, "Cannot open directory '%s': %s\n", +// dir_name, strerror(errno)); +// exit(EXIT_FAILURE); +// } + +// //Boucle sur chaque entrée +// while( (entry = readdir(d)) != NULL ) { + +// // Obtient le nom de l'entrée et affiche +// d_name = entry->d_name; +// printf("%s/%s\n", dir_name, d_name); + +// //Est-ce que 'entry' est un sous-répertoire +// if (entry->d_type & DT_DIR) { +// //Est-ce que 'entry' n'est pas '..' ou '.' +// if (strcmp(d_name, "..") != 0 && strcmp(d_name, ".") != 0) { +// char path[PATH_MAX]; + +// //forme le nom du sous-répertoire et affiche +// int path_length = snprintf (path, PATH_MAX, +// "%s/%s", dir_name, d_name); +// printf("%s\n", path); + +// //Vérifie que le nom du sous-répertoire n'est pas trop long +// if (path_length >= PATH_MAX) { +// fprintf(stderr, "Path length has got too long.\n"); +// exit(EXIT_FAILURE); +// } + +// //Appel récursif +// list_dir(path); +// } +// } +// } //while(1) + +// //On ferme le répertoite +// if( closedir(d) ) { +// fprintf(stderr, "Could not close '%s': %s\n", +// dir_name, strerror (errno)); +// exit (EXIT_FAILURE); +// } +// } + +// int main (int argc, char** argv) { +// list_dir(argv[1]); +// return EXIT_SUCCESS; +// } \ No newline at end of file