Skip to content

Instantly share code, notes, and snippets.

@Sam-Belliveau
Last active October 20, 2019 23:42
Show Gist options
  • Select an option

  • Save Sam-Belliveau/55115aba98eefe7c56e79f2d6591758f to your computer and use it in GitHub Desktop.

Select an option

Save Sam-Belliveau/55115aba98eefe7c56e79f2d6591758f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#define BUFFER_SIZE 0x10000
int write_getline(FILE* file)
{
// buffer
static char buffer[BUFFER_SIZE];
// buffer information
char* bufferptr = &buffer[0];
size_t buffersize = BUFFER_SIZE;
// return result from getline
int result;
// print data
result = getline(&bufferptr, &buffersize, stdin);
// if any data is given
if(1 < result)
{
fputs(bufferptr, file);
return 1;
}
else { return 0; }
}
int main(int argc, char** argv)
{
// open stream
FILE* stream;
// check if arguments are working
if(argc == 2)
{
// open file
stream = fopen(argv[1], "a");
// check if file is open
if(stream != NULL)
{
printf("Successfully Opened Stream \"%s\"\n", argv[1]);
fprintf(stream, "\nUser \"%s\" Has Just Connected To This Stream! (%s)\n", getenv("USER"), argv[1]);
// create shell like interface
do { printf("%s> ", argv[1]); }
while(write_getline(stream));
// close file
fclose(stream);
}
// printe error if file cant be opened
else { printf("Unable to open stream! (%s)\n", argv[1]); }
}
// print error if invalid arguments are given
else
{
printf("Invalid Arguments!\n");
printf("Use: %s <stream name>\n", argv[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment