LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2009, 11:03 AM   #1
dlinux
Member
 
Registered: Jan 2007
Location: Portugal
Distribution: Slackware 13.0
Posts: 52

Rep: Reputation: 15
Pty Programming-Extra newline


Hello everyone, I'm having a hard time figuring out why the program posted below prints an extra newline every time I type the enter key.This program is using the master pseudo-terminal to send the password and receive the output from the slave(connected to the passwd program).I suspect this has to do with the terminal line discipline(s)(2 considering the master and slave), but I can't really understand why.I have tried turning on/off several terminal special characters but to no avail.
Any help is greatly appreciated.
Source code follows(compiled in x86, Slackware-13.0 - linux-2.6.31.5 - gcc 4.3.3):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>

int grantpt(int);
char *ptsname(int);
int unlockpt(int);

void
die(char *ptr, ...)
{
	va_list vlist;
	va_start(vlist, ptr);
	char ch;

	while ((ch = *ptr++) != '\0') {
		if (ch == '%' && *ptr != '\0') {
			ch = *ptr++;
			switch(ch) {
				case 'd': printf("%d", va_arg(vlist, int)); break;
				case 'e': printf("%e", va_arg(vlist, double)); break;
				case 'c': printf("%c", va_arg(vlist, int)); break;
				case 's': printf("%s", va_arg(vlist, char *)); break;
				default: va_end(vlist);exit(1);
	

			}
			

		} else
			printf("%c", ch);


	}

	va_end(vlist);
	exit(1);
}



int
main(void)
{
    int pid,pid2;
    int ptm;
    int pts;
    char *ptr;
    char *program_name[2] = {"/usr/bin/passwd", NULL};
    char buf[512];
    int n;

    if ((ptm = posix_openpt(O_RDWR)) == -1)
        die("posix_openpt error: %s\n", strerror(errno));
    if (grantpt(ptm) == -1)
        die("grantpt error: %s\n", strerror(errno));
    if (unlockpt(ptm) == -1)
        die("unlockpt error: %s\n", strerror(errno));

    if ((pid = fork()) < 0)
        die("fork error: %s\n", strerror(errno));
    else if (pid > 0) {    /* parent */

        if ((pid2 = fork()) < 0)
            die("2 fork error");
        else if (pid2 == 0) {        /* child in parent */
            if(dup2(ptm, STDOUT_FILENO) != STDOUT_FILENO)
                die("dup2 failed");

            for ( ; ;) {
                if ((n = read(STDIN_FILENO, buf,511)) <= 0)
                    die("read error");
                buf[n] = '\0';
                write(STDOUT_FILENO, buf, strlen(buf));

            }
        } else  {    /* parent in parent */
            if (dup2(ptm, STDIN_FILENO) != STDIN_FILENO)
                die("dup2 of parent failed");
            for (; ;) {
                if ((n = read(STDIN_FILENO, buf, 511)) <= 0)
                    die("read error");
                buf[n] = '\0';
                write(STDOUT_FILENO, buf, strlen(buf));

            }
        }
        
    
    } else {    /* child */
        if(setsid() == (pid_t) -1)
            die("setsid() error: %s\n", strerror(errno));

        if ((ptr = (char *) ptsname(ptm)) == NULL)
            die("ptsname error: %s\n", strerror(errno));
        if ((pts = open(ptr, O_RDWR)) < 0)
            die("open of slave failed: %s\n", strerror(errno));
        close(ptm);

        if (dup2(pts, STDIN_FILENO) != STDIN_FILENO ||
                dup2(pts, STDOUT_FILENO) != STDOUT_FILENO ||
                dup2(pts, STDERR_FILENO) != STDERR_FILENO)
            die("error doing dup's : %s\n", strerror(errno));

        if (execve(*program_name, program_name , NULL) == -1)
            die("execve error: %s\n", strerror(errno));
        exit(1);


    }


    /* should not get this far */
    return 0;
}

Last edited by dlinux; 12-22-2009 at 11:41 AM. Reason: updated code
 
Old 11-12-2009, 05:16 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I'm suspecting line discipline.

Usually when I use a pty, I make sure that the end of line character I'm sending to it is carriage return, not line feed or carriage return/line feed.

To a file somewhere, dump the hex form of each character you're sending to the pty. This ought to help narrow the problem down.
 
Old 12-18-2009, 01:01 PM   #3
Gnusboy
LQ Newbie
 
Registered: Jul 2009
Posts: 26

