Ciao,
io ho imparato un po' del linguaggio C dal libro "L'arte dell'hacking" di Apogeo.
ora ti posto il codice completo del programma, spero ti aiuti.
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <hacking.h>
#define FILENAME "/var/notes"
int print_notes(int,int,char*);
int find_usr_note(int, int);
int search_note(char *, char *);
void fatal(char *);
int main(int argc, char** argv [])
{
int uid,printing=1,fd;
char searchstring[100];
if(argc > 1)
strcpy(searchstring,argv[1]);
else searchstring [0]=0;
uid=getuid();
fd=open(FILENAME,O_RDONLY);
if(fd==-1)
fatal("in main() while opening file! ");
while(printing)
printing=print_notes(fd,uid,searchstring);
printf("-----[end of note data]-------\n");
close(fd);
}
int print_notes(int fd, int uid, char *searchstr)
{
int note_lenght;
char byte=0,note_buffer[100];
note_lenght=find_usr_note(fd, uid);
if(note_lenght == -1)
return 0;
read(fd, note_buffer, note_lenght);
note_buffer[note_lenght]=0;
if(search_note(note_buffer,searchstr))
printf(note_buffer);
return 1;
}
int find_usr_note(int fd, int uid)
{
int note_uid=-1;
unsigned char byte;
int lenght;
while (note_uid!=uid) {
if(read(fd, ¬e_uid, 4)!=4)
return -1;
if(read(fd,&byte,1)!=1)
return -1;
byte=lenght=0;
while(byte!='\n') {
if(read(fd,&byte,1)!=1)
return -1;
lenght++;
}
}
lseek(fd,lenght*-1,SEEK_CUR);
printf("[DEBUG] found a %d byte note for user id %d\n", lenght, note_uid);
return lenght;
}
int search_note(char *note, char *keyword)
{
int i, key_lenght,match=0;
key_lenght=strlen(keyword);
if(key_lenght == 0)
return 1;
for(i=0; i < strlen(note); i++) {
if(note[i]==keyword[match])
match++;
else {
if(note[i]==keyword[0])
match=1;
else match=0;
}
if(match==key_lenght)
return 1;
}
return 0;
}
GRAZIE DELL'AIUTO!