1 /*login module, password placed in pass.db*/2#include <stdio.h>3#include <unistd.h>4#include <fcntl.h>5#include <stdlib.h>6#include <string.h>7#include <stdbool.h>8 9typedefstructperson{Ten Charname[ -]; One Charpass[ -]; A }person; - - voidReg () the { - Charname[ -]; -printf"Please input your name:"); -scanf"%s", name); + intFD = open ("pass.db", o_creat| o_rdwr| O_append,0666); - if(FD = =-1){ +printf"cannot open pass.db!\n"); AExit (-1); at } - intn =sizeof(person); - CharTmp[n]; - //determine if the user name exists - while(Read (Fd,tmp,sizeof(person)) ==sizeof(person)) { - if(!STRNCMP (Tmp,name, +)){ inprintf"This name had been registerwd!" - "Please try Anthor one!\n"); toprintf"Now please enter the name of you want:"); +scanf"%s", name); -Lseek (FD,0, Seek_set);//after entering the new name, start the comparison from the file header the } * } $printf"congratulations,%s is avilable!\n", name);Panax Notoginsengprintf"Now,please Enter the password:"); - Charpass[ -]; the Charcheck[ -]; +scanf"%s", pass); Aprintf"Check the password:"); thescanf"%s", check); + while(strcmp (Pass,check)) { -printf"I ' m sorry,you entered different password!\n"); $printf"Please enter another password:"); $scanf"%s", pass); -printf"Check the password:"); -scanf"%s", check); the } - Person Regperson;Wuyi strcpy (regperson.name,name); the strcpy (regperson.pass,pass); -Lseek (FD,0, seek_end); WuWrite (Fd,®person,sizeof(person)); - Close (FD); About } $ - voidconfirm () - { - BOOLFlag =false; A intFD = open ("pass.db", o_rdonly); + if(FD = =-1){ theprintf"FILE \ "Pass.db\" opened failed\n"); -Exit (-2); $ } the Person tmp; the Charname[ -]={0}; the Charpass[ -]={0}; theprintf"\tname:"); -scanf"%s", name); in while(Read (Fd,&tmp,sizeof(TMP)) ==sizeof(TMP)) { the if(!STRNCMP (Name,tmp.name, -)){ the while(STRNCMP (pass,tmp.name+ -, -)){ Aboutprintf"\tpassword:"); thescanf"%s", pass); the } theFlag =true; + } - } the if(flag)Bayiprintf"sucess!\n"); the } the - intMain () - { theprintf"1,reg\t\t2,login\n"); the int Select; thescanf"%d",&Select); the Switch(Select){ - Case 1: Reg (); the Break; the Case 2: Confirm (); the Break;94 default:p rintf ("ERROR input\n"); the Break; the } the return 0;98}
Registered login module implemented with UNIX C system functions