Skip to content

Instantly share code, notes, and snippets.

@MitchiLaser
Created October 10, 2023 13:02
Show Gist options
  • Save MitchiLaser/31f366dec6bc72e794e8c1063d8a8cf5 to your computer and use it in GitHub Desktop.
Save MitchiLaser/31f366dec6bc72e794e8c1063d8a8cf5 to your computer and use it in GitHub Desktop.
lp and lpr wrapper to disable -U command line argument

LP Wrapper

The Linux Command line tools lp and lpr provide the parameter -U which gives a user the ability to switch into the role of another user. This bypasses the possibility to track the amount of printed pages in a large university network. Therefore a wrapper-program was designed which filters the list of arguments and removes a -U if it can be found. The compiled binaries have to be installed at a location which is higher ranked in the $PATH variable than the original binaries.

installation

By Calling $make the source code will be compiled and with $sudo make install after compilation the binaries will be copied into the right location. Make sure that /usr/local/bin is located in the $PATH variable.

#include <iostream>
#include <cstring>
#include <unistd.h>
using namespace std;
int main(int argc, char * argv[])
{
char * new_argv[ argc + 1 ] = {NULL};
unsigned int removed_from_argc = 0;
for(int i = 0; i < argc; i++) { // run through all command line parameters
if( strcmp( argv[i], "-U" ) == 0 ) { // search for a "-U" command line parameter
// skip the -U parameter from the list of new parameters and memorize that the ammount of parameters is now smaller
removed_from_argc++;
if( (i + 1) < argc ) {
// Remove also the next value if there is one
i++;
removed_from_argc++;
}
} else if( strlen(argv[i]) > 2 && ( argv[i][0] == '-' ) && ( argv[i][1] == 'U' ) ) { // search for a "-U..." and remove only this one, keep all following
// skip the -U parameter from the list of new parameters and memorize that the ammount of parameters is now smaller
removed_from_argc++;
}else {
new_argv[i - removed_from_argc] = argv[i];
}
}
// execv() now with the right set of parameters
#ifdef LP
char path_to_exec[] = "/usr/bin/lp";
char name_exec[] = "lp";
#endif
#ifdef LPR
char path_to_exec[] = "/usr/bin/lpr";
char name_exec[] = "lpr";
#endif
new_argv[0] = name_exec;
execv(path_to_exec, new_argv);
}
CC=g++
main:
$(CC) -D LP main.cpp -o lp
$(CC) -D LPR main.cpp -o lpr
clean:
rm lp_wrapper
rm lpr_wrapper
instal: main
mkdir -p /usr/local/bin
cp ./lp /usr/local/bin
chmod +x /usr/local/bin/lp
cp ./lpr /usr/local/bin
chmod +x /usr/local/bin/lpr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment