Guest User

Untitled

a guest
Jun 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. int read_from_file(char *fname, char **content)
  2. {
  3. int fh;
  4. int outbytes, len;
  5. struct stat statbuf;
  6. char *buf;
  7.  
  8. if ( (fh=open(fname, O_RDONLY, 0)) == -1 )
  9. {
  10. /* Complain, explain */
  11. char msg[128];
  12. sprintf( msg, "failure opening file [%.64s]\n", fname );
  13. errorMessage( msg );
  14. return -1;
  15. }
  16.  
  17. /* get file size */
  18. fstat(fh, &statbuf);
  19. outbytes = statbuf.st_size;
  20.  
  21. /* read the file */
  22. buf = *content = (char *) malloc(outbytes+1);
  23. if ( (len = read(fh, buf, outbytes)) == -1 ) {
  24. char msg[128];
  25. sprintf( msg, "failure writing to file [%.64s]\n", fname );
  26. errorMessage( msg );
  27. return -1;
  28. }
  29. buf[outbytes] = '\0';
  30. return len;
  31. }
Add Comment
Please, Sign In to add comment