Sunteți pe pagina 1din 19

----------------------------INFO

FIS---------------------------------#include

<stdio.h>

#include <string.h>
#include
#include
#include
#include

<stdlib.h>
<sys/types.h>
<sys/stat.h>
<errno.h>

/**
* intoarce
* 0 - nu sunt erori
* 1 - eroare la apelul functiei stat
* 2 - nu s-a putut citi numele fisierului
*/
int main(int argc, char** argv){
int test;
struct stat info;
char *numeFis = (char*)malloc(200);
/*nume fisier*/
printf("Introduceti numele fisierului:");
test=scanf("%s",numeFis);
if(test==EOF || test!=1){
printf("Eroare la citire nume fisier");
exit(2);
};
if(stat(numeFis,&info)<0){
perror("Eroare la citire informatii");
return 1;
}
printf("Numele fisierului : %s\n",numeFis);
printf("Lungimea fisierului : %d octeti\n",info.st_size);
printf("Posesor : %x\n",info.st_uid);
printf("Grup : %x\n",info.st_uid);
printf("Drepturi : \n");
printf("\tPosesor %s %s %s\n",((info.st_mode &
S_IRUSR)?"citire":""),
((info.st_mode & S_IWUSR)?"scriere":""),
((info.st_mode & S_IXUSR)?"executie":""));
printf("\tGrup %s %s %s\n",((info.st_mode &
S_IRGRP)?"citire":""),
((info.st_mode & S_IWGRP)?"scriere":""),
((info.st_mode & S_IXGRP)?"executie":""));
printf("\tAltii %s %s %s\n",((info.st_mode &
S_IROTH)?"citire":""),
((info.st_mode & S_IWOTH)?"scriere":""),
((info.st_mode & S_IXOTH)?"executie":""));

-----------------HEX DUMP
--------------------------#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<sys/types.h>
<sys/stat.h>
<fcntl.h>
<unistd.h>

int main(int argc, char* argv[])

int fdFile;
long lSize;
unsigned char * buffer;
size_t result;
char sir[10];
char c;
int startPosition = 0;
char currentLine[100];
// incercam sa deschidem fisierul
fdFile = open(argv[1], O_RDONLY);
if (fdFile == -1) {
printf("Fisier lipsa\n");
exit(1);
}
// calculam dimensiunea fisierului de intrare
lSize = lseek(fdFile, 0, SEEK_END);
lseek(fdFile, 0, SEEK_SET);
// alocam memorie in care sa bagam continutul fisierului
buffer = (unsigned char*) malloc(sizeof (unsigned char) *lSize);
if (buffer == NULL) {
printf("Alocare esuata\n");
exit(1);
}
// copiem efectiv continutul fisierului in buffer
result = read(fdFile, buffer, lSize);
// fread de mai sus intoarce numarul de bytes copiati
// numar ce trebuie sa fie exact acelasi cu cel citit initial
if (result != lSize) {
printf("Eroare la citire\n");

exit(1);

unsigned int i = 1;
printf("\n%08x ", 0x00);
// bucla cu care ciclam prin toate elementele bufferului
for (; i <= lSize; i++) {
printf("%02X ", buffer[i-1]);
if ( i % 0x10 == 0 ) printf("\n%08x

", i);

}
printf("\n%08x\n", i-1);
close(fdFile);
free(buffer);
return 0;
}

---------------------------------TEXTO
------------------------------#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<sys/types.h>
<sys/stat.h>
<fcntl.h>
<unistd.h>

int main(int argc, char **argv) {


int fdFile;
long lSize;
char * buffer;
size_t result;
char sir[10];
char c;
int startPosition = 0;
char currentLine[200];
// incercam sa deschidem fisierul
fdFile = open(argv[1], O_RDONLY);
if (fdFile == -1) {
printf("Fisier lipsa\n");
exit(1);
}
// calculam dimensiunea fisierului de intrare

lSize = lseek(fdFile, 0, SEEK_END);


lseek(fdFile, 0, SEEK_SET);
// alocam memorie in care sa bagam continutul fisierului
buffer = (char*) malloc(sizeof (char) *lSize);
if (buffer == NULL) {
printf("Alocare esuata\n");
exit(1);
}
// copiem efectiv continutul fisierului in buffer
result = read(fdFile, buffer, lSize);
// fread de mai sus intoarce numarul de bytes copiati
// numar ce trebuie sa fie exact acelasi cu cel citit initial
if (result != lSize) {
printf("Eroare la citire\n");
exit(1);
}
printf("%s", buffer);
printf("Introduceti sirul ce trebuie cautat: \n");
fgets(sir, 9, stdin);
printf("Cautam sirul: %s\n", sir);
// facem variabila sir null-terminated, deoarece fgets nu o face
sir[strlen(sir) - 1] = '\0';
// bucla cu care ciclam prin toate elementele bufferului
for (int i = 0; i < lSize; i++) {
c = buffer[i];
// verificam daca am ajuns la sfirsit de linie
if (c == '\n' && (i - startPosition) > 1) {
// copiem in currentLine portiunea din buffer ce
reprezinta ultima linie parcurs
strncpy(currentLine, &buffer[startPosition], i startPosition);
// facem linia null-terminated, deoarece strncpy nu o face
currentLine[i - startPosition] = '\0';
// updatam pozitia de start a urmatoarei linii
startPosition = (i + 1);

cautat

// verificam daca nu cumva linia curenta contine sirul

if (strstr(currentLine, sir) != NULL) { printf("%s\n",


currentLine); }
}
}

close(fdFile);
free(buffer);
return 0;

-------------------------- WORD
COUNT------------------#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<sys/types.h>
<sys/stat.h>
<fcntl.h>
<unistd.h>

int main(int argc, char* argv[])

int fdFile;
long lSize;
unsigned char * buffer;
size_t result;
char sir[10];
char c;
int startPosition = 0;
bool insideWord = false;
unsigned int lineCount = 0;
unsigned int wordCount = 0;
// incercam sa deschidem fisierul
fdFile = open("texto", O_RDONLY);
if (fdFile == -1) {
printf("Fisier lipsa\n");
exit(1);
}
// calculam dimensiunea fisierului de intrare
lSize = lseek(fdFile, 0, SEEK_END);
lseek(fdFile, 0, SEEK_SET);

// alocam memorie in care sa bagam continutul fisierului


buffer = (unsigned char*) malloc(sizeof (unsigned char) *lSize);
if (buffer == NULL) {
printf("Alocare esuata\n");
exit(1);
}
// copiem efectiv continutul fisierului in buffer
result = read(fdFile, buffer, lSize);
// fread de mai sus intoarce numarul de bytes copiati
// numar ce trebuie sa fie exact acelasi cu cel citit initial
if (result != lSize) {
printf("Eroare la citire\n");
exit(1);
}
// bucla cu care ciclam prin toate elementele bufferului
for (unsigned int i = 1; i <= lSize; i++) {
// verificam daca sintem in interiorul unui cuvint
if ((buffer[i] >= 'a' && buffer[i] <= 'z') || (buffer[i] >=
'A' && buffer[i] <= 'Z')) {
insideWord = true;
} else if (!((buffer[i] >= 'a' && buffer[i] <= 'z') ||
(buffer[i] >= 'A' && buffer[i] <= 'Z'))) {
if (insideWord == true) {
wordCount++;
insideWord = false;
}
if (buffer[i] == '\n') {
lineCount++;
}

}
printf("%u %u %u\n", lineCount, wordCount, lSize);
close(fdFile);
free(buffer);
return 0;
}

----------------------------SPLIT--------------------------------------#include
#include
#include
#include
#include
#include
#include
#define
#define
(char *
= '\0';

<stdio.h>
<stdlib.h>
<string.h>
<sys/types.h>
<sys/stat.h>
<fcntl.h>
<unistd.h>
ERROR(s) do { write(1, s, strlen(s)); exit(-1); } while(0);
STRCPY(dest, source) do { int length = strlen(source); dest =
)malloc(length+1); memcpy(dest, source, length); dest[length]
} while(0);

/*
* input corect: numeFisierIntrare numarFisiereIesire lungimeFisiere
*/
int main(int argc, char* argv[]) {
int fdFile;
int fdOutput;
long lSize;
unsigned char * buffer;
size_t result;
int offset = 0;
char * baseFileName;
char * currentFileName;
int nrFisiere = 0;
int lungFisiere = 0;
int currentLength = 0;
if (argc < 4) {
ERROR("Unul din argumente lipseste\n")
} else {
if (argv[1] == NULL)
ERROR("Numele fisierului lipseste\n")
else
STRCPY(baseFileName, argv[1])
if (argv[2] == NULL)
ERROR("Numarul fisierelor de iesire lipseste\n")
else

nrFisiere = atoi(argv[2]);

if (argv[3] == NULL)

ERROR("Dimensiunea fisierelor de iesire lipseste\n")

else
}

lungFisiere = atoi(argv[3]);

fdFile = open(baseFileName, O_RDONLY);


if (fdFile == -1)
ERROR("Fisier lipsa\n")
lSize = lseek(fdFile, 0, SEEK_END);
lseek(fdFile, 0, SEEK_SET);
buffer = (unsigned char*) malloc(sizeof (unsigned char) *lSize);
if (buffer == NULL)
ERROR("Alocare esuata\n")
else
result = read(fdFile, buffer, lSize);
if (result != lSize)
ERROR("Eroare la citire\n")
if (nrFisiere == 0 || lungFisiere == 0 )
ERROR("Numarul de fisiere si lungimea lor trebuie sa fie
diferite de 0\n")
if (lSize > (nrFisiere*lungFisiere))
ERROR("Datele nu vor incapea in noile fisiere\n")
if ((nrFisiere*lungFisiere) - lSize > lungFisiere-1)
ERROR("Vor ramine fisiere goale\n")
printf("Au fost create %d fisiere cu numele:\n", nrFisiere);
for (int i = 0; i < nrFisiere && offset < lSize; ++i) {
currentFileName = (char *) malloc(strlen(baseFileName) + 7);
sprintf(currentFileName, "%s_%d.txt", baseFileName, (i + 1));
fdOutput = open(currentFileName, O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
if (fdOutput == -1) {
ERROR("Nu s-a putut creea fisierul\n")
} else {
currentLength = (lSize - offset > lungFisiere ?
lungFisiere : lSize - offset);
write(fdOutput, buffer + offset, currentLength);
printf("%s | %d bytes\n", currentFileName, currentLength);
close(fdOutput);
free(currentFileName);

offset += currentLength;

}
}

close(fdFile);
free(buffer);
free(baseFileName);
return 0;
}

-------------------------PARTE----------------------------#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<sys/types.h>
<sys/stat.h>
<fcntl.h>
<unistd.h>

#define ERROR(s) do { write(1, s, strlen(s)); exit(-1); } while(0);


/*
* input corect: numeFisierIntrare numarCaractereAfisare pozitieStart
*/
int main(int argc, char* argv[]) {
int fdFile;
int fdOutput;
long lSize;
unsigned char * buffer;
size_t result;
int offset = 0;
int startPosition = 0;
int outLength = 0;
if (argc < 4) {
ERROR("Unul din argumente lipseste\n")
} else {
outLength = atoi(argv[2]);
startPosition = atoi(argv[3]);
}
fdFile = open(argv[1], O_RDONLY);
if (fdFile == -1)

ERROR("Fisier lipsa\n")
lSize = lseek(fdFile, 0, SEEK_END);
lseek(fdFile, 0, SEEK_SET);
buffer = (unsigned char*) malloc(sizeof (unsigned char) *lSize);
if (buffer == NULL)
ERROR("Alocare esuata\n")
else
result = read(fdFile, buffer, lSize);
if (result != lSize)
ERROR("Eroare la citire\n")
if (outLength == 0 || (startPosition+outLength) > lSize)
ERROR("Date de intrare invalide\n")
write(1, buffer + startPosition, outLength);
close(fdFile);
free(buffer);
return 0;
}

--------------------------GOLURI
------------------------------------#include
#include
#include
#include
#include
#include
#include

<stdlib.h>
<stdio.h>
<string.h>
<sys/types.h>
<sys/stat.h>
<unistd.h>
<fcntl.h>

int main(int argc, char** argv){


int fisier, nr_goluri, lungime_gol, i, contor;
struct stat st;
char *sirCompletare = "Un sir oarecare de introdus in fisier";
if(argc!=4){
printf("Utilizare: ./goluri nume_fisier nr_goluri
lungime_gol\n");
return 1;
}

if((nr_goluri=atoi(argv[2]))<0){
printf("Numarul de goluri nu poate fi negativ\n");
return 2;
}
if((lungime_gol=atoi(argv[3]))<1024){
printf("Lungimea golurilor trebuie sa fie >1024\n");
return 3;
}

if((fisier=creat(argv[1],S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|
S_IWOTH))<0){
printf("Nu a putut fi creat fisierul %s\n",argv[1]);
return 4;
}
/* scriere */
if(write(fisier,sirCompletare,strlen(sirCompletare))<0){
printf("Eroare scriere in fisier\n");
return 5;
}
lseek(fisier,nr_goluri * lungime_gol,SEEK_CUR);
/* scriere */
if(write(fisier,sirCompletare,strlen(sirCompletare))<0){
printf("Eroare scriere in fisier\n");
return 5;
}
close(fisier);
stat(argv[1], &st);
printf("Marime totala asteptata: %ld, marime in disc %ld in %ld
blocuri\n",
st.st_size,
st.st_blksize*st.st_blocks,
st.st_blocks);
return 0;
}

--------------------COPIE GOLURI
---------------------

#include
#include
#include
#include
#include
#include
#include

<stdlib.h>
<stdio.h>
<string.h>
<sys/types.h>
<sys/stat.h>
<unistd.h>
<fcntl.h>

int main(int argc, char **argv){


int fisier,destinatie,citit;
char buffer[512];
struct stat st;
if(argc!=3){
printf("Folosire: ./copiere_goluri fis_original
fis_destinatie\n");
return 1;
}
if((fisier=open(argv[1],O_RDONLY))<0){
printf("Eroare la deschidere fisier original\n");
return 2;
}
if((destinatie=creat(argv[2],S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|
S_IROTH|S_IWOTH))<0){
printf("Eroare la deschidere fisier original\n");
close(fisier);
return 3;
}
while((citit = read(fisier, buffer, 512)) > 0){
if(write(destinatie,buffer,citit)<0){
perror("Eroare la scriere:");
close(fisier);
close(destinatie);
return 4;
}
}
close(fisier);
close(destinatie);
stat(argv[1], &st);
printf("Lungime fisier %s : %ld\n",argv[1],st.st_size);
stat(argv[2], &st);
printf("Lungime fisier %s : %ld\n",argv[2],st.st_size);

return 0;

--------------PARINTE CU DOI FII---------------#include <stdio.h>


#include <stdlib.h>
#include <unistd.h>
int main() {
int p1,fii,i,iesire;
for(fii = 1; fii < 3; fii++) {
p1 = fork();
if (p1==0) {
printf("fiu %d PID:%d parinte:
%d\n",fii,getpid(),getppid());
exit(1);
}
}

wait(&iesire);
printf("parinte PID:%d\n",getpid());

--------------PARINTE CU UN FIU SI UN NEPOT---------------#include <stdio.h>


#include <stdlib.h>
#include <unistd.h>
int main() {
int p1,p2,fii,i,iesire;
p1 = fork();
if (p1==0) {
printf("fiu PID:%d parinte:%d\n",getpid(),getppid());
p2=fork();
if(p2==0){
printf("nepot PID:%d parinte:
%d\n",getpid(),getppid());
exit(2);
}
exit(1);
}
wait(&iesire);

printf("parinte:%d iesire\n",getpid());

--------------PARINTE CU DOI FII SI DOI NEPOTI---------------#include <stdio.h>


#include <stdlib.h>
#include <unistd.h>
int main() {
int p1,p2,fii,i,iesire;
for(fii = 1; fii < 3; fii++) {
p1 = fork();
if (p1==0) {
printf("Fiu %d PID:%d parinte:
%d\n",fii,getpid(),getppid());
p2=fork();
if(p2==0){
printf("Nepot %d PID:%d parinte:
%d\n",fii,getpid(),getppid());
exit(2);
}
wait(&iesire);
exit(1);
}
}

wait(&iesire);
printf("parinte PID:%d\n",getpid());

--------------CONDUCTA LA PARINTE CU UN FIU SI UN NEPOT--------------##include <stdio.h>


#include <stdlib.h>
#include <unistd.h>
int main() {
int p1,p2,pipe1[2],i,iesire;
char c;
if(pipe(pipe1)==-1)printf("Nu s-a putut crea pipe in proces
%d\n",getpid());

p1 = fork();
if (p1==0) {
printf("fiu PID:%d parinte:%d\n",getpid(),getppid());
close(1);
dup(pipe1[1]);
close(pipe1[0]);
p2=fork();
if(p2==0){//nepot
c='A';
for(i=0;i<512;i++)printf("%c",c);
exit(2);
}
exit(1);
}
close(pipe1[1]);
while(read(pipe1[0],&c,1)){
printf("%c",c);
}

close(pipe1[0]);
printf("\nparinte:%d iesire\n",getpid());

--------------CONDUCTA LA PARINTE CU DOI FII SI DOI NEPOTI-----------#include <stdio.h>


#include <stdlib.h>
#include <unistd.h>
int main() {
int p1,p2,fii,pipe1[2],i,iesire;
char c;
if(pipe(pipe1)==-1)printf("Nu s-a putut crea pipe in proces
%d\n",getpid());
for(fii = 1; fii < 3; fii++) {
p1 = fork();
if (p1==0) {
printf("Fiu %d PID:%d parinte:
%d\n",fii,getpid(),getppid());

close(1);
dup(pipe1[1]);
close(pipe1[0]);
p2=fork();
if(p2==0){

//printf("Nepot %d PID:%d parinte:


%d\n",fii,getpid(),getppid());
c=(fii==1)?'B':'C';
for(i=0;i<512;i++)printf("%c",c);
exit(2);
}
exit(1);
}

close(pipe1[1]);
while(read(pipe1[0],&c,1)){
printf("%c",c);
}
close(pipe1[0]);
printf("\nparinte PID:%d\n",getpid());
}

-----------------STATISTICI---------------#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<unistd.h>
<fcntl.h>

int main(int argc, char** argv) {


int p1,i,iesire,pipe1[2];
int fisier,contorA=0,contora=0,contor1=0;
char c;
if(argc<2){
printf("Lipseste nume fisier pentru statistica\n");
return 1;
}
if((fisier=open(argv[1],O_RDONLY))<0){
printf("Fisier '%s' inexistent\n",argv[1]);
return 2;

}
if(pipe(pipe1)==-1)printf("Nu s-a putut crea pipe in proces
%d\n",getpid());
p1 = fork();
if (p1==0) {
close(pipe1[1]);
while(read(pipe1[0],&c,1)>0){
if(c>='a' && c<='z')contora++;
if(c>='A' && c<='Z')contorA++;
if(c>='0' && c<='9')contor1++;
}
printf("Sunt:\n\t%d litere mici,\n\t%d litere
mari,\n\t%d cifre\n",contora,contorA,contor1);
}

exit(0);

close(pipe1[0]);
while(read(fisier,&c,1)){
write(pipe1[1],&c,1);
}
close(pipe1[1]);
close(fisier);
}

----------PIPE 2 INFINIT-------------#include <stdio.h>


#include <stdlib.h>
#include <unistd.h>
int main() {
int p1,p2,pipe1[2],i,iesire;
char c;
if(pipe(pipe1)==-1)printf("Nu s-a putut crea pipe in proces
%d\n",getpid());
p1 = fork();
if (p1==0) {
printf("fiu PID:%d parinte:%d\n",getpid(),getppid());

close(1);
dup(pipe1[1]);
close(pipe1[0]);
p2=fork();
if(p2==0){//nepot
c='A';
while(1)printf("%c",c);
exit(2);
}
exit(1);
}
close(pipe1[1]);
while(read(pipe1[0],&c,1)){
printf("%c",c);
}

close(pipe1[0]);
printf("\nparinte:%d iesire\n",getpid());

--------PIPE 3 INFINIT -------------#include <stdio.h>


#include <stdlib.h>
#include <unistd.h>
int main() {
int p1,p2,fii,pipe1[2],i,iesire;
char c;
if(pipe(pipe1)==-1)printf("Nu s-a putut crea pipe in proces
%d\n",getpid());
for(fii = 1; fii < 3; fii++) {
p1 = fork();
if (p1==0) {
printf("Fiu %d PID:%d parinte:
%d\n",fii,getpid(),getppid());
close(1);
dup(pipe1[1]);

close(pipe1[0]);
p2=fork();
if(p2==0){

//printf("Nepot %d PID:%d parinte:


%d\n",fii,getpid(),getppid());
c=(fii==1)?'B':'C';
while(1)printf("%c",c);
exit(2);
}
exit(1);
}

close(pipe1[1]);
while(read(pipe1[0],&c,1)){
printf("%c",c);
}
close(pipe1[0]);
printf("\nparinte PID:%d\n",getpid());
}