This repository was archived by the owner on Feb 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
72 lines (62 loc) · 1.75 KB
/
Copy pathcode.c
File metadata and controls
72 lines (62 loc) · 1.75 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
// Size of `code` command, including space and NUL
#define CMD_SIZE 6
int main(int argc, char *argv[])
{
// Check for too many arguments
if (argc > 2)
{
printf("\033[0;31mError\033[0m: expected none or only one argument, but %i were provided.\n", argc - 1);
printf("Usage: code <file>\n");
return 1;
}
// Check for no argument
else if (argc == 1)
{
// Open VSCode in current directory
system("code .");
return 0;
}
// Check for argument as a directory
else if (argc == 2)
{
// Check if the argument is a directory
struct stat sb;
if (stat(argv[1], &sb) == 0)
{
if (argc == 2 && S_ISDIR(sb.st_mode))
{
// Pass the folder to (actual) code command as an argument
char *cmd_dir = malloc(CMD_SIZE + sizeof(argv[1]));
sprintf(cmd_dir, "code %s", argv[1]);
system(cmd_dir);
free(cmd_dir);
return 0;
}
}
// Check for argument as current directory (.)
else if (strcmp(argv[1], ".") == 0)
{
// Open VSCode in current directory
system("code .");
return 0;
}
}
// Create a new file with provided name
char *filename = argv[1];
FILE *file = fopen(filename, "a");
if (file == NULL)
{
printf("\033[0;31mError\033[0m: Something went wrong while creating %s\n", filename);
return 1;
}
fclose(file);
char *cmd = malloc(CMD_SIZE + sizeof(filename));
sprintf(cmd, "code %s", filename);
system(cmd);
free(cmd);
return 0;
}