• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

C Programming: Code Review

I am getting extra characters in the ouput of my program. The "t" is an example of the extra character I'm getting. Not sure what can be causing this. I though maybe buffer overflow, so I removed all functions like strcpy and strcat from the readInput method, but the problem is still happening.

Here is a sampleoutput:

$ ./yak /dev/pts/3
pz951772@/dev/pts/3: test
pz951772@/dev/pts/3: test
pz951772@/dev/pts/3: hi
pz951772@/dev/pts/3: hi

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <pwd.h>

static int writeLine(const char *b, const char *u, int filedes);
static char* readInput(const char *u, char buffer[]);
static char* getUser();

int main( int argc, char *argv[] ) {

   int fd;
   int max_input = 200;
   char buffer[max_input];
   //char *buf = malloc(sizeof(char) * 1024);

   // get username
   char *myUser = getUser();

   //open file
   fd = open(argv[1], O_RDWR);

   while(!feof(stdin)) {
     char *buf = readInput(myUser, buffer);
     if(writeLine(buf, myUser, fd) < 0)

static char* getUser() {
   struct passwd *mypasswd;
   char *mytty;

   if( (mypasswd = getpwuid(geteuid())) != NULL ) {
      char *user = mypasswd->pw_name;

      if(( mytty = ttyname(STDIN_FILENO)) == NULL)
         perror("ttyname() error");

      int userLen = strlen(user);
      int ttyLen = strlen(mytty);
      char *usr = malloc(sizeof(char) * (userLen + ttyLen + 5));

      strcpy(usr, user);
      strcat(usr, "@");
      strcat(usr, mytty);
      strcat(usr, ": ");

      return usr;

static char* readInput(const char *u, char buffer[]) {

   ssize_t bytes_read;
   size_t count = 200;

   //buffer[0] = '\0';
   //printf("%s", u);

   // display UserName
   if(write(STDOUT_FILENO, u, strlen(u)) < 0)
      perror("write() error");

   // read
   if(( bytes_read = read(STDIN_FILENO, buffer, count)) == -1)
        perror("read() error");
   //fgets(buffer, count, stdin);

   return buffer;

static int writeLine(const char *b, const char *u, int filedes) {
   size_t ulen, blen;

   blen = strlen(b);
   ulen = strlen(u);

   if(write(filedes, u, ulen) < 0 || write(filedes, b, blen) < 0 ) {
        perror("write() error");
        return -1;
   return 0;

Open in new window

1 Solution
// read
   if(( bytes_read = read(STDIN_FILENO, buffer, count-1)) == -1){
        perror("read() error");
pzozulkaAuthor Commented:
I was wondering if you could also help me figure out why ^d doesn't quit the program.

It use to work (line 28) when I was using buffered IO, but now I'm using raw IO and it no longer works. Any suggestions?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Tackle projects and never again get stuck behind a technical roadblock.
Join Now