3

I have this line in my code:

#define DEFAULT_PRINTER "/dev/usb/lp0"

It's node changes after pc reboots though (ex. lp3, lp2). How can I set this automatically? The code won't work with the wrong path. Thanks.

xinthose
  • 3,213
  • 3
  • 40
  • 59

1 Answers1

2

Possible solutions:

  • Try stat() for each /dev/usb/lp* in the loop (should be enough if You have single printer);
  • You can try modifying udev rules to assign fixed lp device numeber to Your printer;

UPDATE ( for stat() ):

char DEFAULT_PRINTER[] = "/dev/usb/lpX";
struct stat buf;
for( i = 0; i < 10; i++ ){
   DEFAULT_PRINTER[11] = '0' + i;
   if( ! stat( DEFAULT_PRINTER, &buf ) ) break;
}

NOTE: this is not "universal" code, it won't work for different length names (for example /dev/usb/lp10) without adjustments. It is just to show an idea.

kestasx
  • 1,091
  • 8
  • 15
  • How would I do that? struct stat buf; int stat ("/dev/usb/lp", &buf); – xinthose Dec 23 '14 at 22:07
  • 1
    @xinthose, I've added C/C++ code fragment to use `stat()`. – kestasx Dec 23 '14 at 22:50
  • It works! thank you. I realize now that the 11th character of buf.st_dev is its node number – xinthose Dec 23 '14 at 23:06
  • No. You can simply ignore `buf` (it is not used - only return value of `stat` is). DEFAULT_PRINTER now is char array, not macro, as in Your original code. 11'th array element is changed in the loop (from '0' to '9'). Code won't work as-is for lp10 - You'll need to adjust the way printer names are created, but idea holds. – kestasx Dec 23 '14 at 23:19
  • I only have one printer, so this will work well for me. – xinthose Dec 24 '14 at 14:49