Rep: Reputation: 0
Failed fork pty error
Ok, so I am fairly new at Ubuntu, but I'd really like a solution to the forked pty issue. I run jaunty 9.04 and it worked fine until package manager broke. After I do what appears correct I still keep getting failed fork pty error.
Please give a solution.
I've done the ctl/alt F1 for terminal and entered this code many times:
sudo cp /etc/init.d/mountdevsubfs.sh.dpkg-dist /etc/init.d/mountdevsubfs.sh
New Error message: mountdevsubfs.sh is not a directory, or is not found or whatever. Whether or not I have mis-entered the code, I don't know, but I type what I see.
All I want is to get my DVD drive recognized and Mediabuntu to work.

I'd like help please. I just want to get the terminal and/or package manager to work.

Thank you
 
Old 12-19-2009, 04:00 AM   #4
dlinux
Member
 
Registered: Jan 2007
Location: Portugal
Distribution: Slackware 13.0
Posts: 52

Original Poster
Rep: Reputation: 15
And why are you asking that in this thread?
 
Old 12-19-2009, 05:06 AM   #5
Gnusboy
LQ Newbie
 
Registered: Jul 2009
Posts: 26

Rep: Reputation: 0
Oh, I am so sorry. I didn't realize I was on the wrong turf. I saw programming and made the assumption that broken code lines and fixes would naturally fit into a programming thread. I was actually hoping for some help fixing my problem with a failed fork pty
 
Old 12-19-2009, 05:43 AM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by Gnusboy View Post
Oh, I am so sorry. I didn't realize I was on the wrong turf. I saw programming and made the assumption that broken code lines and fixes would naturally fit into a programming thread. I was actually hoping for some help fixing my problem with a failed fork pty
No problem, it's a natural mistake. In this forum, we would ordinarily expect you to display some lines of code and ask, what did I do wrong here?
 
Old 12-20-2009, 01:57 AM   #7
Gnusboy
LQ Newbie
 
Registered: Jul 2009
Posts: 26

Rep: Reputation: 0
This is the code I have been trying via the Ctl/Alt F1 console:

sudo cp /etc/init.d/mountdevsubfs.sh.dpkg-dist /etc/init.d/mountdevsubfs.sh
Error message: mountdevsubfs.sh is not a directory. There have been other error messages, but I do not know how to copy the output to read when I close the console.

I cannot have the console and desktop open together. It is one or the other - not both

I cannot find correct keyboard commands to move or stop the fast scrolling while in console
I cannot find how to copy lines of code while in the console
I cannot copy and paste commands while in console
I cannot fix my problem on my own
I have asked for help in the forum - I get different command strings from the 3 people who have responded to my posts. The command strings do not work and the error messages continue.
I have looked in Google, the forums and chat. I have searched dozens of threads that appear related to the "failed to fork pty" but cannot get an understandable answer.
When I try to open a terminal, I get this message: "There was an error creating the child process for this terminal"
The terminal window is broken. The package manager is broken.
I keep hoping that someone will give me a simple, easy to follow solution to the problems, but no one has.
And I suppose someone will now tell me I am in the wrong place to ask such stupid questions.

I have invested many, many hours reading to find a solution, but have found mostly conflicting suggestions that are more confusing.

If this is not the right place - please inform me of the correct place to get a solution.

Thank you
 
Old 12-20-2009, 05:27 AM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by Gnusboy View Post
And I suppose someone will now tell me I am in the wrong place to ask such stupid questions.
You're half right.
  1. You're wrong, in that your question is not stupid. It's an excellent question.
  2. You're right, in that this is the wrong place to ask that question. You have shown us the commands you typed, which in turn run certain programs. That isn't exactly what this forum is for, though linuxquestions may have a forum that helps you. This forum might be used in the quest to fix such programs. Your recent detailing of symptoms, while excellent in itself, does not suggest (to me, at least) that any program is misbehaving.
  3. If you do find the correct forum on linuxquestions, please don't just pick a thread and derail the original conversation by changing the subject, as you did in this thread. Start a new thread, so as not to disrupt the original quest by the original poster for a solution to his problem.
  4. There is enough in your most recent post to suggest that perhaps the Linux Newbie forum might be a good place to start. Please do not be offended by this suggestion.
 
Old 12-20-2009, 05:29 AM   #9
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by wje_lq View Post
I'm suspecting line discipline.

Usually when I use a pty, I make sure that the end of line character I'm sending to it is carriage return, not line feed or carriage return/line feed.

To a file somewhere, dump the hex form of each character you're sending to the pty. This ought to help narrow the problem down.
dlinux, how's your situation coming along?
 
Old 12-20-2009, 08:02 PM   #10
Gnusboy
LQ Newbie
 
Registered: Jul 2009
Posts: 26

Rep: Reputation: 0
Bad, bad Gnusboy

