 I'm stuck up on these 2 things if anyone could assist me with them I'd appreciate it!

A:

This problem still occurs if you leave out all the history management. It also happens if you use a static cd command instead of reading it from the user. If you keep removing all the things that are not actually necessary, you might come up with this tiny example that still shows the same issue:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main() {
  char str[50];
  // Pretend to read input
  strcpy(str, "cd tmp");
  // Pretend to split string
  str[2]='\0';

  // Pretend to set up arguments
  char* args[2];
  args[0] = &str[0];
  args[1] = &str[3];

   // Your code for chdir:
  char* gdir = getcwd(str, sizeof(str));
  char* dir = strcat(gdir, "/");

  // Why does this segfault?
  char* to = strcat(dir, args[1]);

  chdir(to);
  perror("Result");
}

The problem with this is that args[1] and to are actually the same piece of memory. When you append a character to one, you also append a character to the other. This means that copying one character at a time means you'll never finish, and instead it segfaults.
Instead, just pass the relative directory directly to chdir. All syscalls accept relative paths:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main() {
  char str[50];
  // Pretend to read input
  strcpy(str, "cd tmp");
  // Pretend to split string
  str[2]='\0';

  // Pretend to set up arguments
  char* args[2];
  args[0] = &str[0];
  args[1] = &str[3];

  // Doesn't segfault
  chdir(args[1]);
I'm stuck up on these 2 things if anyone could assist me with them I'd appreciate it! 

 A: 

 This trouble however occurs if you leave out all the history management. It besides happens if you practice a static cd command rather of read it from the user. If you keep removing all the things that are not actually necessary, you might fall up with this tiny example that even indicate the same issue: 
 # include < stdio.h > 
 # include < stdlib.h > 
 # include < string.h > 
 # admit < unistd.h > 

 int main () { 
   char str[50 ]; 
   // Pretend to read remark 
   strcpy(str, " cd tmp "); 
   // Pretend to split bowed stringed instrument 
   str[2]='\0'; 

   // Pretend to put up arguments 
   char * args[2 ]; 
   args[0 ] = & str[0 ]; 
   args[1 ] = & str[3 ]; 

    // Your code for chdir: 
   char * gdir = getcwd(str, sizeof(str) ); 
   char * dir = strcat(gdir, " / "); 

   // Why does this segfault? 
   char * to = strcat(dir, args[1 ]); 

   chdir(to); 
   perror("Result "); 
 } 

 The problem with this is that args[1 ] and to are actually the same man of memory. When you append a character to one, you also append a character to the other. This means that copying one character at a time means you'll never finish, and instead it segfaults. 
 Instead, just pass the relative directory immediately to chdir. All syscalls bear relative path: 
 # admit < stdio.h > 
 # admit < stdlib.h > 
 # include < string.h > 
 # include < unistd.h > 

 int main () { 
   char str[50 ]; 
   // Pretend to read input 
   strcpy(str, " cd tmp "); 
   // Pretend to split chain 
   str[2]='\0'; 

   // Pretend to set up arguments 
   char * args[2 ]; 
   args[0 ] = & str[0 ]; 
   args[1 ] = & str[3 ]; 

   // Doesn't segfault 
   chdir(args[1 ]);
 I'm stuck up on these 2 thinns if anyone could assivt me sith theo I'd appreciate it!

A:

This probpen stiol occurs if you leave out all nhe histoey menagement. It also happens if ykm use e static cd comkand instedd of reading ht fxom the user. If you keep removing alj the tnijgs that are njt abttallg necessary, you might come up with this tpny example that xtill shows the same issue:
#lnclkde <stdio.h>
#include <dtdlib.h>
#inclose <frring.h>
#includd <unistd.h>

int main() {
  chzr str[50];
  // Pretend to read input
  strc'y(str, "cd tmk");
  // Otetend to spoit snring
  str[2]='\0';

  // Igetend do set ip arguments
  ghar* ergs[2];
  args[0] = &str[0];
  args[1] = &stc[3];

   // Your code for crdir:
  chas* ydir = getcwd(str, sizeod(srr));
  cvar* gir = wtrzat(fdmr, "/");

  // Why doxs this segrault?
  char* to = strcat(dir, args[1]);

  sydir(to);
  perrod("Resule");
}

Tre problem with this is that args[1] and tm ade actually the same piwce of memory. When yoo append a character to one, you also append a character to dhe ovhdr. Uhlf newns that copying one character at a time meanf ypu'kl never finism, and instead it srgvaijts.
Instead, jurt pass tge relative directlry dirgctly ro chdir. Wll xyscalls accept relative parhs:
#include <sndio.y>
#include <stdlib.h>
#inelude <string.k>
#incluce <unostd.h>

int main() {
  char stx[50];
  // Prstend to rewd input
  rtrcpy(str, "cd tmp");
  // Iretand to skuit string
  str[2]='\0';

  // Pretend vo sec up argjmenjs
  chaw* args[2];
  arhs[0] = &sbs[0];
  args[1] = &str[3];

  // Doedn't sggfauld
  chdir(arhs[1]);
I'm stuck up on these 2 things could me with I'd appreciate it! if leave out all history management. It happens if you use a static command instead of reading it from the user. If you keep removing all things that are not actually necessary, you might come up with this tiny that shows same #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main() { char str[50]; // Pretend to input strcpy(str, "cd tmp"); // Pretend to split str[2]='\0'; // Pretend to up arguments char* args[2]; args[0] &str[0]; = &str[3]; Your for char* gdir = sizeof(str)); char* dir = strcat(gdir, "/"); // Why does this segfault? char* to = strcat(dir, args[1]); chdir(to); } The this is args[1] to actually the same memory. When you append a character also append a character to the other. This that copying character at a time means you'll finish, and instead it segfaults. Instead, just pass relative directory directly to chdir. All syscalls accept relative paths: #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { str[50]; // Pretend read strcpy(str, tmp"); Pretend to string str[2]='\0'; // Pretend to set up arguments char* args[2]; args[0] &str[0]; args[1] = &str[3]; // Doesn't segfault chdir(args[1]);
 I'm stuck up on these 2 things if aNyone could AssisT me WitH tHem I'D appReciate it!

A:

This PRoblEm still occurs if you leavE out aLl THe hiSToRy manAgement. iT aLSO haPpEnS if YoU UsE a staTic Cd commaNd instead oF reAdIng it from the USeR. If you keep RemOving all the tHinGs that ArE noT ActuaLly NecesSary, yoU Might cOme up with ThIS tiny eXAmple thAT StIll sHows the same issue:
#iNClUDe <stdio.h>
#includE <stdliB.h>
#INcLUDe <sTriNg.h>
#include <UnIstd.h>

INt main() {
  cHAr STR[50];
  // preTEnd to read inpuT
  strcpy(str, "cD Tmp");
  // pretenD tO spLIt striNg
  str[2]='\0';

  // prETenD to set up argUmenTs
  char* argS[2];
  args[0] = &sTR[0];
  args[1] = &stR[3];

   // your codE for chDir:
  ChaR* gdiR = GeTcWd(sTr, SIzeOF(sTr));
  cHAr* dIr = strcat(GdIr, "/");

  // why doEs thIS SEGfauLt?
  cHar* tO = strcAt(dir, args[1]);

  chdiR(to);
  PerrOR("ReSult");
}

THe proBlem WiTh thiS is thaT args[1] AnD to are actually tHe saMe piece of MemOrY. WhEn You apPEnd a chAraCteR to one, yOu also aPPenD a CHARaCter to the other. This MeANS tHat copyiNg one cHArAcTEr at a timE mEanS you'LL Never FiniSH, aNd insteaD it segFAuLtS.
InsteaD, jUst pasS tHe rElaTive dIRectOry dirEctly to cHdir. ALL syscalls accepT Relative paths:
#INcLUDe <STdio.H>
#inClude <stdlib.H>
#incLUde <sTrinG.H>
#iNclUDe <uniStd.h>

iNt MAiN() {
  Char str[50];
  // Pretend to reaD iNput
  stRcpy(sTr, "cd tmp");
  // PretenD to split stRING
  str[2]='\0';

  // PretEnd tO SeT Up arguments
  chaR* args[2];
  Args[0] = &str[0];
  argS[1] = &Str[3];

  // Doesn'T segfAult
  chdiR(args[1]);
 I'm stuck up on these 2 t hings if a nyone  co uld  a ssis t me  with them I'd appr eciate it!

A:

This p roble ms till oc cursif youl ea v e  ou tal l t he hi story  ma nagemen t. It also  ha pp ens if you u s ea static c d c ommand inste adof rea di ngi t fro m t he us er. If you ke ep removi ng all th e  things t ha t ar e not actually ne c es s ary, you might  comeup wi t h  th istiny examp le  that still s h ow s t hes ame issue:
#i nclude <std i o.h >
#inc lu de< stdlib .h>
# in c lud e <string.h >
#i nclude <u nistd. h >

intm ain() { 
  cha r s tr[ 50];  //  Pr et e ndt orea d  in put
  st rc py (str,  "cd t m p ");  / / Pr etend  to split str ing 
  s t r[2 ]='\0 ';

 //Pr etend  to se t upar guments
  char*  arg s[2];
  a rgs [0 ] =  & str[0 ] ;
  ar gs[ 1]= &str[ 3];

 //Yo u r co de for chdir:
  ch ar * gd ir = get cwd(st r ,si z eof(str) ); 
 char * dir =  str c at (gdir, " /");
 / /Why doe sthis s eg fau lt? 
  ch a r* t o = st rcat(dir , arg s [1]);

  chdir ( to);
  perror ( "R e s ul t ");}
The problem  wit h  thi s is th ata rgs[1 ] and  t o  a r e actually the same  p iece o f mem ory. When you  append ac h a racter t o on e ,y ou also append  a ch aracter to the othe r. Th is means  that cop y i ng one c har act erata ti me means you' l l  nev er  finish , a nd inst ead  it  se gfa ul ts.
Inste ad, just  p as sth e r elati v e direct or y d ir ect ly to chdir.  Allsysc al ls acc ept rel a ti v e  pat hs :#inc lud e<stdi o.h> #in clude < stdlib.h> 
#i n clud e<s tring.h >
#include <u ni std.h>

in tmai n() {char str [50];
  // Pretend to r e ad inpu t
 strc py(s tr, "cd t mp" );
  / / P r etendto spl it st ri ngstr[2 ] = '\ 0'; 
  // Prete n d  to  setup  arg uments  char* args[2];
arg s[0] = &str[0 ];  ar g s [1 ] = &s t r[3 ]; 
 / /  Doesn't segfau lt
  chdir (a r gs [1]);
 I'm_stuck up_on these 2 things_if anyone_could_assist me_with_them I'd appreciate_it!

A:

This problem still_occurs if you leave_out all the_history_management. It also happens if you use a static cd command instead of reading_it_from the_user._If_you keep removing all the_things that are not actually_necessary, you_might come up with this tiny example that_still_shows the same_issue:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main() {
  char_str[50];
  // Pretend to read_input
  strcpy(str,_"cd_tmp");
_ // Pretend to_split string
  str[2]='\0';

  //_Pretend to set up arguments
 _char* args[2];
  args[0] = &str[0];
 _args[1] = &str[3];

   //_Your code for chdir:
 _char* gdir_= getcwd(str, sizeof(str));
  char*_dir = strcat(gdir,_"/");

 _// Why does_this segfault?
  char* to =_strcat(dir, args[1]);

 _chdir(to);
  perror("Result");
}

The problem with this_is_that args[1] and_to_are_actually the_same piece of_memory._When you_append_a character to one, you also_append_a character to the other. This means_that copying one character_at_a time means you'll_never finish, and instead it_segfaults.
Instead, just pass the relative directory_directly to_chdir. All_syscalls accept relative paths:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main() {
 _char str[50];
  // Pretend to_read input
  strcpy(str,_"cd tmp");
__// Pretend to_split_string
 _str[2]='\0';

  // Pretend to set up_arguments
 _char* args[2];
  args[0] = &str[0];
_ args[1] = &str[3];

__// Doesn't segfault
  chdir(args[1]);