Search This Blog

Tuesday, January 09, 2007

use XML::Simple

suppose I have a .passwds.xml file like this:


<?xml version="1.0"?>
<users>
<user>
<uid>0</uid>
<name>root</name>
<passwd>123456</passwd>
<identity>root</identity>
</user>
<user>
<uid>1</uid>
<name>test</name>
<passwd>123456</passwd>
<identity>root</identity>
</user>
</users>


and I can use the following perl script to parse it and determin whether a user can login


#!/usr/bin/perl
use XML::Simple;

$passwds = ".passwds.xml";

my $xmlobj = new XML::Simple(KeyAttr=>[]);

my $data = $xmlobj->XMLin("$passwds");

my $loggedin = -1;

my $username = "test";

my $password = "123456";

foreach (@{$data->{"user"}}){
if (($_->{"name"} ne $username)){
next;
}
$loggedin = 0;
if ($_->{"passwd"} eq $password){
$loggedin = 1;
}
last;
}
if ($loggedin == -1){
print "username incorrect\n";
die;
}
if ($loggedin == 0){
print "password incorrect";
die;
}
print "successfully login\n";

Thursday, January 04, 2007

strlen&sizeof

strlen is a function calculating the length of a string,i.e.,'\0' excluded,
while sizeof is an operator calculating a variable's size when compiling,i.e.,if affect on a string,'\0' will be included.
just as is shown in this little programme:


#include <stdio.h>
#include <string.h>

#define s "hello"
int main ()
{

printf ("strlen:%d\n",strlen (s));
printf ("sizeof:%d\n",sizeof (s));
return 0;/*thank u,anonymous,haha*/
}

after executed,it outputs:
strlen:5
sizeof:6