Skip to content

Commit 31bdce4

Browse files
committed
Update: Add C version
1 parent e7439ad commit 31bdce4

File tree

9 files changed

+245
-0
lines changed

9 files changed

+245
-0
lines changed

src/copyclip.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "includes/copyclip.h"
2+
#include "includes/colors.h"
3+
#include "includes/os_detect.h"
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
#ifdef OS_WINDOWS
9+
#include <windows.h>
10+
#endif
11+
12+
void copy_to_clipboard_any(const char *text) {
13+
14+
#ifdef OS_LINUX
15+
const char *wayland = getenv("WAYLAND_DISPLAY");
16+
const char *x11 = getenv("DISPLAY");
17+
FILE *pipe = NULL;
18+
19+
if (wayland) {
20+
pipe = popen("wl-copy", "w");
21+
if (!pipe) {
22+
fprintf(stderr, RED "Error: no command wl-copy (Wayland)\n" RESET);
23+
exit(1);
24+
}
25+
fwrite(text, sizeof(char), strlen(text), pipe);
26+
pclose(pipe);
27+
} else if (x11) {
28+
// X11
29+
pipe = popen("xclip -selection clipboard", "w");
30+
if (!pipe) {
31+
fprintf(stderr, RED "Error: no command xclip (X11)\n" RESET);
32+
exit(1);
33+
}
34+
fwrite(text, sizeof(char), strlen(text), pipe);
35+
pclose(pipe);
36+
} else {
37+
fprintf(stderr, RED "Error: no display environment detected (not Wayland or X11)\n" RESET);
38+
exit(1);
39+
}
40+
41+
#elif defined(OS_MAC)
42+
FILE *pipe = popen("pbcopy", "w");
43+
if (!pipe) {
44+
fprintf(stderr, RED "Error: command pbcopy not found.\n" RESET);
45+
exit(1);
46+
}
47+
fwrite(text, sizeof(char), strlen(text), pipe);
48+
pclose(pipe);
49+
50+
#elif defined(OS_WINDOWS)
51+
const size_t len = strlen(text) + 1;
52+
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
53+
if (!hMem) {
54+
fprintf(stderr, RED "Error: unable to allocate memory for clipboard.\n" RESET);
55+
return;
56+
}
57+
memcpy(GlobalLock(hMem), text, len);
58+
GlobalUnlock(hMem);
59+
if (OpenClipboard(NULL)) {
60+
EmptyClipboard();
61+
SetClipboardData(CF_TEXT, hMem);
62+
CloseClipboard();
63+
} else {
64+
fprintf(stderr, RED "Error: could not open clipboard.\n" RESET);
65+
GlobalFree(hMem);
66+
}
67+
68+
#else
69+
fprintf(stderr, RED "Clipboard not supported on this platform.\n" RESET);
70+
#endif
71+
}
72+

src/genpass.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "includes/genpass.h"
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <time.h>
5+
6+
char *generate_password(int length) {
7+
if (length <= 0)
8+
return NULL;
9+
10+
char *password = malloc(length + 1);
11+
if (!password)
12+
return NULL;
13+
14+
srand(time(NULL));
15+
if (length == 8) {
16+
char all_chars[512] = "";
17+
for (int i = 0; ARR_STRINGS[i] != NULL; i++) {
18+
strcat(all_chars, ARR_STRINGS[i]);
19+
}
20+
21+
for (int i = 0; i < length; i++) {
22+
password[i] = all_chars[rand() % strlen(all_chars)];
23+
}
24+
} else {
25+
for (int i = 0; i < length; i++) {
26+
const char *set = ARR_STRINGS[rand() % 5];
27+
password[i] = set[rand() % strlen(set)];
28+
}
29+
}
30+
31+
password[length] = '\0';
32+
return password;
33+
}

src/includes/colors.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef COLOR_H
2+
#define COLOR_H
3+
#define RESET "\033[0m"
4+
5+
#define BLACK "\033[0;30m"
6+
#define RED "\033[0;31m"
7+
#define GREEN "\033[0;32m"
8+
#define YELLOW "\033[0;33m"
9+
#define BLUE "\033[0;34m"
10+
#define MAGENTA "\033[0;35m"
11+
#define CYAN "\033[0;36m"
12+
#define WHITE "\033[0;37m"
13+
14+
#endif

