ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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
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?
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.
And I suppose someone will now tell me I am in the wrong place to ask such stupid questions.
You're half right.
You're wrong, in that your question is not stupid. It's an excellent question.
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.
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.
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.
You're wrong, in that your question is not stupid. It's an excellent question.
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.
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.
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.
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.
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.
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 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.