diff options
Diffstat (limited to 'various/neoup/neoup.c')
-rw-r--r-- | various/neoup/neoup.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/various/neoup/neoup.c b/various/neoup/neoup.c new file mode 100644 index 0000000..0415954 --- /dev/null +++ b/various/neoup/neoup.c @@ -0,0 +1,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; +} |