Académique Documents
Professionnel Documents
Culture Documents
h>
#include <stdlib.h>
char get_byte(int poz, FILE *pFile)
{
char byte;
fseek(pFile, poz, 0);
fread(&byte, 1, 1, pFile);
return byte;
}
int get_int(int poz, FILE *pFile)
{
int x;
fseek(pFile, poz, 0);
fread(&x, 4, 1, pFile);
return x;
}
int get_dimension_of_file(FILE *pFile)
{
int size;
fseek(pFile, 0, 2);
/* file pointer at the end of file */
size = ftell(pFile);
return size;
}
int check_partition(int poz, int cu_afisare, FILE *pFile)
{
if(!poz)
return 1; //nu stiu ce se intamla daca gaseste o partitie stearsa (adica
in mbr sa gasesc 4 de 0 consecutivi ca adresa)
//pe langa faptul ca verifica partitia, poate afisa si schema ei.
int MAXD, MAXF, total_space, bytes_used, i,j,k;
int name_length;
if(cu_afisare)
{
for(i=poz+4; i<poz+12; i++)
{
printf("%c", get_byte(i, pFile));
}
}
MAXD = get_int(poz+12, pFile);
MAXF = get_int(poz+16, pFile);
total_space = MAXD*MAXF*12 + MAXD*12 + MAXF*12;
bytes_used = total_space; //vom scada cand gasim dir/fis goale.
{
printf(" %d %d\n", total_space, bytes_used);
}
return 1;
}
int find_director(char target_partition_name[100], char target_director_name[100
], int *MAXD, int *MAXF, int *poz_partition)
{
/*returneaza pozitiade inceput a directorului, incepand cu headerul*/
/*avem si var poz_partition, deoarece cand cautam un fisier, cu aceasta fct
e posibil sa nu fie specificat un director, si sa avem nevoie doar de incepu
tul partitiei*/
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb" );
int i, ct, nb_partitions, k, poz, j, name_length, k1, k2;
char name_of_partition[10], name_of_director[10];
nb_partitions = get_int(0, pFile);
for(i=1, k=4; i<=nb_partitions; i++, k+=4)
{
//verificam daca exista partitia din path
poz = get_int(k, pFile);
name_length=get_int(poz, pFile);
for(j=poz+4, ct=0; j<poz+4+name_length; j++)
{
name_of_partition[ct++]=get_byte(j, pFile);
}
name_of_partition[ct]='\0';
if(!strcmp(target_partition_name, name_of_partition))
{
//printf("\ngasit partitie\n");
//verificam daca exsita si directorul din path
if(poz_partition!=NULL)
*poz_partition = poz;
(*MAXD) = get_int(poz+12, pFile);
(*MAXF) = get_int(poz+16, pFile);
poz+=20;
for(k1=1; k1<=(*MAXD); k1++)
{
name_length=get_int(poz, pFile);
for(k2 = poz+4, ct=0; k2<poz+4+name_length; k2++)
{
name_of_director[ct++]=get_byte(k2, pFile);
}
name_of_director[ct]='\0';
if(!strcmp(target_director_name, name_of_director))
{
return poz;
}
else
{
poz+=4+8+12*(*MAXF);
}
}
}
}
fclose(pFile);
return 0;
}
if(!name_dim)
{
//am gasit un spatiu gol
valid=1;
fseek(pFile, poz, SEEK_SET );
sir[0]=strlen(name_of_file);
fwrite( sir, 4, 1, pFile );
fwrite( name_of_file, 1, sir[0], pFile );
}
poz+=12;
}
}
}
if(!valid)
{
printf("INVALID\n");
}
else
{
printf("SUCCES\n");
}
fclose(pFile);
}
void RENAME_FILE(char path[100], char new_name[100])
{
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb+" );
char name_of_director[100], name_of_file[100], name_of_partition[100];
int sir[100]={0};
int poz, MAXD, MAXF;
int in_director, i, name_dim;
int valid=0, partition_start;
extract_info_from_path(path, name_of_file, name_of_director, name_of_partiti
on, &in_director);
poz = find_director(name_of_partition, name_of_director, &MAXD, &MAXF, &part
ition_start);
char name[100]; //variabila in care tinem minte numele fisierelor gasite pe
partitie;
if(in_director)
{
if(poz)
{
for(i=1; i<=MAXF; i++)
{
poz+=12;
fseek(pFile, poz, SEEK_SET );
name_dim = get_int(poz, pFile);
fread(name, 1, name_dim, pFile);
name[name_dim]='\0';
if(!strcmp(name, name_of_file))
{
fclose(pFile);
}
}
fclose(pFile);
}
void RENAME_DIR(char path[100], char new_name[100])
{
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb+" );
int i, l, poz;
int MAXD, MAXF;
int sir[1000]={0};
char partition_name[10], director_name[10];
l=strlen(path);
for(i=0; i<=7; i++)
{
partition_name[i]=path[i];
}
partition_name[8]='\0';
for(i=9; i<l; i++) //sarim peste caracterul '/' care urm dupa numele partiti
ei
{
director_name[i-9]=path[i];
}
director_name[l-9]='\0';
//printf("\n%s %s\n", partitie, nume_dir);
poz = find_director(partition_name, director_name, &MAXD, &MAXF, NULL);
if(poz)
{
fseek(pFile, poz, SEEK_SET );
sir[0]=strlen(new_name);
fwrite( sir, 4, 1, pFile );
poz+=4;
fwrite( new_name, 1, sir[0], pFile );
printf("SUCCES\n");
}
else
{
printf("INVALID\n");
}
fclose(pFile);
/*verificare temp
pFile = fopen ( "mbr.bin" , "rb+" );
poz = find_director(partition_name, director_name, &MAXD, &MAXF);
printf("%d", poz);*/
}
void DELETE_DIR(char path[100])
{
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb+" );
int i, l, poz;
int MAXD, MAXF;
int sir[1000]={0};
char partition_name[10], director_name[10];
l=strlen(path);
}
void CREATE_DIR(char path[100])
{
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb+" );
int i, l, ct, nb_partitions, k, poz, j, name_length, k1;
int valid=0;
int MAXD, MAXF;
int sir[1000]={0};
char partition_name[10], director_name[10], name_of_partition[10];
l=strlen(path);
for(i=0; i<=7; i++)
{
partition_name[i]=path[i];
}
partition_name[8]='\0';
for(i=9; i<l; i++) //sarim peste caracterul '/' care urm dupa numele partiti
ei
{
director_name[i-9]=path[i];
}
director_name[l-9]='\0';
//printf("\n%s %s\n", partitie, nume_dir);
void task1()
{
int i, k;
int nb_partitions, valid;
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb" );
valid = 1;
// printf("%d", get_dimension_of_file(pFile));
nb_partitions = get_int(0, pFile);
for(i=1, k=4; i<=nb_partitions; i++, k+=4)
{
if(!check_partition(get_int(k, pFile), 0, pFile))
{
printf("INVALID\n");
valid = 0;
break;
}
}
if(valid)
{
printf("SUCCES\n");
for(i=1, k=4; i<=nb_partitions; i++, k+=4)
{
check_partition(get_int(k, pFile), 1, pFile);
}
}
/*for(i=0; i<=get_dimension_of_file(pFile); i++)
{
printf("%d ", get_byte(i, pFile));
}*/
}
void task2()
{
char command[100], path[100], name[100];
FILE *pFile;
pFile = fopen ( "mbr.op" , "r" );
while(1)
{
strcpy(command, "blank");
fscanf(pFile, "%s", command);
if(!strcmp(command, "CREATE_FILE"))
{
//printf("%s", command);
fscanf(pFile, "%s", path);
//printf("\n%s\n", path);
CREATE_FILE(path);
continue; //CHECKED
}
if(!strcmp(command, "RENAME_FILE"))
{
//printf("%s", command);
fscanf(pFile, "%s", path);
fscanf(pFile, "%s", name);
// printf("\n%s", path);
// printf(" %s\n", name);
RENAME_FILE(path, name);
continue; //CHECKED
}
if(!strcmp(command, "DELETE_FILE"))
{
//printf("%s", command);
fscanf(pFile, "%s", path);
//printf("\n%s\n", path);
RENAME_FILE(path, "");
continue; //CHECKED
}
if(!strcmp(command, "CREATE_DIR"))
{
//printf("%s", command);
fscanf(pFile, "%s", path);
//printf(" %s\n", path);
CREATE_DIR(path);
continue; //CHECKED
}
if(!strcmp(command, "RENAME_DIR"))
{
//printf("%s", command);
fscanf(pFile, "%s", path);
fscanf(pFile, "%s", name);
//printf("\n%s", path);
//printf(" %s\n", name);
RENAME_DIR(path, name); //CHECKED
continue;
}
if(!strcmp(command, "DELETE_DIR"))
{
//printf("%s", command);
fscanf(pFile, "%s", path);
//printf(" %s\n", path);
DELETE_DIR(path);
continue; //CHECKED
}
if(!strcmp(command, "PRINT"))
{
//printf("%s", command);
fscanf(pFile, "%s", path);
//printf(" %s\n", path);
PRINT(path);
continue; //CHECKED
}
if(!strcmp(command, "blank"))
{
//printf("%s\n", command);
break; //CHECKED
}
}
}
void DELETE_PARTITION(char target_partition_name[100])
{
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb+" );
int i,ct, nb_partitions, k, poz, j, name_length;
int valid=0;
int zero=0;
char name_of_partition[10];
}
}
fclose(pFile);
if(!valid)
{
printf("INVALID\n");
}
else
{
printf("SUCCES\n");
}
}
void FORMAT_PARTITION(char partition_name[100])
{
FILE *pFile;
pFile = fopen ( "mbr.op" , "rb+" );
int MAXD, MAXF, poz=0;
int zero[1000]={0};
int formula;
char random_dir_name[100];//ne intereseaza sa gaseasca doar paritita, asa
//ca dau un nume random pt parametrul director
strcpy(random_dir_name, "ceva_random");
find_director(partition_name, random_dir_name, &MAXD, &MAXF, &poz); //gasest
e inceputul partitiei
if(poz)
{
fseek(pFile, poz+20, SEEK_SET );
formula = MAXD*(12+12*MAXF)+12*MAXF; //nr divizibil mereucu 4;
printf("SUCCES\n");
fwrite(zero, 4, formula/4, pFile);
}
else
{
printf("INVALID\n");
}
}
void CREATE_PARTITION(char partition_name[100], int MAXD, int MAXF)
{
int MAXDP, MAXFP;
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb+" );
int lista_adrese[1000], k, i, poz, next_space;
lista_adrese[0]=0;
int liber=0;
int nb_partitions = get_int(0, pFile);
int valid=0, dim, space_needed;
for(i=1, k=4; i<=nb_partitions; i++, k+=4)
{
poz = get_int(k, pFile);
if(poz)
{
lista_adrese[0]++;
lista_adrese[lista_adrese[0]]=poz;
}
else
{
if(!liber)
{
liber=k; //retinem poztia primului spatiu gol din mbr.
}
}
}
//printf("Adresele sunt : ");
for(i=1; i<=lista_adrese[0]; i++)
{
//eventual le sortez crescator
//printf("%d ", lista_adrese[i]);
}
//se garanteaza ca nu se sterge prima partitie. presuspun ca adresa primei e
imediar dupa mbr
//asa ca nu mai verific daca e spatiu intre mbr si restu
if(!liber)
{
//nu mai e loc in mbr!!
}
for(i=1; i<lista_adrese[0]; i++)
{
poz=lista_adrese[i];
//verificam spatiul intre 2 partitii. daca e destul cream o partitie nou
a
fseek(pFile, poz, SEEK_SET );
}
if(!strcmp(command, "blank"))
{
//printf("%s\n", command);
break; //CHECKED
}
}
fclose(pFile);
}
void bonus()
{
FILE *pFile;
pFile = fopen ( "mbr.bin" , "rb+" );
char name[100], pattern[100];
int dim = get_dimension_of_file(pFile);
int mbr[100], i;
mbr[0]=0;
strcpy(pattern, "/dev/sd" );
for(i=1; i<dim-8; i++)
{
fseek(pFile, i, SEEK_SET );
fread(name, 1, 7, pFile);
if(!strcmp(name, pattern))
{
mbr[0]++;
mbr[mbr[0]]=i-4;
}
}
printf("MBR_SIZE: %d\n", mbr[0]);
printf("PARTITIONS_OFFSET:");
for(i=1; i<=mbr[0]; i++)
{
printf(" %d", mbr[i]);
}
fclose(pFile);
}
main(int argc, char *argv[])
{
FILE *pFile;
pFile = fopen ( "stefan.out" , "a" );
//scanf("%d", &opt);
fprintf(pFile, "%s\n", argv[1]);
/*switch(opt)
{
case 1:
task1();
break;
case 2:
task2();
break;
case 3:
task3();
break;
case 4:
bonus();
break;
default:
break;
}*/
int bonuss=1;
if(!strcmp(argv[1], "1"))
{
task1();
bonuss=0;
}
if(!strcmp(argv[1], "2"))
{
bonuss=0;
task2();
}
if(!strcmp(argv[1], "3"))
{
bonuss=0;
task3();
}
if(bonuss)
bonus();
return 0;
}