Frasca, Mario writes:
> 
> Hi David,
> 
>        while (pwd != NULL) {
>          if (pwd->pw_uid == uid) {
> -          int i = strlen(nbuf1);
> -          if(nbuf1[i-1]=='\n')
> -            nbuf1[i-1] = 0;
> +          char *p=strchr(nbuf1,'\n');
> +          if(p) *p='\0';
>            pwd->pw_passwd = crypt(nbuf1, salt);
>          }  
> 
> yes, your suggestion is quite shorter.
> I just took the corresponding code from login.c without questioning its
> efficiency.  I'll never do it again ;-)
> 
> by the way: we had to correct login.c anyway: replace that `write-read` pair
> for prompting for a password with getpass(<prompt>); since we're busy on
> that source, we can as well change the \n-removal according to your
> suggestion.
> 

Okay I have finally got to the bottom of this. getpass() _should_ have done
the removal of the trailing \n itself, but there was a typo,

        n = strlen(result-1);

instead of

        n = strlen(result)-1;

but as David pointed out, this is not efficient code, so I replaced it with
his example, as shown above, fixed login to use getpass() instead of
read(), and removed the \n removing code from both login.c and passwd.c.

Everything now works as required. Here is the current diffs again the
version in CVS, which I am just about to commit.

Al

===================================================================
RCS file: /home/elks/cvs/elkscmd/sys_utils/getpass.c,v
retrieving revision 1.1.1.1
diff -u -d -r1.1.1.1 getpass.c
--- getpass.c   1999/02/16 18:23:00     1.1.1.1
+++ getpass.c   1999/12/09 11:17:17
@@ -63,10 +63,8 @@
     if (fgets(result, sizeof(result)-1, in) == NULL) {
         result[0] = 0;
     } else {
-        n = strlen(result-1);
-       if (result[n] == '\n') {
-           result[n] = 0;
-       }
+       char * p = strchr(result, '\n');
+       if(p) *p = '\0';
     }
 
     /* reset our terminal, if necessary */
Index: login.c
===================================================================
RCS file: /home/elks/cvs/elkscmd/sys_utils/login.c,v
retrieving revision 1.3
diff -u -d -r1.3 login.c
--- login.c     1999/10/28 15:14:22     1.3
+++ login.c     1999/12/09 11:17:17
@@ -98,7 +98,7 @@
        struct utmp entry;
        struct utmp * entryp;
        struct utmp newentry;
-       char lbuf[UT_NAMESIZE], pbuf[20], salt[3];
+       char lbuf[UT_NAMESIZE], * pbuf, salt[3];
        char * tty_name;
        int n;
 
@@ -143,15 +143,9 @@
 #endif
                if ((pwd != NULL) && (pwd->pw_passwd[0] == 0)) {
                        login(pwd, entryp);
-               }
-               write(STDOUT_FILENO,"Password: ",10);
-               if (read(STDIN_FILENO, pbuf, sizeof(pbuf)) < 1) {
-                   exit(1);
                }
-               n = strlen(pbuf)-1;
-               if (pbuf[n] == '\n') {
-                   pbuf[n] = 0;
-               }
+               pbuf = getpass("Password:");
+               write(STDOUT_FILENO, "\n", 1);
                if (pwd != NULL) {
                        salt[0]=pwd->pw_passwd[0];
                        salt[1]=pwd->pw_passwd[1];
Index: passwd.c
===================================================================
RCS file: /home/elks/cvs/elkscmd/sys_utils/passwd.c,v
retrieving revision 1.2
diff -u -d -r1.2 passwd.c
--- passwd.c    1999/12/07 14:39:09     1.2
+++ passwd.c    1999/12/09 11:17:17
@@ -120,8 +120,6 @@
            pwd = getpwent();
            while (pwd != NULL) {
                if (pwd->pw_uid == uid) {
-                       char * p = strchr(nbuf1, '\n');
-                       if(p) *p = '\0';
                        pwd->pw_passwd = crypt(nbuf1, salt);
                }
                if (putpwent(pwd, passwd_fp) == -1) {

Reply via email to