9 static int ensuredir(char *base, char *dir)
11 if(access(dir, X_OK)) {
12 if((mkdir(base, 0777) && (errno != EEXIST)) || mkdir(dir, 0777))
18 static void usage(FILE *out)
20 fprintf(out, "usage: nextep [-h] [-f FILE] [-s SET-VALUE] [DIR]\n");
23 int main(int argc, char **argv)
26 char buf[1024], dbuf[1024], *p;
27 char *file, *value, *dir;
28 char base[1024], fpath[1024];
33 while((c = getopt(argc, argv, "hf:s:")) >= 0) {
50 if(chdir(argv[optind])) {
51 fprintf(stderr, "nextep: %s: %s\n", argv[optind], strerror(errno));
56 if(!getcwd(dbuf, sizeof(dbuf))) {
57 fprintf(stderr, "nextep: getcwd: %s\n", strerror(errno));
61 if((p = getenv("HOME")) == NULL) {
62 fprintf(stderr, "nextep: $HOME is not set\n");
65 snprintf(buf, sizeof(buf), "%s/.nextep", p);
66 snprintf(base, sizeof(base), "%s/%s", buf, dir);
67 snprintf(fpath, sizeof(fpath), "%s/%s", base, file);
69 if((fp = fopen(fpath, "r")) == NULL) {
71 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
75 while(fgets(buf, sizeof(buf), fp) != NULL)
80 if(unlink(fpath) && (errno != ENOENT)) {
81 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
85 if(ensuredir(buf, base)) {
86 fprintf(stderr, "nextep: %s: %s\n", base, strerror(errno));
89 if((fp = fopen(fpath, "w")) == NULL) {
90 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
93 fprintf(fp, "%s\n", value);
101 * compile-command: "gcc -Wall -g -o nextep nextep.c"