user3358064
user3358064

Reputation: 7

Printing out the current working directory

Below I have a tokenizer that I am trying to turn into a shell program. I am just beginning so I know that the program isn't set up to take any shell commands but I am having problems with just printing out the current working directory. I will show my code below:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
{
pid_t pid;
char cwd[1000];
if (getcwd, sizeof(cwd) != NULL)
{
    fprintf(stdout, "Current working dir: %s\n");
    return 0;
}
printf("Please enter a string\n");
int ch = fgetc(stdin);
while (ch != EOF)
{
    pid = fork();
    if (pid == 0)
    {
        printf("Child Working");

    }
    else
        printf("Child not working");
while (isspace(ch))
{

  // If only 1 line of input allowed, then add
  if (ch == '\n') return 0;;

  ch = fgetc(stdin);
}
if (ch != EOF)
    {
  do
  {
    fputc(ch, stdout);
    ch = fgetc(stdin);
  }
  while (ch != EOF && !isspace(ch));
  fputc('\n', stdout);
    }
  }
  return 0;
}

The output I am getting so far is in a screen capture below:

enter image description here

Upvotes: 0

Views: 3880

Answers (1)

Iłya Bursov
Iłya Bursov

Reputation: 24146

change

if (getcwd, sizeof(cwd) != NULL)

to:

if (getcwd(cwd, sizeof(cwd)) != NULL)

and

fprintf(stdout, "Current working dir: %s\n");

to:

printf("Current working dir: %s\n", cwd);

Upvotes: 1

Related Questions