summaryrefslogtreecommitdiffstats
path: root/hw1/shell-builtin.c
blob: 87d0632a260c1a125f3bd5ea376b8a23ed1aff47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "shell-builtin.h"

#include "xwrap.h"

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

const RasShellBuiltin ras_shell_builtins[] = {
    { "cd",          ras_shell_builtin_cd           },
    { "echo",        ras_shell_builtin_echo         },
    { "exit",        ras_shell_builtin_exit         },
    { "logout",      ras_shell_builtin_logout       },
    { "printenv",    ras_shell_builtin_printenv     },
    { "pwd",         ras_shell_builtin_pwd          },
    { "setenv",      ras_shell_builtin_setenv       },
    { "umask",       ras_shell_builtin_umask        },
    {  NULL,         NULL                           }
};

int ras_shell_builtin_cd (int argc, char* argv[], RasShell* shell) {
    if (shell->attr_rshell) {
        fprintf (stderr, "%s: you are not permitted to run this command\n", argv[0]);
        return 1;
    }
    char* dest = argv[1];
    if (dest == NULL) {
        dest = getenv ("HOME");
        if (dest == NULL) {
            dest = ".";
        }
    }
    if (chdir (dest) < 0) {
        fprintf (stderr, "cd: %s: %s\n", dest, strerror (errno));
        return 1;
    }
    return 0;
}

int ras_shell_builtin_echo (int argc, char* argv[], RasShell* shell) {
    for (int i = 1; argv[i] != NULL; i++) {
        if (i > 1) {
            putchar (' ');
        }
        fputs (argv[i], stdout);
    }
    putchar ('\n');
    return 0;
}

int ras_shell_builtin_exit (int argc, char* argv[], RasShell* shell) {
    if (argc > 2) {
        fprintf (stderr, "%s: too many arguments\n", argv[0]);
        return 1;
    }
    if (argc < 2) {
        shell->req_exit_status = 0;
    } else if (sscanf (argv[1], "%d", &shell->req_exit_status) < 1) {
        fprintf (stderr, "%s: `%s\' is not a number\n", argv[0], argv[1]);
        return 1;
    }
    shell->req_exit = true;
    return 0;
}

int ras_shell_builtin_logout (int argc, char* argv[], RasShell* shell) {
    if (!shell->attr_login) {
        fprintf (stderr, "%s: not a login shell\n", argv[0]);
        return 1;
    }
    return ras_shell_builtin_exit (argc, argv, shell);
}

int ras_shell_builtin_printenv (int argc, char* argv[], RasShell* shell) {
    if (argc > 2) {
        fprintf (stderr, "%s: too many arguments\n", argv[0]);
        return 1;
    }
    if (argc < 2) {
        extern char** environ;
        for (int i = 0; environ[i] != NULL; i++) {
            puts (environ[i]);
        }
    } else {
        char* res = getenv (argv[1]);
        if (res == NULL) {
            return 1;
        }
        puts (res);
    }
    return 0;
}

int ras_shell_builtin_pwd (int argc, char* argv[], RasShell* shell) {
    char* cwd = xgetcwd ();
    if (cwd == NULL) {
        fprintf (stderr, "%s: cannot get working directory: %s\n", argv[0], strerror (errno));
        return 1;
    }
    puts (cwd);
    free (cwd);
    return 0;
}

int ras_shell_builtin_setenv (int argc, char* argv[], RasShell* shell) {
    if (argc != 3) {
        fprintf (
            stderr, "%s: too %s arguments\n", argv[0], argc > 3 ? "many" : "few");
        return 1;
    }
    return - setenv (argv[1], argv[2], 1);
}

int ras_shell_builtin_umask (int argc, char* argv[], RasShell* shell) {
    if (argc > 2) {
        fprintf (stderr, "%s: too many arguments\n", argv[0]);
        return 1;
    }

    if (argc < 2) {
        mode_t old_mode = umask (0777);
        umask (old_mode);
        printf ("%04o\n", old_mode);
    } else {
        int mode_int;
        if (sscanf (argv[1], "%o", &mode_int) < 0) {
            fprintf (stderr, "%s: `%s\' is not a valid octal number\n", argv[0], argv[1]);
            return 1;
        }
        mode_t new_mode = mode_int;
        umask (new_mode);
    }
    return 0;
}