Documente Academic
Documente Profesional
Documente Cultură
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 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>
cautat
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 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);
}
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)
else
}
lungFisiere = atoi(argv[3]);
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>
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>
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>
return 0;
wait(&iesire);
printf("parinte PID:%d\n",getpid());
printf("parinte:%d iesire\n",getpid());
wait(&iesire);
printf("parinte PID:%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());
close(1);
dup(pipe1[1]);
close(pipe1[0]);
p2=fork();
if(p2==0){
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>
}
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);
}
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());
close(pipe1[0]);
p2=fork();
if(p2==0){
close(pipe1[1]);
while(read(pipe1[0],&c,1)){
printf("%c",c);
}
close(pipe1[0]);
printf("\nparinte PID:%d\n",getpid());
}