Hi
This is, i think, the relevant part of my C program that segfaults straight after processing options:
Code:
char *usr_nam=0;
char *host=0;
int main(int argc, char *argv[])
{
int len, l;
int n=0, c;
char *cp, *service = "12349";
char send_auth[128], auth_ok[1];
int err, err2, zero;
struct passwd *pwd;
sigset_t childmask, oldsigmask;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
printf("Starting getopt: \n");
opterr = 0; /* don't want getopt() writing to stderr */
while ((c = getopt(argc, argv, "h:l:")) != -1) {
switch (c) {
case 'l':
usr_nam = optarg;
printf("usr_nam is %s\n", usr_nam);
break;
case 'h':
host = optarg;
printf("Host is %s\n", host);
break;
}
}
/*if (optind >= argc)
perror("usage: pty [ -d driver -einv ] program [ arg ... ]");*/
argc -= optind;
argv += optind;
printf("We are here");
/* We will be writing to sockets so block SIG_PIPE */
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &sa, NULL) < 0)
perror("sigaction failed");
The output that i get from this is:Starting getopt:
Code:
Host is localhost
usr_nam is chuck
Segmentation fault: 11
The code always segfaults whether or not i initialise *host and *usr_nam to NULL or not!
Please could someone let me know or help me regarding why this programming is segfaulting on the printf statement following the option processing code?
Thankx in advance for any help/replies!