Skip to content

Instantly share code, notes, and snippets.

@OsandaMalith
Last active August 29, 2015 14:20
Show Gist options
  • Save OsandaMalith/57eeaf1ec7b15f457fa0 to your computer and use it in GitHub Desktop.
Save OsandaMalith/57eeaf1ec7b15f457fa0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#define MAX 65535
// https://github.com/OsandaMalith
void banner() {
static const char *banner =
"\t+------------------------+\n"
"\t+---Simple Note Taker----+\n"
"\t+-Coded by @OsandaMalith-+\n"
"\t+------------------------+\n\n";
for(banner; *banner; ++banner) fprintf(stdout, "%c", *banner);
}
void fatal(char *msg) {
char err_msg[100];
strcpy(err_msg, "[!!] Fatal Error ");
strncat(err_msg, msg, 75);
perror(err_msg);
exit(-1);
}
void *ecalloc(unsigned int size) {
void *ptr = calloc(size, sizeof(char));
if(!ptr) fatal("in emalloc() on memory allocation");
return ptr;
}
void usage(char *appname) {
printf("Usage: %s <path name> <data to add>\n", appname);
exit(0);
}
int main (int argc, char *argv[]) {
banner();
char *path = (char *) ecalloc(MAX),
*data = (char *) ecalloc(MAX);
if(argc < 2) usage(argv[0]);
strcpy(path, argv[1]);
strcpy(data, argv[2]);
printf("[iNFO] path -> 0x%p: \'%s\'\n", path, path);
printf("[iNFO] data -> 0x%p: \'%s\'\n", data, data);
strncat(data, "\n", 1);
int fd = open(path, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
if(fd == -1) fatal("in main() while opening file");
printf("[iNFO] File Descriptor is %d\n", fd);
if(write(fd, data, strlen(data)) == -1)
fatal("in main() while writing buffer to file");
if(close(fd) == -1) fatal("in main() while closing file");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment