Initial commit of writer application, written in C. Incomplete.

This commit is contained in:
asdf 2019-03-03 00:06:47 +11:00
parent 048d0f98ff
commit 50aadd67db
1 changed files with 84 additions and 0 deletions

84
writer.c Normal file
View File

@ -0,0 +1,84 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h> //mkdir
#include <sys/types.h>
#include <fcntl.h> //creat
#include <unistd.h> //write
#define DIRPERMISSIONS 0700
#define FILEPERMISSIONS 0655
int main(int argc, char *argv[])
{
// ensure proper usage of the application when executed
// writer new dirname link title description keywords
// TODO: this could be a single if
if (argc != 7 && strcmp(argv[1], "new") == 0)
{
fprintf(stderr, "Usage: writer <action> <dirname> <data>\nActions: new or reply\nData: new - link title description keywords\nreply - comment\n");
return 1;
}
// writer reply dirname/filename comment
if (argc != 4 && strcmp(argv[1], "reply") == 0)
{
fprintf(stderr, "Usage: writer <action> <dirname/reply_id> <data>\nActions: new or reply\nData: new - link title description keywords\nreply - comment\n");
return 1;
}
//debug output displaying command line arguments
if (argc == 7)
{
printf("debug test input for new: %s, %s, %s, %s, %s\n", argv[2], argv[3], argv[4], argv[5], argv[6]);
}
if (argc == 4)
{
printf("debug test input for reply: %s, %s\n", argv[2], argv[3]);
}
// only accepts arguments of a certain length
// TODO validation needs to be as configured in config file, plus additional checks. might need to be a validator function
//for (int i = 1; i < 5; i++)
//{
// if (strlen(argv[i]) > 200)
// {
// fprintf(stderr, "Error: Arguments cannot be longer than 200 characters\n");
// return 1;
// }
//}
// action outcomes vary - new links create 4 files, replies create 1
if (argc == 7) //new
{
// create the new link directory from the dirname supplied
if (mkdir(argv[2], DIRPERMISSIONS) == -1)
{
perror("Invalid dirname ");
return 1;
}
// TODO: create, write argv[3], argv[4], argv[5], argv[6]
}
if (argc == 4) //reply
{
// TODO: create, write argv[3] with appropriate file name
}
// example file create action - creates file and returns file descriptor to a variable that is used by the write function
// TODO: I don't like this garbage if statement and variable declaration
// TODO: implement dirname + filename concatenation for creat function
int fdfile = 0;
if ((fdfile = creat("data/test", FILEPERMISSIONS))== -1)
{
perror("Invalid file ");
return 1;
}
// write to file "fdfile"
if (write(fdfile, argv[3], strlen(argv[3])) == -1)
{
perror("Invalid write ");
return 1;
}
}