c - printf is causing a segfault with getlogin() -
i'm new c, apologize if answer obvious, i've searched elsewhere.
the libraries i'm including are:
#include <unistd.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/wait.h> #include <fcntl.h> #include <sys/stat.h>
the code failing is:
char *user = getlogin(); char cwd[128]; if (user == null) printf("cry\n"); getcwd(cwd, sizeof(cwd)); printf("this prints\n"); printf(user); printf("this not\n"); printf("%s@myshell:%s> ", user, cwd);
cry not print, should mean getlogin successful. segfault caused on printf(user);
further testing shows folling block prints entirely
printf("this prints\n"); printf(user); printf("this prints\n");
but folling block print prints end segfault without showing user
printf("this prints\n"); printf(user);
edit:
sorry wasting time. accidentally deleted fgets supposed follow , causing segfault. i've been on bug couple hours now, love when problem small.
thanks
you should check getcwd return value. according man page of getcwd:
if length of absolute pathname of current working directory, including terminating null byte, exceeds size bytes, null returned, , errno set erange; application should check error, , allocate larger buffer if necessary.
Comments
Post a Comment