#! /users/jonh/bin/perl



















($Pgm = $0) =~ s|.*/||;
$Verbose = 0;


  arg: while ($_ = $ARGV[0]) {
  shift(@ARGV);
  /^-v$/    && do {$Verbose = 1; next arg;};
  }
  &main();
  exit(0);



sub main {
  local($userid)    =$ENV{'LOGNAME'};
  local(@memory)    =();
  local(@semaphores)=();
  local($command, $return_val);

  if (!open(FILEHANDLE,"ipcs |")) {
    print STDERR "$Pgm: Unable to clean semaphores. Use ipcs and ipcrm\n";
  }
  while (<FILEHANDLE>) {
    if (/$userid/) {
      push(@memory,    int($1)) if /^\s*m\s*(\d+)\s/;
      push(@semaphores,int($1)) if /^\s*s\s*(\d+)\s/;
    } 
  }
  close(FILEHANDLE);
  if ($Verbose) {
    print "$Pgm: shared memory to clean:",&myjoin(' ' ,@memory), "\n";
    print "$Pgm: semaphores to clean:",   &myjoin(' ' ,@semaphores), "\n";
  }
  $command = "ipcrm " . &myjoin(' -m ',@memory) . &myjoin(' -s ',@semaphores);
  system($command);
  $return_val=$?;
  if ($? !=0) {
    print STDERR "$Pgm: execution of ipcrm failed";
    print STDERR " (program not found)\n" if $return_val == 255;
    print STDERR " ($!)\n" if $! != 0;
  }  
}



sub myjoin {
  local($prefix,@list) = @_;

  if ($#list<0) {
    return "";
  } else {
    return ($prefix . join($prefix,@list));
  }
}


