forked from cmccabe/linkulator
Added logic for each of the two main use cases
This commit is contained in:
parent
50aadd67db
commit
b782932044
48
writer.c
48
writer.c
|
@ -8,6 +8,9 @@
|
|||
#define DIRPERMISSIONS 0700
|
||||
#define FILEPERMISSIONS 0655
|
||||
|
||||
// function that writes files from a string input
|
||||
int storeinput (char * storelocation, char * inputstring);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// ensure proper usage of the application when executed
|
||||
|
@ -53,32 +56,53 @@ int main(int argc, char *argv[])
|
|||
// create the new link directory from the dirname supplied
|
||||
if (mkdir(argv[2], DIRPERMISSIONS) == -1)
|
||||
{
|
||||
perror("Invalid dirname ");
|
||||
perror("Invalid dirname for new link ");
|
||||
return 1;
|
||||
}
|
||||
// TODO: create, write argv[3], argv[4], argv[5], argv[6]
|
||||
|
||||
// create, write and close the files for argv[3], argv[4], argv[5], argv[6] in directory argv[2]
|
||||
// TODO: the store location needs to be the directory name and the file name concatenated. Currently this only creates files in the current directory which is wrong.
|
||||
// TODO: These need to return 1 upon failure i.e. if (storeinput(..) == 1 { print error and return } and eventually do cleanup
|
||||
storeinput("link", argv[3]);
|
||||
storeinput("title", argv[4]);
|
||||
storeinput("description", argv[5]);
|
||||
storeinput("keywords", argv[6]);
|
||||
|
||||
|
||||
}
|
||||
if (argc == 4) //reply
|
||||
{
|
||||
// TODO: create, write argv[3] with appropriate file name
|
||||
// create, write and close the file argv[3] located in argv[2]
|
||||
storeinput(argv[2], argv[3]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
|
||||
// function for creating, writing and storing files
|
||||
// TODO: I don't know if the declarations char * storelocation, char * inputstring) are correct. I think it might be? It works.
|
||||
int storeinput (char * storelocation, char * inputstring)
|
||||
{
|
||||
int fdfile = creat(storelocation, FILEPERMISSIONS);
|
||||
if (fdfile == -1)
|
||||
{
|
||||
perror("Invalid file ");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// write to file "fdfile"
|
||||
if (write(fdfile, argv[3], strlen(argv[3])) == -1)
|
||||
if (write(fdfile, inputstring, strlen(inputstring)) == -1)
|
||||
{
|
||||
perror("Invalid write ");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// close the file
|
||||
if (close(fdfile) == -1)
|
||||
{
|
||||
perror("Invalid close ");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// success
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue