Replace the local getname() and getuserid() functions with calls
to user_from_uid(3) and uid_from_user(3).  This requires sprinkling
const in a few places but is otherwise mechanical.

 - todd

Index: usr.bin/mail/Makefile
===================================================================
RCS file: /cvs/src/usr.bin/mail/Makefile,v
retrieving revision 1.11
diff -u -p -u -r1.11 Makefile
--- usr.bin/mail/Makefile       30 Mar 2016 06:38:46 -0000      1.11
+++ usr.bin/mail/Makefile       13 Sep 2018 16:25:54 -0000
@@ -2,7 +2,7 @@
 
 PROG=  mail
 SRCS=  version.c aux.c cmd1.c cmd2.c cmd3.c cmdtab.c collect.c \
-       edit.c fio.c getname.c head.c v7.local.c lex.c list.c main.c names.c \
+       edit.c fio.c head.c v7.local.c lex.c list.c main.c names.c \
        popen.c quit.c send.c strings.c temp.c tty.c vars.c
 SFILES=        mail.help mail.tildehelp
 EFILES=        mail.rc
Index: usr.bin/mail/aux.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/aux.c,v
retrieving revision 1.29
diff -u -p -u -r1.29 aux.c
--- usr.bin/mail/aux.c  16 Oct 2015 17:56:07 -0000      1.29
+++ usr.bin/mail/aux.c  13 Sep 2018 15:38:59 -0000
@@ -45,7 +45,7 @@ static char *save2str(char *, char *);
  * Return a pointer to a dynamic copy of the argument.
  */
 char *
-savestr(char *str)
+savestr(const char *str)
 {
        char *new;
        int size = strlen(str) + 1;
Index: usr.bin/mail/extern.h
===================================================================
RCS file: /cvs/src/usr.bin/mail/extern.h,v
retrieving revision 1.28
diff -u -p -u -r1.28 extern.h
--- usr.bin/mail/extern.h       20 Jan 2015 16:59:07 -0000      1.28
+++ usr.bin/mail/extern.h       13 Sep 2018 16:27:43 -0000
@@ -35,7 +35,7 @@
 
 struct name;
 struct name *cat(struct name *, struct name *);
-struct name *delname(struct name *, char *);
+struct name *delname(struct name *, const char *);
 struct name *elide(struct name *);
 struct name *extract(char *, int);
 struct grouphead;
@@ -55,7 +55,6 @@ char  *copyin(char *, char **);
 char   *detract(struct name *, int);
 char   *expand(char *);
 char   *getdeadletter(void);
-char   *getname(uid_t);
 struct message;
 char   *hfield(char *, struct message *);
 FILE   *infix(struct header *, FILE *);
@@ -67,12 +66,13 @@ char        *readtty(char *, char *);
 char   *reedit(char *);
 FILE   *run_editor(FILE *, off_t, int, int);
 char   *salloc(int);
-char   *savestr(char *);
+char   *savestr(const char *);
 FILE   *setinput(struct message *);
 char   *skin(char *);
 char   *skip_comment(char *);
 char   *snarf(char *, int *);
-char   *username(void);
+const char
+       *username(void);
 char   *value(char *);
 char   *vcopy(char *);
 char   *yankword(char *, char *);
@@ -119,7 +119,7 @@ void         fail(char *, char *);
 int     file(void *);
 struct grouphead *
         findgroup(char *);
-void    findmail(char *, char *, int);
+void    findmail(const char *, char *, int);
 void    fioint(int);
 int     first(int, int);
 void    fixhead(struct header *, struct name *);
@@ -134,7 +134,6 @@ int  gethfield(FILE *, char *, int, char
 int     gethfromtty(struct header *, int);
 int     getmsglist(char *, int *, int);
 int     getrawlist(char *, char **, int);
-uid_t   getuserid(char *);
 int     grabh(struct header *, int);
 int     group(void *);
 int     hash(char *);
Index: usr.bin/mail/getname.c
===================================================================
RCS file: usr.bin/mail/getname.c
diff -N usr.bin/mail/getname.c
--- usr.bin/mail/getname.c      27 Oct 2009 23:59:40 -0000      1.8
+++ /dev/null   1 Jan 1970 00:00:00 -0000
@@ -1,64 +0,0 @@
-/*     $OpenBSD: getname.c,v 1.8 2009/10/27 23:59:40 deraadt Exp $     */
-/*     $NetBSD: getname.c,v 1.4 1996/06/08 19:48:23 christos Exp $     */
-
-/*
- * Copyright (c) 1980, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include "rcv.h"
-#include <pwd.h>
-#include "extern.h"
-
-/* Getname / getuserid for those with hashed passwd data base. */
-
-/*
- * Search the passwd file for a uid.  Return name on success, NULL on failure
- */
-char *
-getname(uid_t uid)
-{
-       struct passwd *pw;
-
-       if ((pw = getpwuid(uid)) == NULL)
-               return(NULL);
-       return(pw->pw_name);
-}
-
-/*
- * Convert the passed name to a user id and return it.  Return -1
- * on error.
- */
-uid_t
-getuserid(char *name)
-{
-       struct passwd *pw;
-
-       if ((pw = getpwnam(name)) == NULL)
-               return(UID_MAX);
-       return(pw->pw_uid);
-}
Index: usr.bin/mail/glob.h
===================================================================
RCS file: /cvs/src/usr.bin/mail/glob.h,v
retrieving revision 1.8
diff -u -p -u -r1.8 glob.h
--- usr.bin/mail/glob.h 24 Nov 2014 20:01:43 -0000      1.8
+++ usr.bin/mail/glob.h 13 Sep 2018 15:38:59 -0000
@@ -54,7 +54,8 @@ FILE  *input;                         /* Current command input
 char   mailname[PATHSIZE];             /* Name of current file */
 char   prevfile[PATHSIZE];             /* Name of previous file */
 char   *homedir;                       /* Path name of home directory */
-char   *myname;                        /* My login name */
+const char
+       *myname;                        /* My login name */
 off_t  mailsize;                       /* Size of system mailbox */
 int    lexnumber;                      /* Number of TNUMBER from scan() */
 char   lexstring[STRINGLEN];           /* String from TSTRING, scan() */
Index: usr.bin/mail/lex.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/lex.c,v
retrieving revision 1.39
diff -u -p -u -r1.39 lex.c
--- usr.bin/mail/lex.c  16 Oct 2015 17:56:07 -0000      1.39
+++ usr.bin/mail/lex.c  13 Sep 2018 15:38:59 -0000
@@ -58,7 +58,7 @@ setfile(char *name)
        int i, fd;
        struct stat stb;
        char isedit = *name != '%';
-       char *who = name[1] ? name + 1 : myname;
+       const char *who = name[1] ? name + 1 : myname;
        char tempname[PATHSIZE];
        static int shudclob;
 
Index: usr.bin/mail/names.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/names.c,v
retrieving revision 1.23
diff -u -p -u -r1.23 names.c
--- usr.bin/mail/names.c        16 Oct 2015 17:56:07 -0000      1.23
+++ usr.bin/mail/names.c        13 Sep 2018 15:38:59 -0000
@@ -582,7 +582,7 @@ count(struct name *np)
  * Delete the given name from a namelist.
  */
 struct name *
-delname(struct name *np, char *name)
+delname(struct name *np, const char *name)
 {
        struct name *p;
 
Index: usr.bin/mail/temp.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/temp.c,v
retrieving revision 1.17
diff -u -p -u -r1.17 temp.c
--- usr.bin/mail/temp.c 28 Jul 2016 21:37:45 -0000      1.17
+++ usr.bin/mail/temp.c 13 Sep 2018 16:28:28 -0000
@@ -31,6 +31,7 @@
  */
 
 #include "rcv.h"
+#include <pwd.h>
 #include "extern.h"
 
 /*
@@ -62,15 +63,17 @@ tinit(void)
         * do a spreserve() after us.
         */
        if (myname != NULL) {
-               if (getuserid(myname) == UID_MAX)
+               uid_t uid;
+
+               if (uid_from_user(myname, &uid) == -1)
                        errx(1, "\"%s\" is not a user of this system", myname);
        } else {
-               if ((cp = username()) == NULL) {
+               if ((myname = username()) == NULL) {
                        myname = "nobody";
                        if (rcvmode)
                                exit(1);
                } else
-                       myname = savestr(cp);
+                       myname = savestr(myname);
        }
        if ((cp = getenv("HOME")) == NULL || *cp == '\0' ||
            strlen(cp) >= PATHSIZE)
Index: usr.bin/mail/v7.local.c
===================================================================
RCS file: /cvs/src/usr.bin/mail/v7.local.c,v
retrieving revision 1.17
diff -u -p -u -r1.17 v7.local.c
--- usr.bin/mail/v7.local.c     19 Jul 2016 06:43:27 -0000      1.17
+++ usr.bin/mail/v7.local.c     13 Sep 2018 16:27:17 -0000
@@ -41,6 +41,7 @@
 #include "rcv.h"
 #include <stdlib.h>
 #include <fcntl.h>
+#include <pwd.h>
 #include "extern.h"
 
 /*
@@ -48,7 +49,7 @@
  * mail is queued).
  */
 void
-findmail(char *user, char *buf, int buflen)
+findmail(const char *user, char *buf, int buflen)
 {
        char *mbox;
        struct stat sb;
@@ -82,20 +83,20 @@ demail(void)
 /*
  * Discover user login name.
  */
-char *
+const char *
 username(void)
 {
-       char *np;
+       const char *np;
        uid_t uid;
 
        if ((np = getenv("USER")) != NULL)
                return(np);
        if ((np = getenv("LOGNAME")) != NULL)
                return(np);
-       if ((np = getname(uid = getuid())) != NULL)
+       if ((np = user_from_uid(uid = getuid(), 1)) != NULL)
                return(np);
        if ((np = getlogin()) != NULL)
                return(np);
-       printf("Cannot associate a name with uid %u\n", (unsigned)uid);
+       printf("Cannot associate a name with uid %u\n", uid);
        return(NULL);
 }

Reply via email to