Bring back some C89/C90 elements

dmesg: don't use VLAs
getty, su: no need to use compound literals
This commit is contained in:
FRIGN 2014-06-05 11:28:08 +01:00 committed by sin
parent 8ab8a4cad0
commit 5a2f3c85b7
3 changed files with 8 additions and 5 deletions

View File

@ -80,12 +80,13 @@ static int
dmesg_show(int fd, const void *buf, size_t n) dmesg_show(int fd, const void *buf, size_t n)
{ {
int last = '\n'; int last = '\n';
char newbuf[n], *q = newbuf; char *newbuf, *q;
const char *p = buf; const char *p = buf;
ssize_t r; ssize_t r;
size_t i; size_t i;
memset(newbuf, 0, n); newbuf = calloc(n, sizeof(char));
q = newbuf;
for (i = 0; i < n; ) { for (i = 0; i < n; ) {
if (last == '\n' && p[i] == '<') { if (last == '\n' && p[i] == '<') {
i += 2; i += 2;
@ -97,6 +98,7 @@ dmesg_show(int fd, const void *buf, size_t n)
last = p[i++]; last = p[i++];
} }
r = write(fd, newbuf, n); r = write(fd, newbuf, n);
free(newbuf);
if(r < 0 || (size_t)r != n) if(r < 0 || (size_t)r != n)
return -1; return -1;
if (last != '\n') if (last != '\n')

View File

@ -109,5 +109,5 @@ main(int argc, char *argv[])
eprintf("login name cannot start with '-'\n"); eprintf("login name cannot start with '-'\n");
if (logname[0] == '\0') if (logname[0] == '\0')
return EXIT_FAILURE; return EXIT_FAILURE;
return execvp("/bin/login", (char *[]){ "login", "-p", logname, NULL }); return execlp("/bin/login", "login", "-p", logname, NULL);
} }

5
su.c
View File

@ -30,9 +30,9 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *usr = "root", *pass, *cryptpass; char *usr = "root", *pass, *cryptpass;
char * const *newargv;
struct spwd *spw; struct spwd *spw;
struct passwd *pw; struct passwd *pw;
char *newargv[2];
uid_t uid; uid_t uid;
ARGBEGIN { ARGBEGIN {
@ -120,7 +120,8 @@ dosu:
if (lflag) { if (lflag) {
return dologin(pw); return dologin(pw);
} else { } else {
newargv = (char *const[]){pw->pw_shell, NULL}; newargv[0] = pw->pw_shell;
newargv[1] = NULL;
if (!pflag) { if (!pflag) {
setenv("HOME", pw->pw_dir, 1); setenv("HOME", pw->pw_dir, 1);
setenv("SHELL", pw->pw_shell, 1); setenv("SHELL", pw->pw_shell, 1);