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.
Possible solutions:
stat()
for each /dev/usb/lp*
in the loop (should be enough if You have single printer);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.