Quote:
Originally Posted by wje_lq View Post
You're half right.
  1. You're wrong, in that your question is not stupid. It's an excellent question.
  2. You're right, in that this is the wrong place to ask that question. You have shown us the commands you typed, which in turn run certain programs. That isn't exactly what this forum is for, though linuxquestions may have a forum that helps you. This forum might be used in the quest to fix such programs. Your recent detailing of symptoms, while excellent in itself, does not suggest (to me, at least) that any program is misbehaving.
  3. If you do find the correct forum on linuxquestions, please don't just pick a thread and derail the original conversation by changing the subject, as you did in this thread. Start a new thread, so as not to disrupt the original quest by the original poster for a solution to his problem.
  4. There is enough in your most recent post to suggest that perhaps the Linux Newbie forum might be a good place to start. Please do not be offended by this suggestion.
Wouldn't it have been more helpful to solve my problem rather than excoriating me for being in the wrong place to ask?
Besides, I thought I was responding to your message. If I could copy the code and include it, I would have, which is what I said in the above message.
I have also posted these problems/questions to the Newbie forum, but no one answered.

But thanks anyway. I'll just spend another 50 hours or so working on it. I will either have a functioning system or a paperweight.
 
1 members found this post helpful.
Old 12-21-2009, 11:02 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Please stop the bickering about what should or should not have been posted and where---I was asked to split some of this out, but it is now too entangled for me to see what to move.

Gnusboy;

Please start a new thread with your specific question.
 
Old 12-21-2009, 05:54 PM   #12
dlinux
Member
 
Registered: Jan 2007
Location: Portugal
Distribution: Slackware 13.0
Posts: 52

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wje_lq View Post
dlinux, how's your situation coming along?
Hey, I still don't know why the program prints 2 newlines instead of 1. I am currently reading http://www.amazon.com/UNIX-Internals...1439458&sr=8-1 hoping to learn something that might help me with that.
 
Old 12-21-2009, 08:10 PM   #13
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by pixellany View Post
Please stop the bickering about what should or should not have been posted and where---I was asked to split some of this out, but it is now too entangled for me to see what to move.
That would be posts 3 through 8, post 10, post 13, and optionally post 11.
 
Old 12-21-2009, 08:23 PM   #14
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by dlinux View Post
Hey, I still don't know why the program prints 2 newlines instead of 1. I am currently reading http://www.amazon.com/UNIX-Internals...1439458&sr=8-1 hoping to learn something that might help me with that.
I browsed through the table of contents. You'll learn a lot about UNIX (and, by extension, Linux) from this book, but I don't think it'll help you with your current situation.

I tried compiling your program "as is" on Slackware 13.0, using this command:
Code:
cc -Wall 1.c -o 1
and got this compilation output:
Code:
1.c: In function 'main':
1.c:61: warning: implicit declaration of function 'grantpt'
1.c:63: warning: implicit declaration of function 'unlockpt'
1.c:100: warning: implicit declaration of function 'ptsname'
1.c:56: warning: unused variable 'fd'
1.c:55: warning: unused variable 'term'
1.c:54: warning: unused variable 'fp'
1.c:53: warning: unused variable 'i'
Fix the warnings, especially the first three. Do you still have the same problem?
 
1 members found this post helpful.
Old 12-21-2009, 09:55 PM   #15
dlinux
Member
 
Registered: Jan 2007
Location: Portugal
Distribution: Slackware 13.0
Posts: 52

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by wje_lq View Post
I browsed through the table of contents. You'll learn a lot about UNIX (and, by extension, Linux) from this book, but I don't think it'll help you with your current situation.

I tried compiling your program "as is" on Slackware 13.0, using this command:
Code:
cc -Wall 1.c -o 1
and got this compilation output:
Code:
1.c: In function 'main':
1.c:61: warning: implicit declaration of function 'grantpt'
1.c:63: warning: implicit declaration of function 'unlockpt'
1.c:100: warning: implicit declaration of function 'ptsname'
1.c:56: warning: unused variable 'fd'
1.c:55: warning: unused variable 'term'
1.c:54: warning: unused variable 'fp'
1.c:53: warning: unused variable 'i'
Fix the warnings, especially the first three. Do you still have the same problem?
I fixed it(code in 1st post updated), just added the declarations at the top and removed the unused variables.

Same thing happens, I always get two newlines instead of one.
When and if I get this right, I will post the solution here.

Last edited by dlinux; 12-21-2009 at 10:00 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vpn poptop - CTRL: PTY read or GRE write failed (pty,gre)=(5,6) dezeque Linux - Networking 9 08-26-2016 10:39 AM
Extra newline at end of file? sayeo87 Linux - Software 7 09-17-2010 04:45 AM
LXer: Fedora's Extra Packages for Enterprise Linux the Extra Mile LXer Syndicated Linux News 0 06-20-2008 10:10 PM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM
Programming extra mouse buttons? dive Slackware 1 07-23-2005 12:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration