Clone C from hackertyper.net – Review of Stack Exchange Code

This is a simple clone of the hackertyper.net website. The code can be found at github.com/Hurricane996/hackertyper. Here is a local copy.

PREFIX = / usr / local
BINDIR = $ (PREFIX) / bin
DATAROOTDIR = $ (PREFIX) / share
DATADIR = $ (DATAROOTDIR)
MAN1DIR = $ (DATAROOTDIR) / man / man1

CFLAGS = -Wall -pedantic -std = c99
LDFLAGS = -lncurses

all: hackertyper

PHONE: everything clean install

hackertyper: hackertyper.o
cc -o $ @ $ ^ $ (LDFLAGS)
hackertyper.o: src / hackertyper.c
cc -c $ (CFLAGS) -o $ @ $ ^
src / hackertyper.c: src / hackertyper.h
src / hackertyper.h: src / hackertyper.h.in
thirst "s @% datadir% @ $ (DATADIR) @g" src / hackertyper.h.in> src / hackertyper.h

clean:
rm -f hackertyper {, .o} src / hackertyper.h

install:
install -D -m644 data / hackertyper.txt $ (DATADIR) /hackertyper.txt
install -D -m644 man / hackertyper.1 $ (MAN1DIR) /hackertyper.1
install -D -m755 hackertyper $ (BINDIR) / hackertyper
#include "hackertyper.h"

char * file name;
FILE * file;


char chars_per_nl = 1;

int main (int argc, char * argv[]) {
parse_args (argc, argv);

if (open_file (file name) == -1) {

returns -1;
}

in that();

int clear_msg_flag = 0;

nc_color_green ();

int running = 1;

while running) {
// buffer here so that the buffer occurs before clear_msg
int input_ch = getch ();
// the message was drawn the last time and we need to delete it
if (clear_msg_flag) {
clear_msg_flag = 0;
clear_msg ();
}

switch (input_ch) {
// C-c
Case 3:
running = 0;
break;
//CD
Case 4:
Clear();
nc_color_red ();
draw_msg ("ACCESS DENIED");
clear_msg_flag = 1;
break;
// C-g
Case 7:
Clear();
nc_color_green ();
draw_msg ("ACCESS GRANTED");

clear_msg_flag = 1;

break;
KEY_BACKSPACE case:
recoil ();
break;
default:
for (int i = 0; i < 5; i++) {
        int output_ch = fgetc(file);

        if(output_ch == EOF) {
          rewind(file);

          output_ch = fgetc(file);
        }

        if(output_ch != 'r')
          addch(output_ch);
      }

      refresh();
      break;
    }
  }

  end();
}

void parse_args(int argc, char* argv[]) {
  if(argc > one) {
for (int i = 0; i < argc; i++) {
      if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
        printf(HELP_TEXT);

        exit(0);
      }

      if(strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
        printf(VERSION_TEXT);

        exit(0);
      }

      if(strcmp(argv[i], "-f") == 0) {
        if(i+1 >= argc) {
fprintf (stderr, HELP_TEXT);

output (-1);
}

filename = argv[i+1];
}
}
}
}

int open_file (char * filename) {
File name = file name? filename: default filename;
file = fopen (file name, "r");

returns the file == NULL? -1: 0;
}

void init () {
initscr ();
raw();
noecho ();
scrollok (stdscr, true);
keyboard (stdscr, true);

// check the line ends
// ALL: expand this to work with endings in addition to  n and  r  n

char ch;

while (ch! = & # 39;  n & # 39; &&&! = & # 39;  r & # 39;) {
ch = fgetc (file);
}

yes (ch == & # 39;  r & # 39;) {
chars_per_nl = 2;
}

rewind (file);

if (has_colors ()) {
start_color ();
init_pair (1, COLOR_GREEN, COLOR_BLACK);
init_pair (2, COLOR_RED, COLOR_BLACK);
}
}

void nc_color_green () {
if (has_colors ()) {
attroff (COLOR_PAIR (2));
attron (COLOR_PAIR (1));
}
}

void nc_color_red () {
if (has_colors ()) {
attroff (COLOR_PAIR (1));
attron (COLOR_PAIR (2));
}
}

void nc_color_default () {
if (has_colors ()) {
attroff (COLOR_PAIR (1));
attroff (COLOR_PAIR (2));
}
}

void backspace () {
fseek (file, -1, SEEK_CUR);

int x, y;
getyx (stdscr, y, x);

yes (x == 0) {

yes (y == 0) {

he came back;
}

x = getmaxx (stdscr);
// set x to x minus 1
move (- and, - x);

char ch = & # 39; & # 39 ;;

while (ch == & # 39; & # 39; && x! = 0) {
move (y, - x);
ch = inch ();
}

fseek (file, -chars_per_nl, SEEK_CUR);
} else {
move (y, x-1);

}


delch ();
}

void draw_msg (char * msg) {
int len ​​= strlen (msg);
no sign char hash = & # 39; # & # 39 ;;
space without sign = & # 39; & # 39 ;;

int w;
int h;

getmaxyx (stdscr, h, w);

move (h / 2 - 2, w / 2 - len / 2 - 3);

for (int i = 0; i <len + 6; i ++)
addch (hash);

move (h / 2 - 1, w / 2 - len / 2 - 3);

addch (hash);

for (int i = 0; i <len + 4; i ++)
addch (space);

addch (hash);

move (h / 2, w / 2 - len / 2 - 3);
printw ("#% s #", msg);

move (h / 2 + 1, w / 2 - len / 2 - 3);
addch (hash);

for (int i = 0; i <len + 4; i ++)
addch (space);

addch (hash);

move (h / 2 + 2, w / 2 - len / 2 - 3);

for (int i = 0; i <len + 6; i ++)
addch (hash);
}

void clear_msg () {
nc_color_green ();
Clear();
move (0, 0);
// search file to the next line
int ch = 0;

while (ch! = & # 39;  n & # 39;) {
ch = fgetc (file);
}
}

void end () {
endwin ();
fclose (file);
}
#include 
#include 
#include 
#include 

#define default_filename "% datadir% / hackertyper.txt"

#define HELP_TEXT "
Usage: hackertyper [-f file] [-h] [-v]north
Print the text from the file to the standard output by pressing the keys, similar to the behavior of the website https: //www.hackertyper.org/  n 
north
Options:  n 
-f FILE, - filename FILE Prints text from FILE instead of from the default file  n 
-h, --help Print this help string  n 
-v, --version Print version information  n 
"

#define VERSION_TEXT "
hackertyper 2.1  n 
Copyright (C) 2019 Lani Willrich  n 
GPLv3 + license: GNU GPL version 3 or later north
This is free software: you can change it and redistribute it.  N 
THERE IS NO WARRANTY, to the extent permitted by law.  N 
Written by Lani Willrich  n 
"

void parse_args (int argc, char * argv[]);

int open_file (char * filename);

void init ();
void nc_color_red ();
void nc_color_green ();
void nc_color_default ();

void backspace ();

void draw_msg (char * msg);
void clear_msg ();

void end ();

Some irrelevant files have not been included in the list, including an automatically generated manager and data files