Skip to content

Instantly share code, notes, and snippets.

@therealprocyon
Last active February 21, 2020 18:34
Show Gist options
  • Save therealprocyon/55046238fc5d2cbe666a877a6d0a63fe to your computer and use it in GitHub Desktop.
Save therealprocyon/55046238fc5d2cbe666a877a6d0a63fe to your computer and use it in GitHub Desktop.
terminote version 0.0.1 main file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <linux/limits.h>
// define function for the handling of a notition
void write_file(char *note_name);
int main(void)
{
/* allocates memory for a notition name and invoke function
* to handle the creation of the notition, afterwards it frees it
*/
char *path = malloc(sizeof(char)*PATH_MAX);
printf("Enter a notition name: ");
fgets(path,sizeof(path),stdin);
path[strlen(path)-1] = '\0';
write_file(path);
free(path);
return 0;
}
// declare the function to handle the notition
void write_file(char *note_name)
{
// create file
int notition_desc = open(note_name, O_WRONLY|O_CREAT|O_EXCL, 0644);
// if file exists ask what to do
if (notition_desc == -1 && errno == EEXIST)
{
//TODO finish handler for asking what to do
/* printf("The file exists, what would you like to do?\n");
printf("[0] Cancel file operations\n");
printf("[1] Append new data to the file\n");
printf("[2] Overwrite the file with new data\n");
printf("Your choice: ");
char buf[1];
*/
// print an error if the file exists
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
} else if (notition_desc == -1) {
// print an error if an error occurs
fprintf(stderr, "Error opening file: %s\n", strerror(errno));
} else {
// open the file for writing or appending data
FILE *notition = fopen(note_name, "a");
// append data
fprintf(notition, "This is a test");
// close the file after writing
if (notition != NULL)
{
fclose(notition);
}
}
// close the file descriptor
close(notition_desc);
}
/* printf("Terminote version 0.0.1 Alpine Antler\n\n");
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment