This repository has no description
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186#include <stdlib.h>#include <stdio.h>#include <sys/stat.h>#include <unistd.h>#include <string.h>#include <dirent.h>
#ifdef DEBUG#define DBG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)#else#define DBG(fmt, ...)#endif
#define RED "\033[0;31m"#define GREEN "\033[0;32m"#define CLEAR "\033[0;0m"
enum destination { HOME, XDG_CONFIG_HOME, UNDEFINED };
int resolve_target_path(const char *file){ struct stat sb; char source[512];
snprintf(source, sizeof(source), "%s/dotfiles/%s", getenv("HOME"), file);
if (lstat(source, &sb) == -1) { return UNDEFINED; }
if (file[0] == '.') { return HOME; } else if (S_ISDIR(sb.st_mode)) { return XDG_CONFIG_HOME; }
return UNDEFINED;}
int is_link(const char *path){ struct stat sb;
if (lstat(path, &sb) == 0) { if (S_ISLNK(sb.st_mode)) { return 1; } }
return 0;}
void create_link(const char *file){ struct stat sb; char source[512]; char target[512];
switch (resolve_target_path(file)) { case HOME: snprintf(target, sizeof(target), "%s/%s", getenv("HOME"), file); break; case XDG_CONFIG_HOME: snprintf(target, sizeof(target), "%s/.config/%s", getenv("HOME"), file); break; default: fprintf(stderr, "%serror: could not resolve destination for %s%s\n", RED, source, CLEAR); exit(EXIT_FAILURE); break; }
snprintf(source, sizeof(source), "%s/dotfiles/%s", getenv("HOME"), file);
if (stat(source, &sb) == -1) { fprintf(stderr, "%snot found: %s%s\n", RED, source, CLEAR); exit(EXIT_FAILURE); }
if (is_link(target)) { printf("already linked\n"); exit(EXIT_SUCCESS); }
if (symlink(source, target) == 0) { printf("Changes:\n"); printf("\t%slinked: %s%s\n", GREEN, file, CLEAR); exit(EXIT_SUCCESS); }}
void remove_link(const char *file){ char target[512];
switch (resolve_target_path(file)) { case HOME: snprintf(target, sizeof(target), "%s/%s", getenv("HOME"), file); break; case XDG_CONFIG_HOME: snprintf(target, sizeof(target), "%s/.config/%s", getenv("HOME"), file); break; default: fprintf(stderr, "%snot found: %s%s\n", RED, file, CLEAR); exit(EXIT_FAILURE); break; }
if (!is_link(target)) { printf("already unlinked\n"); exit(EXIT_SUCCESS); }
if (unlink(target) == 0) { printf("Changes:\n"); printf("\t%sunlinked: %s%s\n", RED, file, CLEAR); exit(EXIT_SUCCESS); }}int main(int argc, char *argv[]){ char path[512]; int opt; char *arg;
if (getenv("HOME") == NULL) { fprintf(stderr, "%s\n", "variable not set: $HOME"); exit(EXIT_FAILURE); }
while ((opt = getopt(argc, argv, "f:")) == 0) { switch (opt) { case 'f': break; default: fprintf(stderr, "usage: pt [link | unlink] <file>\n"); exit(EXIT_FAILURE); } }
if (argv[optind] == NULL) { fprintf(stderr, "usage: pt [link | unlink] <file>\n"); exit(EXIT_FAILURE); }
char *cmd = argv[optind]; if (strcmp(cmd, "link") == 0) { if (argv[optind + 1] == NULL) { fprintf(stderr, "missing argument: <file>\n"); exit(EXIT_FAILURE); } create_link(argv[optind + 1]); } else if (strcmp(cmd, "unlink") == 0) { if (argv[optind + 1] == NULL) { fprintf(stderr, "missing argument: <file>\n"); exit(EXIT_FAILURE); } remove_link(argv[optind + 1]); } else if (strcmp(cmd, "status") == 0) { char source[512]; struct dirent *d;
snprintf(source, sizeof(source), "%s/dotfiles", getenv("HOME"));
printf("Using directory %s\n\n", source); printf("Unlinked files:\n");
DIR *dir = opendir(source); while ((d = readdir(dir)) != NULL) { if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) { continue; }
printf("\t%s%s%s\n", RED, d->d_name, CLEAR); } }}