Files
z/z_prompt_command.c
T

97 lines
2.6 KiB
C
Raw Normal View History

2014-03-26 11:38:00 -04:00
/* PROMPT_COMMAND for z.sh */
2013-12-24 17:33:45 -05:00
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2013-12-25 00:17:17 -05:00
#include <sys/stat.h>
2013-12-24 17:33:45 -05:00
#include <time.h>
#include <unistd.h>
2013-12-25 00:17:49 -05:00
int main(int __unused argc, char **argv) {
2013-12-24 23:55:19 -05:00
2013-12-24 23:56:45 -05:00
if (argv[1] == NULL) return EXIT_FAILURE;
2013-12-24 23:55:19 -05:00
2013-12-25 13:30:46 -05:00
static const float MULTIPLIER = 0.99;
static const int RANK_SUM_MAX = 6000;
2013-12-24 17:33:45 -05:00
char * _Z_DATA;
char datafile[PATH_MAX];
char tmpfile[PATH_MAX];
2013-12-25 13:30:46 -05:00
char agedfile[PATH_MAX];
2013-12-24 17:33:45 -05:00
FILE *read;
FILE *write;
2013-12-25 13:30:46 -05:00
FILE *aged;
2013-12-24 17:33:45 -05:00
char line[LINE_MAX];
char pth[PATH_MAX];
float rank;
int timestamp;
2013-12-25 00:17:17 -05:00
struct stat s;
2013-12-24 17:33:45 -05:00
time_t epoch = time(NULL);
int found = 0;
2013-12-25 13:30:46 -05:00
float rank_sum = 0;
2013-12-24 17:33:45 -05:00
2014-03-26 11:38:00 -04:00
/* Don't track $HOME */
2013-12-24 23:55:19 -05:00
if (strcmp(argv[1], getenv("HOME")) == 0) return EXIT_SUCCESS;
2013-12-24 20:42:25 -05:00
2014-03-26 11:38:00 -04:00
/*
TODO
# don't track excluded dirs
local exclude
for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
[ "$*" = "$exclude" ] && return
done
*/
2013-12-25 00:18:13 -05:00
2013-12-24 17:33:45 -05:00
_Z_DATA = getenv("_Z_DATA");
if( _Z_DATA == NULL ) {
sprintf(datafile, "%s/.z", getenv("HOME"));
} else {
sprintf(datafile, "%s", _Z_DATA);
}
read = fopen(datafile, "r");
if (read == NULL) return EXIT_FAILURE;
srand(time(NULL));
2013-12-25 13:30:46 -05:00
2013-12-24 17:33:45 -05:00
sprintf(tmpfile, "%s.%d", datafile, rand());
write = fopen(tmpfile, "w");
if (write == NULL) return EXIT_FAILURE;
2013-12-25 13:30:46 -05:00
sprintf(agedfile, "%s.%d", datafile, rand());
aged = fopen(agedfile, "w");
if (aged == NULL) return EXIT_FAILURE;
2013-12-24 17:33:45 -05:00
while (fgets(line, sizeof line, read) != NULL) {
sscanf(line, "%[^|]|%f|%d\n", pth, &rank, &timestamp);
2013-12-25 00:17:17 -05:00
if (stat(pth, &s) == 0 && s.st_mode & S_IFDIR ) {
2013-12-25 13:30:46 -05:00
rank_sum += rank;
2013-12-25 00:17:17 -05:00
if (strcmp(pth, argv[1]) == 0) {
fprintf(write, "%s|%f|%d\n", pth, rank + 1, (int) epoch);
2013-12-25 13:30:46 -05:00
fprintf(aged, "%s|%f|%d\n", pth, rank * 1, (int) epoch);
2013-12-25 00:17:17 -05:00
found = 1;
2013-12-25 13:30:46 -05:00
} else if (rank >= 1) {
fprintf(write, "%s", line);
fprintf(aged, "%s|%f|%d\n", pth, rank * MULTIPLIER, timestamp);
}
2013-12-25 00:17:17 -05:00
}
2013-12-24 17:33:45 -05:00
}
2013-12-25 13:30:46 -05:00
if (!found) {
fprintf(write, "%s|1|%d\n", argv[1], (int) epoch);
fprintf(aged, "%s|1|%d\n", argv[1], (int) epoch);
}
2013-12-24 17:33:45 -05:00
fclose(read);
fclose(write);
2013-12-25 13:30:46 -05:00
fclose(aged);
2013-12-24 17:33:45 -05:00
2013-12-25 13:30:46 -05:00
if (rank_sum > RANK_SUM_MAX) {
if (rename(agedfile, datafile) != 0) return EXIT_FAILURE;
if (unlink(tmpfile) != 0) return EXIT_FAILURE;
} else {
if (rename(tmpfile, datafile) != 0) return EXIT_FAILURE;
if (unlink(agedfile) != 0) return EXIT_FAILURE;
}
2013-12-24 17:33:45 -05:00
2013-12-25 13:30:46 -05:00
return EXIT_SUCCESS;
2013-12-24 17:33:45 -05:00
}