-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirutils.c
More file actions
58 lines (50 loc) · 1.04 KB
/
dirutils.c
File metadata and controls
58 lines (50 loc) · 1.04 KB
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
#include <pwd.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define max_cwd 256
void current_working_directory()
{
char cwd[max_cwd] = {'\0'};
if (getcwd(cwd, sizeof(cwd)) != NULL)
fprintf(stdout, "Current working dir: %s\n", cwd);
else
perror("getcwd() error");
}
void change_directory(char *buffer)
{
char delimiter[] = " ";
char *path;
char *inptcpy;
int inptlen;
/* Copy buffer for parsing */
inptlen = strlen(buffer);
inptcpy = (char*) calloc(inptlen + 1, sizeof(char));
strncpy(inptcpy, buffer, inptlen);
/* Get second word (path) */
strtok (inptcpy, delimiter);
path = strtok (NULL, delimiter);
if (strcmp(buffer,"cd ..") == 0)
chdir("../");
else if (strcmp(buffer,"cd") == 0 || strcmp(buffer,"cd ~") == 0)
chdir(getenv("HOME"));
else
chdir(path);
if (inptcpy)
free(inptcpy);
}
/*
void list_directory()
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d) {
while ((dir = readdir(d)) != NULL)
printf("%s\n", dir->d_name);
closedir(d);
}
}
*/