Skip to content
Snippets Groups Projects
Commit d8ec8a83 authored by vincent.andrey1's avatar vincent.andrey1
Browse files

début backup

parent 312763d0
Branches main
No related tags found
No related merge requests found
Serie1C.txt 100644 → 100755
...@@ -4,4 +4,5 @@ a) p = addresse de n , pp = addresse de p ...@@ -4,4 +4,5 @@ a) p = addresse de n , pp = addresse de p
b) *p = 15 b) *p = 15
c) *pp = adresse de n c) *pp = adresse de n
d) **pp = 15 d) **pp = 15
e) e) ajout
\ No newline at end of file test
#include <stdio.h> #include <stdio.h>
long n = 15; long n = 15L;
long *p = &n; long *p = &n;
long **pp = &p; long **pp = &p;
......
...@@ -14,8 +14,6 @@ char* f(SpaceReplace* str){ ...@@ -14,8 +14,6 @@ char* f(SpaceReplace* str){
for(int i=0;i<length;i++){ for(int i=0;i<length;i++){
if(str->s[i]==' '){ if(str->s[i]==' '){
str->s[i]=str->r; str->s[i]=str->r;
} }
new_str[length-i-1]=str->s[i]; new_str[length-i-1]=str->s[i];
} }
......
No preview for this file type
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
int main(int argc,char **argv){ int main(int argc,char **argv){
if(argc!=5){ if(argc!=5){
printf("Usage: 'label' <n (nombre itérations)> <x (nombre décimal)> <y (nombre entier) >\n"); printf("Usage: 'label' <n (nombre itérations)> <x (nombre décimal)> <y (nombre entier) >\n");
return EXIT_FAILURE;
} }
else{ else{
printf("%s\n",argv[1]); printf("%s\n",argv[1]);
...@@ -17,4 +18,5 @@ int main(int argc,char **argv){ ...@@ -17,4 +18,5 @@ int main(int argc,char **argv){
printf("%f^%d = %f\n",x,(y+i),pow(x,(y+i))); printf("%f^%d = %f\n",x,(y+i),pow(x,(y+i)));
} }
} }
return EXIT_SUCCESS;
} }
\ No newline at end of file
voici un fichier pour tester ultra cp
sysTracer 0 → 100755
File added
#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
ultra-cp 0 → 100755
File added
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment