summary refs log tree commit diff stats
path: root/various/neoup/neoup.c
blob: 8f26738cf72f05cc726f54ef9f6e6962c8ec31c0 (plain) (blame)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include "config.h"

#define MAXPATH 1024
#define MAXCMD 4096

int
upfile(char *name, char *rdir)
{
	char *cmd = calloc(MAXCMD, sizeof(char));
	snprintf(cmd, MAXCMD, "curl -u %s:%s -F \"%s/%s=@%s\" %s",
		username, password, rdir, name, name, url);

	printf("%s/%s\n", rdir, name);
	/*printf("\t%s\n", cmd);*/
	system(cmd);

	free(cmd);
	return 0;
}

int
updir(DIR *dirp, char *rdir, char *dir)
{
	DIR *nestdirp;
	struct dirent *dp;
	char *name = calloc(MAXPATH, sizeof(char));
	while ((dp = readdir(dirp)) != NULL) {
		if (strcmp(".", dp->d_name) == 0
			|| strcmp("..", dp->d_name) == 0)
			continue;
		if (strcmp(".", dir) != 0) {
			strcpy(name, dir);
			if (name[strlen(name) - 1] != '/')
				strcat(name, "/");
			strcat(name, dp->d_name);
		}
		else
			strcpy(name, dp->d_name);
		nestdirp = opendir(name);
		if (nestdirp == NULL)
			upfile(name, rdir);
		else if (nestdirp != NULL) {
			updir(nestdirp, rdir, name);
		}		
	}

	free(name);
	closedir(dirp);
	return 0;
}

/* Upload to a neocities directory a local file or directory. */
int
main(int argc, char **argv)
{
	char *rdir;
	rdir = calloc(MAXPATH, sizeof(char));
	char *local;
	local = calloc(MAXPATH, sizeof(char));
	/* In case getcwd is more appropriate than simply '.'.
	char *dir = calloc(MAXPATH, sizeof(char));
	getcwd(dir, MAXPATH * sizeof(char));*/
	local[0] = '.';
	int i;
	for (i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "-r") == 0) {
			if (i < argc - 1) {
				++i;
				rdir = argv[i];
			}
			else {
				puts("Supply a directory"
					" name after -r");
				return 1;
			}
		} else if (strcmp(argv[i], "-f") == 0) {
			if (i < argc - 1) {
				++i;
				local = argv[i];
			}
			else {
				puts("Supply a file or directory"
					" name after -f");
				return 2;
			}
		}
		else {
			puts(argv[i]);
			printf("%s [-r neocities directory]"
				" [-f local file or directory]\n",
				argv[0]);
			return -1;
		}
	}
	int tmp = strlen(rdir) - 1;
	if (rdir[tmp] == '/' && tmp > 0)
		rdir[tmp] = '\0';
	/*if (rdir[0] == '\0')
		printf("%s ", "/");
	else
		printf("%s ", rdir);
	printf("%s\n", local);*/

	DIR *dirp = opendir(local);
	if (dirp != NULL) {
		updir(dirp, rdir, local);
	} else if (access(local, R_OK) == 0) {
		upfile(local, rdir);
	} else {
		printf("I can't open this!\n");
		return 3;
	}

	return 0;
}