#!/usr/bin/perl -wI./
#
# Created 9/25/97 by Anthony Ball (originally for Digital Unix)
# Modified 1/27/00 by Mark Claypool (to work with Linux)
# Copyright 1999, 2001
# ant@wpi.edu, claypool@cs.wpi.edu
# Free for use, edits and abuse by whoever wants it
# especially wpi... 

# Shared Memory
$ipcs_type = "-m";
$ipcrm_type = "shm";
&doIt;

# Semaphore
$ipcs_type = "-s";
$ipcrm_type = "sem";
&doIt;

# Message Queue 
$ipcs_type = "-q";
$ipcrm_type = "msg";
&doIt;

print "Done.\n";

# Parse and remove
sub doIt {
  @dude = split("\n", `ipcs $ipcs_type`);
  foreach (@dude) {
    s/\s+/ /ig;  
    @data = split(" ", $_);
    if($data[2] && $data[2] =~ /$ENV{USER}/) {
      system("ipcrm $ipcrm_type $data[1]");
      print "IPC $data[1] of type ";
      print "Message Queue" if $ipcs_type eq "-q";
      print "Shared Memory" if $ipcs_type eq "-m";
      print "Semaphore" if $ipcs_type eq "-s";
      print " has been removed (hopefully).\n";
    }
  }
}
