trojanxem

Untitled

Aug 18th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7.  
  8. void usage( char *prog_name, char *filename)
  9. {
  10.      printf("Sposob uzycia %s <dane do dopisania do pliku %s> \n", prog_name,filename);
  11.      getch();
  12.      exit(0);
  13.      }
  14. void fatal(char *);
  15. void *ec_malloc(unsigned int);
  16.      
  17. int main(int argc, char *argv[])
  18. {
  19.     int fd;
  20.     char *buffer, *datafile;
  21.     buffer = (char *) ec_malloc(100);
  22.     datafile = (char *) ec_malloc(20);
  23.     strcpy(datafile, "/games/notes");
  24.     if ( argc < 2 )
  25.     usage(argv[0], datafile);
  26.     strcpy(buffer,argv[1]);
  27.     printf("[DEBUG] buffer ( bufor ) @ %p: \'%s'\ \n", buffer, buffer);
  28.     printf("[DEBUG] datafile ( plik z danymi ) @ %p: \'%s'\ '\n", datafile, datafile);
  29.     strncat(buffer, "\n", 1);
  30.     fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
  31.     if(fd == -1 )
  32.     fatal("w main() przy otwieraniu pliku \n");
  33.     printf("[DEBUG] deskryptor pliku to %d \n", fd);
  34.     if(write(fd,buffer,strlen(buffer)) == -1 )
  35.     fatal("w main() error\n");
  36.     if(close(fd) == -1 )
  37.     fatal("w main() przy zamykaniu\n");
  38.     printf("Notatka zapisana\n");
  39.     free(buffer);
  40.     free(datafile);
  41. }
  42. void vatal( char *message)
  43. {
  44.      char error_message[100];
  45.      strcpy(error_message, "[!!] Blad krytyczny ");
  46.      strncat(error_message, message, 83);
  47.      perror(error_message);
  48.      exit(-1);
  49.      
  50. }
  51. void *ec_malloc(unsigned int size)
  52. {
  53.      void *ptr;
  54.      ptr = malloc(size);
  55.      if(ptr == NULL)
  56.      fatal("w ec_malloc podczas alokacji pamieci");
  57.      return ptr;
  58.      }
Advertisement
Add Comment
Please, Sign In to add comment