Joseph Durbin wrote:

> Hello!,  I'm getting confused over pointers here,
> I am trying to convert a passwd file,  which has TEXT passwords instead
> of the
> encrypted versions ,  this is suppossed to replace those.
> (but i run into pointer problems with the "crypt" command...)
> where should my *'s and &'s be?!   (I will use a random SALT once it
> works...)

> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #define _XOPEN_SOURCE
> #include <unistd.h>
> 
> void main() {
>   FILE *F,*FF;
>   char salt[2]="ab";
>   char oldpasswd[20], *newpasswd;
>   char c;
>   int r1;
>   F=fopen("./passwd","r");
>   FF=fopen("./passwd.new","w");
>   while (feof(F)==0) {
>     while ((c!=':')&&(feof(F)==0)) { c=fgetc(F); fprintf(FF,"%c",c); }
> //Read the [username:]
>     r1=-1; c=0;
>     while ((c!=':')&&(feof(F)==0)) { c=fgetc(F); ++r1; oldpasswd[r1]=c;
> } oldpasswd[r1]=0;
>     newpasswd=crypt(oldpasswd,salt);
>     if (feof(F)==0) fprintf(FF,"%s:",newpasswd);
>     while ((c!='\n')&&(feof(F)==0)) { c=fgetc(F); fprintf(FF,"%c",c); }
> //Read the rest
>   }
>   fclose(FF);
>   fclose(F);
> }

It would be much simpler to read a line at a time into the buffer and
use strtok() to split it into fields.

A couple of other points:

First, feof() tests whether you have already tried to read past EOF;
it won't return true until *after* fgetc() has returned EOF. 
Consequently, you need to check whether the value returned from
fgetc() is equal to EOF before you actually use it.

Second, it's preferable to use fputc() rather than fprintf("%c") to
write a single character.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to