this struct
when I try to add user I have that :
testmachine:/usr/share/my_passwd# ./my_passwd -a users.db
Ajout d'une entrée dans la base :
Username : test
Password : aze
Gecos : qsd
Home directory : wxc
Shell : rfv
Nb Items : 1
And when I try to list all data in my list, I have that
testmachine:/usr/share/my_passwd# ./my_passwd -l users.db
Liste toutes les entrées dans la base :
Username : testg@Password : Gecos : ¼@¾«Lc@ØÄ@¼p@>@(x@À¨@¤öÿ¿¢B@ l@Äy@½@¬@¿
Home directory : <î@ðöÿ¿<{Shell : @
Can somebody help me ???
this is my struct :
typedef struct _Infos Infos;
struct _Infos {
char pw_name[255]; /* user name */
char pw_passwd[255]; /* password */
unsigned int pw_uid; /* user uid */
unsigned int pw_gid; /* user gid */
char pw_gecos[128]; /* general info */
char pw_dir[255]; /* home directory */
char pw_shell[64]; /* default shell */
};
this is my code :
#include "my_passwd.h"
/* Init base */
void init_userdb (char* file)
{
Infos user;
int f;
f = open(file, O_RDONLY|O_CREAT);
if (f < 0) exit(-1);
userdb = list_new();
while (read(f, &user, sizeof(user)) > 0) {
userdb = list_push(userdb, &user);
}
close(f);
}
/* save into base */
void save_userdb (char *file)
{
List *vListMove;
int f;
f = open(file, O_RDWR);
if (f < 0) exit(-1);
for(vListMove = userdb; !list_is_end(pList,vListMove);
list_move_next(userdb,vListMove)) {
write(f, vListMove->data, sizeof(vListMove->data));
}
close(f);
}
/* add to base */
void adduser (char *file)
{
Infos user;
init_userdb(file);
printf("Username : ");
fgets(user.pw_name, sizeof (user.pw_name), stdin);
printf("Password : ");
fgets(user.pw_passwd, sizeof (user.pw_passwd), stdin);
printf("Gecos : ");
fgets(user.pw_gecos, sizeof (user.pw_gecos), stdin);
printf("Home directory : ");
fgets(user.pw_dir, sizeof (user.pw_dir), stdin);
printf("Shell : ");
fgets(user.pw_shell, sizeof (user.pw_shell), stdin);
user.pw_uid = 0;
user.pw_gid = 0;
userdb = list_push(userdb, &user);
save_userdb (file);
printf("Nb Items : %d",list_nb_item(userdb));
printf("\n");
exit(0);
}
/* del item from la base */
void deluser (char *file)
{
puts("del item from la base.\n");
exit(0);
}
/* List all items */
void listusers (char *file)
{
List *vListMove;
Infos *user;
init_userdb(file);
for(vListMove = userdb; !list_is_end(pList,vListMove);
list_move_next(userdb,vListMove)) {
user = vListMove->data;
printf("Username : %s",user->pw_name);
printf("Password : %s",user->pw_passwd);
printf("Gecos : %s",user->pw_gecos);
printf("Home directory : %s",user->pw_dir);
printf("Shell : %s",user->pw_shell);
printf("\n");
}
exit(0);
}