sub get_field { #returns field value if all OK # -1 no $dba_file # -2 no field in database # my ($dba_file,$field)=@_; my (@all,$exist,$out); $exist = 0; unless (open (x_dba_fh_x, "< ".$dba_file)){ $out = -1; return ($out); exit(-1); } flock (x_dba_fh_x, LOCK_EX); while (){ if ($_ =~ /^$field\: /){ $exist = 1; $out = substr($_,(length($field)+2)); chomp ($out); } } flock (x_dba_fh_x, LOCK_UN); close (x_dba_fh_x); if ($exist == 0){ $out = -2; } return ($out); } sub modify_field { #returns 1 if field has been modified successfully # -1 no $dba_file # -2 modification failed #Note: adds the field if it doesn't exist! local ($dba_file,$field,$value)=@_; local (@all,$out,$exist); $exist = 0; @all = (); $value =~ s/\r/\\n/g; $value =~ s/\n/\\n/g; $value =~ s/(\\n)+/\\n/g; unless (open (y_dba_fh_y, "< ".$dba_file)){ $out = -1; return ($out); exit(-1); } flock (y_dba_fh_y, LOCK_EX); while (){ if ($_ =~ /^$field\: /){ push (@all, $field.':'.' '.$value."\n"); $exist = 1; } else { push (@all, $_); } } flock (y_dba_fh_y, LOCK_UN); close (y_dba_fh_y); unless (open (y_dba_fh_y, "> ".$dba_file)){ $out = -2; return ($out); exit(-2); } flock (y_dba_fh_y, LOCK_EX); foreach $temp(@all){ print y_dba_fh_y $temp; } if ($exist == 0) { print y_dba_fh_y $field.':'.' '.$value."\n"; } flock (y_dba_fh_y, LOCK_UN); close (y_dba_fh_y); return (1); } 1;