[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

cd erstatning



Har fået en lille sjov opgave på studiet hvor vi skal lave en erstatning
til kommandoen "cd" i C ved hjælp af systemkald.
Jeg ved ikke helt hvordan det kan gribes an, men startede med at tænke at
jeg nok skulle bruge chdir() og så opdatere variablen PWD med setenv().
Dette resulterede i det her:

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

int main(int argc, char *argv[])
{
  if(argc != 2)
  {
    printf("Usage is: chdir <directory>\n");
    exit(1);
  }

  printf("Trying to change directory to %s\n", argv[1]);
  int r = chdir(argv[1]);

  if(r != 0)
  {
    printf("An error has occured\n");
    exit(1);
  }

  char *buf = getcwd(NULL,0);

  if(setenv("PWD",buf,1) != 0)
  {
    printf("Error");
    exit(1);
  }

  printf("Directory succesfulled changed to %s\n", buf);
  exit(0);
}

Det kører og fuldfører uden fejl, men det aktuelle dir bliver ikke
opdateret. Jeg står stadig i samme dir og environment variablen PWD er
ikke opdateret. Er det helt anden tilfaldsvinkel jeg skal ind på?

-- 
vh
Jeppe W. Larsen

"Logic is the beginning of wisdom; not the end."



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-01-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *