mem.c 581 B

123456789101112131415161718192021222324252627282930
  1. #include <malloc.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. int main(int argc, char **argv)
  7. {
  8. /* Apre il file con il nome con cui e' stato invocato questo programma */
  9. int f = open(argv[0], O_RDONLY);
  10. struct stat s;
  11. int *buf;
  12. if (f == -1) {
  13. fprintf(stderr, "Errore apertura file\n");
  14. return 1;
  15. }
  16. if (fstat(f, &s) == -1) {
  17. fprintf(stderr, "Errore fstat\n");
  18. return 2;
  19. }
  20. if (!(buf = malloc(s.st_size))) {
  21. fprintf(stderr, "Errore malloc\n");
  22. return 3;
  23. }
  24. read(f, buf, s.st_size);
  25. free(buf);
  26. close(f);
  27. return 0;
  28. }