#ifdef UNIX
#include <unistd.h>
#else
#include <windows.h>
#include <direct.h>
#define getcwd _getcwd
#define chdir _chdir
#endif

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#include "yanling.h"

int printPrompt(){
  char buf[128];
  char * bigpath;
  unsigned int size=2048;


  buf[127]=0;
  if(getcwd(buf, 127)){
    printf("%s$", buf);
  }
  else{
    bigpath = malloc(sizeof(char)*2048);
    bigpath[2047] = 0;
    if (getcwd(bigpath, size)){
      printf("%s$", bigpath);
    }
    else {
      printf("You are crazy to have such a long path!$");
    }
    free(bigpath);
  }
  return 0;
}

int cd(int argc, char * const argv []){
#ifdef UNIX
#else
  char * buf;
#endif
  if (argc > 2) {
    fprintf(stderr, "too many arguments for cd! \n");
  }
  else if (argc == 1){
#ifdef UNIX
    if (chdir(getenv("HOME"))){
      fprintf(stderr, "shell: cd: wrong Home directory \n");
    }
#else
	buf = getcwd(NULL, 2048);
	if (buf == NULL){
	}
	else{
		printf("%s\n", buf);
		free(buf);
	}
#endif

  }else if (argc ==2){
    if (chdir(argv[1])){
		perror("shell: cd");
    }
  }
  return 0;
}
