#!/usr/bin/perl print "Content-type: text/html\n\n"; print qq~        ~; use Net::FTP; my $host="host"; my $directory="html"; my $counter_file = 'cam.csv'; my $lock_file    = 'cam.lock'; my $lock_dir     = 'cam_lock'; my $count; my $flock_support = 1; if ($flock_support == 0) {    &lock_file;    if (-e $counter_file) {         open (COUNTER_FILE,"+<$counter_file") or die "Kann $counter_file nicht öffnen: $!\n";    } }else {    if (-e $counter_file) {         open (COUNTER_FILE,"+<$counter_file") or die "Kann $counter_file nicht öffnen: $!\n";         flock(COUNTER_FILE,2) or die "Can file nicht sichern: $!\n";    } } #------------------------------------------------------------------------------------------ --    $count = ;    chop $count;    close(COUNTER_FILE);    $count++;    if (-e $counter_file) {         open (COUNTER_FILE,">$counter_file") or die "Kann $counter_file nicht öffnen: $!\n";         flock(COUNTER_FILE,2) if $flock_support or die "Can file nicht sichern: $!\n";    }    print (COUNTER_FILE "$count;");    close(COUNTER_FILE); #------------------------------------------------------------------------------------------ -- if($flock_support == 0) {    close(COUNTER_FILE);    &unlock_file; }else { close(COUNTER_FILE); }    $ftp=Net::FTP->new($host,Timeout=>240) or $newerr=1;      push @ERRORS, "Can't ftp to $host: $!\n
" if $newerr;      myerr() if $newerr;      print $ftp->message;      print "
";    $ftp->login("usn","pwd") or $newerr=1;    print "Getting file list
";      push @ERRORS, "Can't login to $host: $!\n
" if $newerr;      $ftp->quit if $newerr;      myerr() if $newerr;      print $ftp->message;      print "
";    $ftp->cwd($directory) or $newerr=1;      push @ERRORS, "Can't cd  $!\n
" if $newerr;      myerr() if $newerr;      print $ftp->message;      print "
";      $ftp->quit if $newerr;    @files=$ftp->dir or $newerr=1;      push @ERRORS, "Can't get file list $!\n
" if $newerr;      myerr() if $newerr;      print $ftp->message;      print "
";    #foreach(@files) {    #  print "$_\n
";    #}    $ftp->binary();    $ftp->get('cam.jpg',"cam_$count.jpg") or $newerr=1;     push @ERRORS, "Can't get file $!\n
" if $newerr;     myerr() if $newerr;     print $ftp->message;     print "
";    $ftp->quit;    print "Closed FTP
";    print qq~$count Bilder downloaded        ~; sub lock_file {    my $lock_timeout = 60;    my $lock_count;    while(-f $lock_file && !mkdir($lock_dir,0777)) {        if (++$lock_count > $lock_timeout) {            die("Kann \"$lock_file\" nicht sichern!: $!\n");            return 1;        }        sleep (1);    }    open(LOCK,">$lock_file") || die("Can't open $lock_file: $!\n");    return 0; } sub unlock_file {    close(LOCK);    unlink("$lock_file");    rmdir($lock_dir); } sub myerr {  print "Error: \n";  print @ERRORS;  exit 0; }