src/includes/copyclip.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef COPYCLIP_H
2+
#define COPYCLIP_H
3+
4+
void copy_to_clipboard_any(const char* text);
5+
6+
#endif

src/includes/genpass.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef GENPASS_H
2+
#define GENPASS_H
3+
4+
#include "strings.h"
5+
6+
char* generate_password(int length);
7+
8+
#endif
9+

src/includes/os_detect.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef OS_DETECT_H
2+
#define OS_DETECT_H
3+
4+
#if defined(_WIN32) || defined(_WIN64)
5+
#define OS_WINDOWS 1
6+
#define OS_NAME "Windows"
7+
8+
#elif defined(__linux__)
9+
#define OS_LINUX 1
10+
#define OS_NAME "Linux"
11+
12+
#elif defined(__APPLE__) && defined(__MACH__)
13+
#define OS_MAC 1
14+
#define OS_NAME "macOS"
15+
16+
#elif defined(__unix__)
17+
#define OS_UNIX 1
18+
#define OS_NAME "Unix-like"
19+
20+
#else
21+
#define OS_UNKNOWN 1
22+
#define OS_NAME "Unknown"
23+
#endif
24+
25+
#endif // OS_DETECT_H
26+

src/includes/strings.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef STRINGS_H
2+
#define STRINGS_H
3+
4+
extern const char *LETTERS_UPPER;
5+
extern const char *LETTERS_LOWER;
6+
extern const char *NUMBERS;
7+
extern const char *SYMBOLS;
8+
extern const char *MIXED;
9+
extern const char *ARR_STRINGS[6];
10+
11+
void init_strings(void);
12+
13+
#endif
14+

src/main.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "includes/colors.h"
2+
#include "includes/genpass.h"
3+
#include "includes/strings.h"
4+
#include "includes/copyclip.h"
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
#define VERSION "1.0.0"
10+
11+
int main(int argc, char *argv[]) {
12+
if (argc != 2) {
13+
fprintf(stderr, RED "Use: " RESET GREEN "%s <length>" RESET "\n", argv[0]);
14+
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
15+
return 1;
16+
}
17+
18+
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
19+
printf(GREEN "Password Generator " RESET "v%s\n", VERSION);
20+
printf("Written by: " CYAN "Jakepys\n" RESET);
21+
return 0;
22+
}
23+
24+
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
25+
printf("\n" CYAN "Password Generator " RESET "v%s\n", VERSION);
26+
printf("Usage: %s <length>\n\n", argv[0]);
27+
printf("Options:\n");
28+
printf(" -h, --help Show this help message\n");
29+
printf(" -v, --version Show program version\n\n");
30+
printf("Example:\n");
31+
printf(" %s 12\n\n", argv[0]);
32+
printf("This will generate a random password with 12 characters.\n");
33+
return 0;
34+
}
35+
36+
int length = atoi(argv[1]);
37+
38+
if (length < 8) {
39+
fprintf(stderr, RED "Error:" RESET " length must be at least 8.\n");
40+
return 1;
41+
}
42+
43+
init_strings();
44+
char *pass = generate_password(length);
45+
copy_to_clipboard_any(pass);
46+
printf(GREEN "Generated password: " RESET "%s\n", pass);
47+
48+
return 0;
49+
}
50+

src/strings.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "includes/strings.h"
2+
#include <stddef.h>
3+
4+
const char *LETTERS_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5+
const char *LETTERS_LOWER = "abcdefghijklmnopqrstuvwxyz";
6+
const char *NUMBERS = "0123456789";
7+
const char *SYMBOLS = "!@#$%^&*()_+-=[]{}|;:',.<>/?";
8+
const char *MIXED =
9+
"aZ1!bY2@cX3#dW4$eV5%fU6^gT7&hS8*iR9(jQ0)kP_lO+mN-nM=oL[pK]qJ{rI}sH|tG;"
10+
"uF:'vE,wD<xC>yB?zA.";
11+
12+
const char *ARR_STRINGS[6];
13+
14+
void init_strings(void) {
15+
ARR_STRINGS[0] = LETTERS_LOWER;
16+
ARR_STRINGS[1] = LETTERS_UPPER;
17+
ARR_STRINGS[2] = NUMBERS;
18+
ARR_STRINGS[3] = SYMBOLS;
19+
ARR_STRINGS[4] = MIXED;
20+
ARR_STRINGS[5] = NULL;
21+
}

0 commit comments

Comments
 (0)