Telling apart serial from // port

I'm writing a journal_write() function and I want it to:

  • be a possible drop in replacement for write()
  • write entries to the journal :wink: Could be a regular file (journal.txt), a serial printer or a // printer.
  • handle printer status if needed.
    fstat() tells weather or not we're dealing with a regular file. So far so good. Now, how can I tell the difference between a serial and a // port?
    Once this is done, how can I get the // printer status the way stty does? (I currently do it using a non-ANSI, non-POSIX devctl() call...)

OS: QNX 6.2.1 on x86

TIA

I don't know QNX, and I'm not sure what you mean by serial or // printer. Nor do I understand why you are taking such an odd approach to this.

I think it would make much more sense to be writing a journal_open() and maybe a journal_close(). A journal_write() might also make sense if you need to greatly assist with a device error such as out-of-paper. But there is a lot of one-time setup stuff for many printers.

Also a journal_open() would make the filename visable to you. With unix, you know that /dev/dsk/c0t6d0s4 is not a line printer. Unix has some naming conversions for devices. They vary from version to version...but each version has a well defined convention. I know that the whole raison d'�tre of fstat() and fcntrl() was to enable programs to work with files already opened by the shell before the program even started to run. But a program like:
diagnose < /dev/any/device
is not a reasonable extention of this. A system can have many line printers. If your particular line printer has trouble, how would phrase the error message without a filename?

If you want to use your approach anyway, I can think of 2 approaches that might work. The first is to get the major number of the device. You do that looking at the st_dev field in the stat structure. You want to use a macro like this:
major(statbuf.st_dev)
where major is a macro in <sys/sysmacros.h>. You would have to know which major number refers to which driver and there is no portable way to do that...well maybe searching /dev. The second approach is to assume it is a serial device. Try a serial ioctl to it. If the ioctl works, it must be serial.

On unix, stty would only work with a serial port. So at this point, I'm confused, however, to configure a serial port on unix using posix conventions, see "man termios". Here is an example. To configure other devices under unix, you would typically use ioctl() with a request that is documented on the man page for the driver that controls the device.

I meant "serial or parallel printer."
I took this approach to be the least intrusive in the client code. Replacing only 1 call (write) on the client side is better (to me)that replacing 3 calls(open, write, close). But I agree that writing a journal_open() and a journal_close() would allow to determine the kind of devices we're dealing with using the name. Then the journal_write() could take care of the details like paper-out/busy/alarm when appropriate.

I know that this isn't the best place to post QNX related problems, but it's for sure the best place for me to learn a bunch of UNIX related programming basics that are usefull in QNX. I'll try to keep it on the UNIX side!

Thank you for help and your well documented answer!