nmps

simple network-based command-line rpg wrote in c99
git clone git://git.kocotian.pl/nmps.git
Log | Files | Refs | LICENSE

commit 88ab7d5c017ecee756eba4efded037139b33df3c
parent 8adf475b82df6cd295f198e48e74df9f35280d57
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon,  1 Feb 2021 18:43:46 +0100

username fix

Diffstat:
Mnmps.c | 12+++++++-----
Mutil.c | 14++++++++++++++
Mutil.h | 1+
3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/nmps.c b/nmps.c @@ -14,7 +14,7 @@ #include "http.h" #include "util.c" -#define VERSION "a0.4-rc2" +#define VERSION "a0.4-rc3" extern void herror(const char *s); @@ -132,7 +132,7 @@ command(char *command, char *args, char *host, char *port, char *beforeOutput) static int gameplay(char *username, char *host, char *port) { - char line[4096], *linedup, *token, *cmd, + char line[4096]; char *linedup, *token, *cmd, *args; size_t argsize; int character, chiter = -1, commandret; @@ -144,8 +144,10 @@ gameplay(char *username, char *host, char *port) while ((character = getch(0)) != '\n') { switch (character) { case 127: - line[chiter--] = 0; - printf("\033[1D \033[1D"); + if (chiter >= 0) { + line[chiter--] = 0; + printf("\033[1D \033[1D"); + } break; case 12: printf("\033c\030"PS1); @@ -268,7 +270,7 @@ main(int argc, char *argv[]) if (username == NULL && defaultusername == NULL) { username = malloc(32); - getlogin_r(username, 32); + strncpy(username, getusername(), 32); } else if (username == NULL && defaultusername != NULL) { username = strdup(defaultusername); } diff --git a/util.c b/util.c @@ -1,5 +1,6 @@ /* See LICENSE_LIBSL file for copyright and license details. */ +#include <pwd.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -34,3 +35,16 @@ die(const char *fmt, ...) { exit(1); } + +const char * +getusername(void) +{ + uid_t uid = geteuid(); + struct passwd *pw = getpwuid(uid); + if (pw) + { + return pw->pw_name; + } + + return ""; +} diff --git a/util.h b/util.h @@ -6,3 +6,4 @@ void die(const char *fmt, ...); void *ecalloc(size_t nmemb, size_t size); +const char *getusername(void);