Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
141 Kevin 1
/*
2
 * An example plug-in, which implements the 'cd' command.
3
 */
4
#include <stdbool.h>
5
#include <stdio.h>
6
#include <pwd.h>
7
#include <unistd.h>
8
#include <sys/types.h>
9
#include "../esh.h"
10
#include <signal.h>
11
#include "../esh-sys-utils.h"
12
 
13
static bool 
14
init_plugin(struct esh_shell *shell)
15
{
16
    printf("Plugin 'cd' initialized...\n");
17
    return true;
18
}
19
 
20
/* Implement chdir built-in.
21
 * Returns true if handled, false otherwise. */
22
static bool
23
chdir_builtin(struct esh_command *cmd)
24
{
25
    if (strcmp(cmd->argv[0], "cd"))
26
        return false;
27
 
28
    char *dir = cmd->argv[1];
29
    // if no argument is given, default to home directory
30
    if (dir == NULL) {
31
        struct passwd *pw = getpwuid(getuid());
32
        if (pw == NULL) {
33
            esh_sys_error("Could not obtain home directory.\n"
34
                          "getpwuid(%d) failed: ", getuid());
35
        } else {
36
            dir = pw->pw_dir;
37
        }
38
    }
39
 
40
    if (chdir(dir) != 0)
41
        esh_sys_error("chdir: ");
42
 
43
    return true;
44
}
45
 
46
struct esh_plugin esh_module = {
47
  .rank = 1,
48
  .init = init_plugin,
49
  .process_builtin = chdir_builtin
50
};