xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
Commit 7b7e81519dfe6f04cf31f01dba5caa4592682c39

Hostname memory leak fix.
Author: Martin Dvorak
Author date (UTC): 2014-02-08 18:49
Committer name: Martin Dvorak
Committer date (UTC): 2014-02-08 18:49
Parent(s): 269ccb8804659141fd710ac3cafe61f9a70c30d9
Signing key:
Tree: 152bd9e6cc52400fcbef4b37c4413c4f6c03e472
File Lines added Lines deleted
src/hstr.c 11 5
src/hstr_utils.c 8 10
src/include/hstr_utils.h 1 1
tests/monster/monster-hh-env.sh 2 0
File src/hstr.c changed (mode: 100644) (index fc115af..f3db5e8)
26 26 #include "include/hstr_utils.h" #include "include/hstr_utils.h"
27 27
28 28 #define SELECTION_CURSOR_IN_PROMPT -1 #define SELECTION_CURSOR_IN_PROMPT -1
29 #define SELECTION_PREFIX_MAX_LNG 500
29 #define SELECTION_PREFIX_MAX_LNG 512
30 30 #define CMDLINE_LNG 2048 #define CMDLINE_LNG 2048
31 #define HOSTNAME_BUFFER 128
32
31 33
32 34 #define Y_OFFSET_PROMPT 0 #define Y_OFFSET_PROMPT 0
33 35 #define Y_OFFSET_HELP 1 #define Y_OFFSET_HELP 1
 
... ... void get_env_configuration()
129 131
130 132 int print_prompt() int print_prompt()
131 133 { {
132 char *hostname = get_hostname();
133 134 char *user = getenv(ENV_VAR_USER); char *user = getenv(ENV_VAR_USER);
134 int xoffset = 0;
135 char *hostname=malloc(HOSTNAME_BUFFER);
136 int xoffset = 0, promptLength;
135 137
136 138 if(hicolor) { if(hicolor) {
137 139 color_attr_on(COLOR_PAIR(HH_COLOR_PROMPT)); color_attr_on(COLOR_PAIR(HH_COLOR_PROMPT));
138 140 color_attr_on(A_BOLD); color_attr_on(A_BOLD);
139 141 } }
140 mvprintw(Y_OFFSET_PROMPT, xoffset, "%s@%s$ ", (user?user:"me"), (hostname?hostname:"localhost"));
142 user=(user?user:"me");
143 get_hostname(HOSTNAME_BUFFER, hostname);
144 mvprintw(Y_OFFSET_PROMPT, xoffset, "%s@%s$ ", user, hostname);
145 promptLength=strlen(user)+1+strlen(hostname)+1+1;
146 free(hostname);
141 147 if(hicolor) { if(hicolor) {
142 148 color_attr_off(A_BOLD); color_attr_off(A_BOLD);
143 149 color_attr_off(COLOR_PAIR(HH_COLOR_PROMPT)); color_attr_off(COLOR_PAIR(HH_COLOR_PROMPT));
144 150 } }
145 151 refresh(); refresh();
146 152
147 return strlen(user)+1+strlen(hostname)+1+1;
153 return promptLength;
148 154 } }
149 155
150 156 void print_help_label() void print_help_label()
File src/hstr_utils.c changed (mode: 100644) (index dccbfab..830c6da)
11 11 #include <ctype.h> #include <ctype.h>
12 12
13 13 #define DEFAULT_COMMAND "pwd" #define DEFAULT_COMMAND "pwd"
14 #define HOSTNAME_BUFFER 100
15 14 #define PROC_HOSTNAME "/proc/sys/kernel/hostname" #define PROC_HOSTNAME "/proc/sys/kernel/hostname"
16 15
17 16 void tiocsti() void tiocsti()
 
... ... void reverse_char_pointer_array(char **array, unsigned length)
50 49 } }
51 50 } }
52 51
53 char *get_hostname()
52 void get_hostname(int bufferSize, char *buffer)
54 53 { {
55 char *b=malloc(HOSTNAME_BUFFER);
56 if(access( PROC_HOSTNAME, F_OK ) != -1) {
57 FILE *f = fopen(PROC_HOSTNAME, "r");
58 b=fgets(b, HOSTNAME_BUFFER, f);
59 fclose(f);
54 char *b=buffer;
55 if(access(PROC_HOSTNAME, F_OK) != -1) {
56 FILE *file = fopen(PROC_HOSTNAME, "r");
57 b=fgets(buffer, bufferSize, file);
58 fclose(file);
60 59 if(b) { if(b) {
61 60 b[strlen(b)-1]=0; b[strlen(b)-1]=0;
62 return b;
61 return;
63 62 } }
64 63 } }
65 strcpy(b,"localhost");
66 return b;
64 strcpy(buffer,"localhost");
67 65 } }
68 66
69 67 void toggle_case(char *str, bool lowercase) { void toggle_case(char *str, bool lowercase) {
File src/include/hstr_utils.h changed (mode: 100644) (index fa435c1..aa5733c)
23 23 void tiocsti(); void tiocsti();
24 24 void fill_terminal_input(char* cmd, bool padding); void fill_terminal_input(char* cmd, bool padding);
25 25 void reverse_char_pointer_array(char **array, unsigned length); void reverse_char_pointer_array(char **array, unsigned length);
26 char *get_hostname();
26 void get_hostname(int bufferSize, char *buffer);
27 27 void toggle_case(char *str, bool lowercase); void toggle_case(char *str, bool lowercase);
28 28
29 29 #endif #endif
File tests/monster/monster-hh-env.sh changed (mode: 100755) (index 95d30a0..051d80c)
... ... export HISTFILE=/home/dvorka/p/hstr/monster/.bash_history_50M
8 8 export HISTFILESIZE=10000000 export HISTFILESIZE=10000000
9 9 export HISTSIZE=1000000 export HISTSIZE=1000000
10 10
11 PS1="\$? \$(if [[ \$? == 0 ]]; then echo \"\[\033[0;32m\];)\"; else echo \"\[\033[0;31m\];(\"; fi)\[\033[00m\] : "
12
11 13 # eof # eof
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master