-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopr.c
162 lines (148 loc) · 3.78 KB
/
copr.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <linux/limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void printHelp();
void copy(const char *arg, const char *cwd);
void paste();
int isDir(const char *path);
void showClipboardContents();
void clearClipboard() { remove("/tmp/copr.txt"); }
int main(int argc, char *argv[]) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
perror("getcwd() error");
return 1;
}
if (argc < 2 || argc > 3) {
printf("Copr: invalid arguments\n");
printf("For help: copr -h\n");
return 0;
}
if (argc == 2) {
if (strcmp(argv[1], "-c") == 0) {
copy("null", cwd);
} else if (strcmp(argv[1], "-p") == 0) {
paste();
} else if (strcmp(argv[1], "-h") == 0) {
printHelp();
} else if (strcmp(argv[1], "-r") == 0) {
clearClipboard();
printf("Copr: clipboard cleared\n");
} else if (strcmp(argv[1], "-d") == 0) {
showClipboardContents();
} else {
printf("Copr: unknown arg\n");
}
} else if (argc == 3) {
if (strcmp(argv[1], "-c") == 0) {
copy(argv[2], cwd);
} else if (strcmp(argv[1], "-p") == 0) {
printf("Copr: too many args\n");
} else if (strcmp(argv[1], "-h") == 0) {
printHelp();
} else if (strcmp(argv[1], "-r") == 0) {
clearClipboard();
printf("Copr: clipboard cleared\n");
} else if (strcmp(argv[1], "-d") == 0) {
showClipboardContents();
} else {
printf("Copr: unknown arg\n");
}
}
return 0;
}
void printHelp() {
printf("Copr: usage: copr [-c] [-p] [-r] [file]\n");
printf("Copr: -c: copy file or current directory\n");
printf("Copr: -p: paste file\n");
printf("Copr: -d: show clipboard contents\n");
printf("Copr: -r: remove file from clipboard\n");
}
void copy(const char *arg, const char *cwd) {
// var to hold full path
char path[PATH_MAX];
// if arg is none then copy current directory
if (strcmp(arg, "null") == 0) {
strcpy(path, cwd);
printf("Copr copied: %s\n", path);
}
// else copy path and add filename
else {
strcpy(path, cwd);
strcat(path, "/");
strcat(path, arg);
if (access(path, F_OK) != 0) {
printf("Copr: file does not exist\n");
exit(1);
}
printf("Copr copied: %s\n", path);
}
// remove file if it exists
clearClipboard();
// open file and write path
FILE *file;
file = fopen("/tmp/copr.txt", "w");
if (file == NULL) {
printf("copr: error copying\n");
exit(1);
}
fprintf(file, "%s", path);
fclose(file);
}
// returns 1 if arg is a directory else 0
int isDir(const char *path) {
struct stat statbuf;
if (stat(path, &statbuf) != 0) {
return 0;
}
return S_ISDIR(statbuf.st_mode);
}
void paste() {
// var to hold path from file
char path[PATH_MAX];
// open file and read path
FILE *file;
file = fopen("/tmp/copr.txt", "r");
if (file == NULL) {
printf("Copr: Nothing in clipboard\n");
exit(1);
}
fscanf(file, "%s", path);
fclose(file);
// check if file or dir
bool dir = isDir(path);
// copy file or dir to current directory
if (dir == 1) {
char *cmd = malloc(strlen("cp -r ") + strlen(path) + strlen(" .") + 1);
strcpy(cmd, "cp -r ");
strcat(cmd, path);
strcat(cmd, " .");
system(cmd);
free(cmd);
} else {
char *cmd = malloc(strlen("cp ") + strlen(path) + strlen(" .") + 1);
strcpy(cmd, "cp ");
strcat(cmd, path);
strcat(cmd, " .");
system(cmd);
free(cmd);
}
printf("Copr pasted: %s\n", path);
}
void showClipboardContents() {
char path[PATH_MAX];
FILE *file;
file = fopen("/tmp/copr.txt", "r");
if (file == NULL) {
printf("Copr: Nothing in clipboard\n");
exit(1);
}
fscanf(file, "%s", path);
fclose(file);
printf("Copr: %s\n", path);
}