#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "hacking.h"
#define FILENAME "/var/notes"
int print_notes(int, int, char *);
int find_user_note(int, int);
int search_note(char *, char *);
void fatal(char *);
int main(int argc, char *argv[])
{
int userid, fd, printing = 1;
char searchstring[100];
if(argc > 1)
strcpy(searchstring, argv[1]);
else
searchstring[0] = 0;
printf("[DEBUG]: searchstring in main = %s\n", searchstring);
userid = getuid();
fd = open(FILENAME, O_RDONLY);
if(fd == -1)
fatal("in main() while opening file for reading");
while(printing)
printing = print_notes(fd, userid, searchstring);
printf("-------[end of note data]-------\n");
return 0;
}
int print_notes(int fd, int uid, char *searchstring)
{
int note_length;
char note_buffer[100];
note_length = find_user_note(fd, uid);
if(note_length == -1)
return 0;
read(fd, note_buffer, note_length);
note_buffer[note_length] = 0;
if(search_note(note_buffer, searchstring))
printf(note_buffer);
return 1;
}
int find_user_note(int fd, int uid)
{
int note_uid = -1;
int length;
unsigned char byte;
while(note_uid != uid)
{
if(read(fd, ¬e_uid, 4) != 4)
return -1;
if(read(fd, &byte, 1) != 1)
return -1;
byte = length = 0;
while(byte != '\n')
{
if(read(fd, &byte, 1) != 1)
return -1;
length++;
}
}
lseek(fd, length * -1, SEEK_CUR);
printf("[DEBUG]: found a %d byte note for user id %d\n", length, uid);
return length;
}
int search_note(char *note, char *keyword)
{
int i, keyword_length, match = 0;
keyword_length = strlen(keyword);
if(keyword_length == 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 == keyword_length)
return 1;
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHVuaXN0ZC5oPgojaW5jbHVkZSA8ZmNudGwuaD4KI2luY2x1ZGUgPHN5cy9zdGF0Lmg+CiNpbmNsdWRlICJoYWNraW5nLmgiCgojZGVmaW5l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