lab5 Pipe Signals
  • Introduction
  • Signals
    • kill() system call
    • Custom signal handler
  • Knowing about pipe
    • Pipe Overview
    • System call pipe()
  • Learn to use pipe
    • Pipe Creation
    • Pipe with fork
    • Exercise 1
  • Connecting two programs with pipe
    • Implement ls | less in C
  • Shell Examples
    • Get Current Path
    • Change Directory
    • Change Environment Variables
  • Reference
Powered by GitBook
On this page

Was this helpful?

  1. Shell Examples

Change Directory

We can use chdir() to change the current working directory (cwd) of the current process.

#include <unistd.h>
int chdir(const char *path);
/* Shell/chdir.c */
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
int main(int argc,char *argv[])
{ 
    char buf[PATH_MAX+1]; 
    char input[255]; 
    if(getcwd(buf,PATH_MAX+1) != NULL) { 
        printf("Now it is %s\n",buf); 
        printf("Where do you want to go?:");         
        fgets(input,255,stdin); 
        input[strlen(input)-1] = '\0'; 
        if(chdir(input) != -1) { 
            getcwd(buf,PATH_MAX+1); 
            printf("Now it is %s\n",buf); 
        } 
        else { 
            printf("Cannot Change Directory\n"); 
        } 
    } 
return 0;
}
PreviousGet Current PathNextChange Environment Variables

Last updated 5 months ago

Was this helpful?