Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include "hacking.h"
  7.  
  8. void usage(char *prog_name, char *filename) {
  9. printf("Usage: %s <data to add to %s>\n", prog_name, filename);
  10. exit(0);
  11. }
  12.  
  13. void fatal(char *); // a function for fatal errors
  14. void *ec_malloc(unsigned int); // an errorchecked malloc() wrapper
  15.  
  16. int main(int argc, char *argv[]) {
  17. int userid, fd; // file descriptor
  18. char *buffer, *datafile;
  19.  
  20. buffer = (char *) ec_malloc(100);
  21. datafile = (char *) ec_malloc(20);
  22. strcpy(datafile, "/var/notes");
  23.  
  24. if(argc < 2) // If there aren't commandline arguments
  25. usage(argv[0], datafile); // display usage message and exit
  26.  
  27. strcpy(buffer, argv[1]); // copy into buffer
  28.  
  29. printf("[DEBUG] buffer @ %p: \'%s\'\n", buffer, buffer);
  30. printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);
  31.  
  32. // Opening the file
  33. fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
  34. if(fd == -1)
  35. fatal("in main() while opening file");
  36. printf("[DEBUG] file descriptor is %d\n", fd);
  37.  
  38. userid = getuid(); // get the real user ID
  39.  
  40. // Writing data
  41. if(write(fd, &userid, 4) == -1) // write user ID before note data
  42. fatal("in main() while writing userid to file");
  43. write(fd, "\n", 1); // terminate line
  44.  
  45. if(write(fd, buffer, strlen(buffer)) == -1) // write note
  46. fatal("in main() while writing buffer to file");
  47. write(fd, "\n", 1); // terminate line
  48.  
  49. // Closing file
  50. if(close(fd) == -1)
  51. fatal("in main() while closing file");
  52.  
  53. printf("Note has been saved.\n");
  54. free(buffer);
  55. free(datafile);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement