forked from fifteenhex/smolutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch.c
More file actions
148 lines (116 loc) · 2.3 KB
/
Copy pathtouch.c
File metadata and controls
148 lines (116 loc) · 2.3 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
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
144
145
146
147
148
// SPDX-License-Identifier: GPL-3.0-or-later
#include "config.h"
#include "common.h"
#include "multicall.h"
static int prog_touch(int argc, char **argv, char **envp)
{
const char *path;
int __cleanup_fd fd = -1;
if (argc != 2)
return 1;
path = argv[1];
/* File doesn't exist, try to create it */
if (access(path, F_OK)) {
fd = creat(path, 0644);
if (fd < 0) {
error("Failed to create file\n");
return 1;
}
}
/* File exists, update timestamp(s) */
else {
#if 0 // utime/utimes is missing?
if (utime(path, NULL)) {
error("Failed to update timestamps\n");
return 1;
}
#endif
}
return 0;
}
static int prog_ln(int argc, char **argv, char **envp)
{
const char *target, *linkpath;
bool symbolic = false;
int ret;
char c;
while ((c = getopt(argc, argv, "s")) != -1) {
switch (c) {
case 's':
symbolic = true;
break;
}
}
target = (optind < argc) ? argv[optind++] : NULL;
if (!target)
return 1;
linkpath = (optind < argc) ? argv[optind++] : NULL;
if (!linkpath)
return 1;
ret = symbolic ? symlink(target, linkpath) : link(target, linkpath);
if (ret) {
error("ln() failed: %d\n", errno);
return 1;
}
return 0;
}
static int prog_mv(int argc, char **argv, char **envp)
{
return 0;
}
/* no recursive support for now */
static int prog_rm(int argc, char **argv, char **envp)
{
const char *path;
int ret;
if (argc != 2)
return 1;
path = argv[1];
ret = unlink(path);
if (ret) {
error("unlink() failed: %d\n", errno);
return 1;
}
return 0;
}
static int prog_rmdir(int argc, char **argv, char **envp)
{
const char *path;
int ret;
if (argc != 2)
return 1;
path = argv[1];
ret = rmdir(path);
if (ret) {
error("rmdir() failed: %d\n", errno);
return 1;
}
return 0;
}
static int prog_mkdir(int argc, char **argv, char **envp)
{
const char *path;
int ret;
if (argc != 2)
return 1;
path = argv[1];
ret = mkdir(path, 0755);
if (ret) {
error("mkdir() failed: %d\n", errno);
return 1;
}
return 0;
}
static const struct mutlicall_prog progs[] = {
{ "touch", prog_touch },
{ "ln", prog_ln },
{ "mv", prog_mv },
{ "mkdir", prog_mkdir },
{ "rm", prog_rm },
{ "rmdir", prog_rmdir },
};
int main (int argc, char **argv, char **envp)
{
MULTICALL_DISPATCH(argv[0], progs);
return 1;
}