Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- void usage( char *prog_name, char *filename)
- {
- printf("Sposob uzycia %s <dane do dopisania do pliku %s> \n", prog_name,filename);
- getch();
- exit(0);
- }
- void fatal(char *);
- void *ec_malloc(unsigned int);
- int main(int argc, char *argv[])
- {
- int fd;
- char *buffer, *datafile;
- buffer = (char *) ec_malloc(100);
- datafile = (char *) ec_malloc(20);
- strcpy(datafile, "/games/notes");
- if ( argc < 2 )
- usage(argv[0], datafile);
- strcpy(buffer,argv[1]);
- printf("[DEBUG] buffer ( bufor ) @ %p: '%s' \n", buffer, buffer);
- printf("[DEBUG] datafile ( plik z danymi ) @ %p: '%s' '\n", datafile, datafile);
- strncat(buffer, "\n", 1);
- fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
- if(fd == -1 )
- fatal("w main() przy otwieraniu pliku \n");
- printf("[DEBUG] deskryptor pliku to %d \n", fd);
- if(write(fd,buffer,strlen(buffer)) == -1 )
- fatal("w main() error\n");
- if(close(fd) == -1 )
- fatal("w main() przy zamykaniu\n");
- printf("Notatka zapisana\n");
- free(buffer);
- free(datafile);
- return 0;
- }
- void fatal( char *message)
- {
- char error_message[100];
- strcpy(error_message, "[!!] Blad krytyczny ");
- strncat(error_message, message, 83);
- perror(error_message);
- exit(-1);
- }
- void *ec_malloc(unsigned int size)
- {
- void *ptr;
- ptr = malloc(size);
- if(ptr == NULL)
- fatal("w ec_malloc podczas alokacji pamieci");
- return ptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment