fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include "hacking.h"
  7.  
  8. #define FILENAME "/var/notes"
  9.  
  10. int print_notes(int, int, char *);
  11. int find_user_note(int, int);
  12. int search_note(char *, char *);
  13. void fatal(char *);
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. int userid, fd, printing = 1;
  18. char searchstring[100];
  19.  
  20. if(argc > 1)
  21. strcpy(searchstring, argv[1]);
  22. else
  23. searchstring[0] = 0;
  24.  
  25. printf("[DEBUG]: searchstring in main = %s\n", searchstring);
  26.  
  27. userid = getuid();
  28. fd = open(FILENAME, O_RDONLY);
  29. if(fd == -1)
  30. fatal("in main() while opening file for reading");
  31. while(printing)
  32. printing = print_notes(fd, userid, searchstring);
  33. printf("-------[end of note data]-------\n");
  34. return 0;
  35. }
  36.  
  37.  
  38. int print_notes(int fd, int uid, char *searchstring)
  39. {
  40. int note_length;
  41. char note_buffer[100];
  42.  
  43. note_length = find_user_note(fd, uid);
  44. if(note_length == -1)
  45. return 0;
  46.  
  47. read(fd, note_buffer, note_length);
  48. note_buffer[note_length] = 0;
  49.  
  50. if(search_note(note_buffer, searchstring))
  51. printf(note_buffer);
  52. return 1;
  53. }
  54.  
  55. int find_user_note(int fd, int uid)
  56. {
  57. int note_uid = -1;
  58. int length;
  59. unsigned char byte;
  60.  
  61. while(note_uid != uid)
  62. {
  63. if(read(fd, &note_uid, 4) != 4)
  64. return -1;
  65. if(read(fd, &byte, 1) != 1)
  66. return -1;
  67.  
  68. byte = length = 0;
  69. while(byte != '\n')
  70. {
  71. if(read(fd, &byte, 1) != 1)
  72. return -1;
  73. length++;
  74. }
  75. }
  76. lseek(fd, length * -1, SEEK_CUR);
  77.  
  78. printf("[DEBUG]: found a %d byte note for user id %d\n", length, uid);
  79. return length;
  80. }
  81.  
  82. int search_note(char *note, char *keyword)
  83. {
  84. int i, keyword_length, match = 0;
  85.  
  86. keyword_length = strlen(keyword);
  87. if(keyword_length == 0)
  88. return 1;
  89.  
  90. for(i = 0; i < strlen(note); i++)
  91. {
  92. if(note[i] == keyword[match])
  93. match++;
  94. else{
  95. if(note[i] == keyword[0])
  96. match = 1;
  97. else
  98. match = 0;
  99. }
  100. if(match == keyword_length)
  101. return 1;
  102. }
  103. return 0;
  104. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:21: fatal error: hacking.h: No such file or directory
compilation terminated.
stdout
Standard output is empty