Piping into pager (less) from C program

I am trying to write output to "less" from a C program to allow paging.  I have tried using popen (3), similar as in the following simple test program:
#include <stdio.h>
int main(int argc, char **argv)
FILE *out;
int i;
out= popen("/usr/bin/less", "w");
for( i= 0; i< 512; ++i )
fprintf(out, "This is line %d\n", i);
pclose(out);
This mostly works, but behaves differently from other programs that do the same thing.  In particular, you cannot use Ctrl-C to stop skipping to the end of the output without killing both less and the original program.  (Apparently less normally catches the signal, but is prevented from doing so when I start it as above.)
Both when typing "| less" in the shell and when using "man", this works.  I have looked at the output of pstree and noticed the following differences:  When piping into less from the shell, the less process is a child of the shell, so this is clearly different from what I can get with popen().  But when man is running, the less process is also a child of man, but it still behaves correctly.
Has anyone done this kind of thing before or has any ideas?

You want to play with pipe/fork:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void work(void) {
int i;
for (i=0; i<512; i++) {
printf("Line %d\n", i);
int main(void) {
int pfds[2];
pid_t pid;
if (pipe(pfds) < 0) {
perror("pipe failed");
return EXIT_FAILURE;
pid = fork();
if (pid < 0) {
perror("fork failed");
return EXIT_FAILURE;
} else if (pid == 0) { /* child */
close(pfds[0]); /* close unused read end */
/* set write end of pipe as stdout for this child process */
dup2(pfds[1], STDOUT_FILENO);
close(pfds[1]);
work();
} else /* if (pid > 0) */ { /* parent */
char *args[] = { "less", NULL };
close(pfds[1]); /* close unused write end */
/* set read end of pipe as stdin for this process */
dup2(pfds[0], STDIN_FILENO);
close(pfds[0]); /* already redirected to stdin */
execvp(args[0], args);
perror("exec failed");
exit(EXIT_FAILURE);
return EXIT_SUCCESS;
Hint for the future, you can use strace to observe how programs work. For pipes:
strace -o log.txt -f sh -c 'echo 1337 | less'
(`-o log.txt` writes to that file, `-f` takes forks into account)

Similar Messages

  • How to open-up a pdf file to a particular page via java? (file and page number from another program)

    I have created an Access 2003 Form. Data is entered. The data can be viewed once entered in the same form format, with one addition. The addition is a link to another program where we store files. The files stored are pdf documents. Each document is a scanned file (500+ pages). The link can open-up the pdf file. I want the link to open-up the pdf file to a particular page. I will supply the page number from the database.
    I am aware of Adobe Acrobat 7.0 PDF Open Parameters using a URL that specifies the file to be opened plus actions to be performed once the file is openend (i.e. http://example.org/doc.pdf#page=3). However, the 3rd party program transfers a file from a server to the client via java. The file does not go via a web browser. The java code transfer the file directly from the file server to the the client.
    Any idea how to accomplish the task of viewing a particular page with the process flow described?
    I am using Adobe Acrobat 7.0 Professional.

    So you are reading this file in a standalone version of Acrobat? If so then use the PDF Open Parameters /a option via the command line.
    Sabian
    Example:
    C:\Program Files\Adobe\Acrobat 7.0\Acrobat>Acrobat.exe /A "zoom=1000=OpenActions" "C:\Program Files\Adobe\Acrobat 7.0\Acrobat\SimpleSubmit.pdf"
    Where:
    Path to Acrobat: C:\Program Files\Adobe\Acrobat 7.0\Acrobat>Acrobat.exe
    Note: The example starts in the Acrobat application directory
    Switch: /A
    Parameter Syntax: "zoom=1000=OpenActions"
    Path to file: "C:\Program Files\Adobe\Acrobat 7.0\Acrobat\SimpleSubmit.pdf"
    Note: The quotes are very important for the syntax to work appropriately.

  • Insert into SAP table from external program

    Hi Gurus,
    I need to perform an insert of several records into a couple of SAP tables from an external program.
    Do you know of an RFC (if it is remote enabled the better) that allows me to specify the table and records to perfom an insert?
    Many thanks
    Mauricio Pego

    I don't know ABAP and wanted to avoid writing my own function if one standard exists.
    My requirement stands like this, I have a few z tables that need to populate from my C# application by use of the .Net connector.
    I haven't found a BAPI or RFC that allows me to add records to any table, but I don't know all the RFCs.
    May be one of you passed throu this already.
    Mauricio

  • Using 6.0.2 just updated, Firefox has stopped responding to web page calls from other programs such as Thunderbird. How can I fix this?

    running Windows 7, Firefox 6.0.2 and Thunderbird 3.0.2

    Unfortunately I tried that and it didn't work. Thanks for the help though. I tried to uninstall and it said I couldn't because the file is corrupt or something, but I think I finally got it uninstalled and/or deleted or whatever, but now I try to reinstall/download it again from the beginning and I can't. No matter what I do I can not get Firefox working again. No matter how many times I try to redownload it. Any other suggestions?

  • My mac is converting Word and Excel files into Pages and Numbers files when I click to open them (or download and open them) from an email. I do I get this to stop? It's really aggravating.

    My mac is converting Word and Excel files into Pages and Numbers files when I click to open them from an email. Downloading them and opening them doesn't help. How do I make this stop? I don't use iLife and don't want to; my clients are using Office and it's easier for me, except when I have to copy and paste everything they send me from an iLife file back to Word and Excel. Help!!! It's like Apple has become Microsoft, forcing us to use their apps.

    Do you have Word & Excel installed, if yes, navigate in the finder to an example of each, right click and select 'Get Info' scroll down to 'Open With' and select the appropriate program, check the box that says 'Change All'

  • I am trying to download music lyrics from the internet on my regular PC into pages on icloud.  I have a folder started in my Pc and not sure  Could you please give me some direction?  I am really new to iCloud!

    I have a question regarding copying something from the internet into pages on icloud.  I am trying to create a folder of music lyrics.  Has anyone tried doing this?

    You made a purchase and exhausted the credit on your card before it processed. All purchases are final. Contact iTunes Store support. You need to settle up before you can purchase or download anything else.

  • Quick doubt.. from cursor vairable how to get into page vairable

    Hello
    i am creating an apex page where i have 2 regions. From the Top region stores all fields entered into the bottom region
    Text fields like first name and last name and address fields are in region 2(bottom).After region 2, i have a add person button.
    Once i click add person, that person will get into top i.e region 1.
    Now, Region 1 got person1 first name ,last name
    person2, first name,last name
    etc..
    I am not able to display like p1_first_name,p1_laast_name as the list is not stable..it is growing and not showing the person who already got saved..I can retrieve them from DB using a cursor..But from cursor vairable how to get into page vairable..
    appreciate ur help..
    kp

    Your explanation is not good enough. You have to make a better description of your problem or create an example at apex.oracle.com.
    Denes Kubicek
    http://deneskubicek.blogspot.com/
    http://www.apress.com/9781430235125
    http://apex.oracle.com/pls/apex/f?p=31517:1
    http://www.amazon.de/Oracle-APEX-XE-Praxis/dp/3826655494
    -------------------------------------------------------------------

  • When I import or drag/drop an image from Finder into Pages, it floats the image instead of keeping it inline with text

    I'm new to Pages and I've searched online for an answer to this question but haven't found anything quite what I need.
    When I import an image from Finder (or drag and drop from Finder) into Pages, the Text Wrap default is "automatic" which seems to be "in front of text." I have to manually click each image and then change it from "Automatic" to "Inline With Text." Is there a way to change the default so it's not "Automatic" but "Inline With Text" to save me these steps for every single image I import?
    (If importing from Media Browser would behave differently, I don't use iPhoto except for Photo Stream so I need to bring them in from where I've saved them in my Finder.)
    Thanks so much for any help!

    Good questions...  I'll have to try and figure out when the problem first happened - I was thinking it was when I upgraded to Safari 5.1.4 but that might not be the case...  I'll try your suggestions to see if any of them eliminate the problem (I have restarted recenlty and that didn't fix it) but I'll try repairing permissions,etc.  Thanks!
    hmmm... Mcbuffy: are you implying that you *don't* get the webloc when dragging an image out of Safari and that this isn't a caused by change to Safari but is a big/conflict in mine?

  • How do I save documents from iCloud into pages on my ipad?

    HOw do I save documents from iCloud into pages using an ipad?

    Where are you expecting to see "iCloud document?" Pages stores copies of your documents in iCloud but the documents appear in the Pages app. When you edit the document changes are automatically saved to iCloud. You do not go to iCloud to look for documents to open or copy.

  • How can i save a pages doc from my macbook air into include

    how can i save a pages doc from my macbook air into include

    For about the 20th time, iWork for OS X, at this time, isn't iCloud aware.
    Look at :
    http://support.apple.com/kb/HT4942
    Yvan KOENIG (VALLAURIS, France)  mardi 22 janvier 2011 10:52:50
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.2
    My iDisk is : <http://public.me.com/koenigyvan>
    Please : Search for questions similar to your own before submitting them to the community

  • When I copy and paste a chapter from MS Word into Pages my tables and equations are trashed.  But importing them results in no control over my template How do I get my chapter into my template without copying

    When I copy and paste a chapter from MS Word into Pages my tables and equations are trashed.  But importing them results in no control over my template How do I get my chapter into my template without copying?

    Try just Opening the Word .doc from within Pages and see if that gets closer to what you want, then copy the chapter you want from the Pages version.
    Peter

  • How can you copy information from Safari into Pages

    Is there a way to easily copy data, such as financial tables from Safari into Pages and maintain the integrity of the table and the data within it?  When I copy and paste it comes out all misaligned and into multiple pages.  The same thing happens if I try to highlight the table in Safari and drag it into Pages.  Is there not a "paste special" function in Pages like there is in Microsoft??

    See:
    http://support.apple.com/kb/HT5775
    You can then drag the resulting image into a Pages document, and position.
    Ernie

  • How do I import document from my desk top into pages on my ipad?

    How do I import document from my desk top into pages on my ipad?

    Files Sharing
    http://support.apple.com/kb/ht4094

  • Insert records from report program into R3 table

    Hi
    I wanted to insert records from report program into R3 table.
    here is my code
    data : itab type standard table of zemp initial size 10 with header line.
    itab-EMPNO = '012'.
    itab-ENAME = 'XXXX'.
    itab-JOB = 'XXXX'.
    APPEND itab.
    insert ztable from table itab.
    but i am getting the following error
    the type of the data base table and work area/internal table "ITAB" are no unicode-converible.
    how can I insert records from report program into R3 table
    should I have to write move corresponding
    pls guide
    thanks
    manian

    Hi,
    itab-EMPNO = '012'.
    itab-ENAME = 'XXXX'.
    itab-JOB = 'XXXX'.
    APPEND itab.
    insert ztable from table itab.
    Do one thing
    Data : itab type table of ztable with header line.
    itab-EMPNO = '012'.
    itab-ENAME = 'XXXX'.
    itab-JOB = 'XXXX'.
    APPEND itab.
    insert ztable from table itab.
    error will resolve, then try to make structure similar to ZTABLE

  • Problems pasting pie chart from Numbers into Pages table

    Hello,
    I'm trying to paste a pie chart from Numbers into a table cell in Pages 09 but it doesn't allow me. Would you know why?
    Thanks,
    Pedro

    Probably because it is a smart object connected back to the Numbers document.
    If you want it purely as a graphic, try copying the chart in Numbers switching to Preview.app and going command n which will make it a pdf file, then copy that and paste that into Pages. But it will no longer be a live chart.
    Peter

Maybe you are looking for

  • How do I find/upgrade to DRM free Music from EMI

    I just read that EMI and Apple have agree to sell DRM free music at a higher quality for $1.29. iTune purchased songs from EMI can also be upgraded to DRM free tracks at $0.30 each. The problem is where do I find them? I can't seem to find search EMI

  • How to assgn charge head with GL and Sales order

    Dear Exparts' I am a new user of SD module .I have created a charge head but stuck in GL assignment and Assign the charge head with Sales order.(sales order is already created) Can any one tell me any direct transaction code or SPRO for the said prob

  • How to write Decode ?

    Hi All, please tell me how to write below statement in using decode statement in a single query. if A1 IS NULL and A2 IS NOT NULL then C := 'NA'; elsifF A1 IS NULL AND A2 IS NULL then C:='Success'; end if; Regards, Subir

  • Spotlight Search stopped working on my 4s

    Spotlight Search stopped working on my iPhone 4s.  Apple replaced my 4s 2 days ago and now spotlight search has stopped working on the 2nd 4s.  The Apple techs are stumped, any ideas?

  • Deleting archived files

    I am not able to delete any files in my archive. I keep getting an "error please try again message." I moved all my files to the archive because I'm over my limit but I'm not able to actually delete any of them.