简体   繁体   English

在ac程序中的linux中,“ lp filename”命令的调用是什么?

[英]What is the call for the “lp filename” command in linux in a c program?

I want to use the above command in ac program in linux. 我想在Linux的ac程序中使用以上命令。

I have searched so far that there are system calls and exec calls that one may make in a code. 到目前为止,我已经搜索了可能在代码中进行的系统调用和exec调用。 Is there any other way using exec or system commands? 还有其他使用exec或系统命令的方式吗? Using the system command isn't an ideal command for a multi-threaded server ,what do you suggest? 对于多线程服务器,使用system命令不是理想的命令,您有何建议?

First make sure you have lp installed in this path. 首先确保在此路径中安装了lp (Using which lp in the terminal). (在终端中使用which lp )。

You may want to understand the lp command. 您可能想了解lp命令。 It's a classic unix command to send data to the "line printer", but it works with eg .pdf files too nowadays, depending on your printer system. 这是将数据发送到“行式打印机”的经典unix命令,但是现在它也可以与.pdf文件一起使用,具体取决于您的打印机系统。 However, it isn't necessarily installed. 但是,不一定要安装它。 Sometimes, lpr may work better, too. 有时, lpr也可能会更好。

See also: http://en.wikipedia.org/wiki/Lp_%28Unix%29 另请参阅: http : //en.wikipedia.org/wiki/Lp_%28Unix%29

The second part is about executing unix commands. 第二部分是关于执行unix命令的。 system is the easiest (also the easiest to introduce a security issue into your program!), using fork and execve is one of a number of alternatives (have a look at man execve ). system是最简单的(也是将安全问题引入程序的最简单的方法!),使用forkexecve是多种选择之一(请man execve )。

Yes, this code is ok. 是的,此代码可以。 It will print the file named filename provided that the lp is found at /usr/bin and the filename file exists. 只要在/usr/bin找到lp并且filename文件存在,它将打印名为filename的文件。 You can add checks for that if you want your program to report if something went wrong, other than that it will do exactly what you expect. 如果希望程序在出现问题时报告错误,则可以添加检查,否则程序将完全按预期运行。

Doing system("lp filename"); 正在做system("lp filename"); would work if you don't mind your program blocking after that system() call and until lp finishes. 如果您不介意在system()调用之后直到lp完成之前阻塞程序,它将可以工作。

You could also use popen(3) (instead of system(3) ). 您也可以使用popen(3) (而不是system(3) )。 But you always need to fork a process (both system and popen are calling fork(2) ). 但是,您始终需要派生一个进程( systempopen都在调用fork(2) )。 BTW, if you have a CUPS server you might use some HTTP client protocol library like libcurl but that is probably inconvenient. 顺便说一句,如果您有CUPS服务器,则可以使用一些HTTP客户端协议库,例如libcurl,但这可能很不方便。 Better popen or system an lp (or lpr ) command. 更好地popensystem lp (或lpr )命令。

BTW, printing is a relatively slow and complex operation, so the overhead of forking a process is negligible (I believe you could do that in a server; after all people usually don't print millions of pages). 顺便说一句,打印是一个相对较慢且复杂的操作,因此,分叉进程的开销可以忽略不计(我相信您可以在服务器上完成;毕竟人们通常不会打印数百万页)。 Some libraries might give you some API (eg QPrinter in Qt). 一些库可能会为您提供一些API(例如Qt中的QPrinter )。

Notice that the lp (or lpr ) command is not actually doing the printing, it is simply interacting with some print daemon ( cupsd , lpd ...) and its spooling system. 注意, lp (或lpr )命令实际上并没有进行打印,它只是与某些打印守护进程( cupsdlpd ...)及其后台打印系统进行交互。 See eg CUPS . 参见例如CUPS So running the lp or lpr command is reasonably fast (much faster than the printing itself), generally a few milliseconds (certainly compatible with a multi-threaded or server application). 因此,运行lplpr命令的速度相当快(比打印本身快得多),通常要几毫秒(肯定与多线程或服务器应用程序兼容)。

Quite often, the command passed to popen or system is constructed (eg with snprintf(3) etc...), eg 通常,构造传递给popensystem的命令(例如,使用snprintf(3)等),例如

  char cmdbuf[128];
  snprintf (cmdbuf, sizeof(cmdbuf), "lp %s", filename);

but beware of code injection (think about filename containing foo; rm -rf $HOME ) and of buffer overflow 但要注意代码注入 (考虑包含foo; rm -rf $HOME filename foo; rm -rf $HOME )和缓冲区溢出

Of course, notice that library functions like system , popen , fopen are generally built above existing syscalls(2) . 当然,请注意,像systempopenfopen类的库函数通常是在现有syscalls(2)之上构建的。 Read Advanced Linux Programming 阅读高级Linux编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM