-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.c
77 lines (65 loc) · 1.44 KB
/
misc.c
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
#include "misc.h"
void error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, " ");
perror(NULL);
va_end(args);
exit(1);
}
bool prompt(char *fmt, ...)
{
char c;
va_list args;
va_start(args, fmt);
vfprintf(stdout, fmt, args);
va_end(args);
c = getchar();
if (c == 'y')
return true;
else
return false;
}
void usage(void)
{
printf("cli usage: COMMAND [FLAGS] ([FILE(s)])\n\n"
"rm\tdelete a file.\n"
"ls\tlist directory contents.\n"
"mkdir\tcreat a directory.\n"
"cat\tprint contents of a file.\n"
"cd\tchange working directory.\n\n"
"appending \" -h\" to most commands will give\n"
"additional info regarding that command\n\n");
}
char *strdup(const char *s)
{
char *d = malloc(strlen(s) + 1);
if (d == NULL)
return NULL;
strcpy(d, s);
return d;
}
int is_dir(const char *file)
{
struct stat statbuf;
stat(file, &statbuf);
return S_ISDIR(statbuf.st_mode);
}
int create_file(const char *name)
{
int fd;
if ((fd = creat(name, S_IRWXU)) == -1)
error("can't create file %s.", name);
return fd;
}
int open_file(const char *name, int flag)
{
int fd;
fd = open(name, flag);
if (fd == -1)
error("can't open file %s.", name);
return fd;
}