Serial Ports - locking them, checking if they are open

Hi, I'm trying to fix up some serial code that was given to me by a coworker no longer on the project. Problem is, when the program is running and reading the port, I can take away the port by doing a cu command in another terminal window. When I release the cu, the program crashes, I think because it is trying to read a closed port.

I would like to know how to lock the port so the cu will not take it. One of our subcontractors has a program that tells me DEVICE LOCKED if I try the cu trick. I'd like to do that for my port. Does this involve the flock() call?

As a alternative, I'd like to be able to check if the serial port is open before I attempt a read. I'd also like to check if the port is already open before I call my original open. This seems like it would be solved with the stat() call, but I'm not seeing how to make it work.

I appreciate any advice/ideas. FYI, I am working on Solaris 7 and Solaris 8 (have to be compatible with both).

Thanks,
Kate

As I recall, Solaris does enforce mandatory locking.
Here is a sample by diddling the permission bits:

int fd;
mode_t perms=PERM_FILE;
perms|=S_ISGID;
fd=open("filename",O_CREATE | O_RDWR, perms);