Assuming you aren't using the program as root, one way of doing it is to give /media/usb permission 755. Any attempt to write to the directory will fail if the device hasn't been mounted. You'll have to add user to the list of options in /etc/fstab for the device.
Otherwise, you can just parse the output of mount, or look in /proc/mounts or in /etc/mtab.