10 static int ensuredir(char *base, char *dir)
12 if(access(dir, X_OK)) {
13 if((mkdir(base, 0777) && (errno != EEXIST)) || mkdir(dir, 0777))
19 static void checkempty(char *dir)
25 if((dh = opendir(dir)) == NULL)
28 while((de = readdir(dh)) != NULL) {
29 if(de->d_name[0] != '.') {
39 static void usage(FILE *out)
41 fprintf(out, "usage: nextep [-h] [-f FILE] [-s SET-VALUE] [DIR]\n");
44 int main(int argc, char **argv)
47 char buf[1024], dbuf[1024], *p;
48 char *file, *value, *dir;
49 char base[1024], fpath[1024];
54 while((c = getopt(argc, argv, "hf:s:")) >= 0) {
71 if(chdir(argv[optind])) {
72 fprintf(stderr, "nextep: %s: %s\n", argv[optind], strerror(errno));
77 if(!getcwd(dbuf, sizeof(dbuf))) {
78 fprintf(stderr, "nextep: getcwd: %s\n", strerror(errno));
82 if((p = getenv("HOME")) == NULL) {
83 fprintf(stderr, "nextep: $HOME is not set\n");
86 snprintf(buf, sizeof(buf), "%s/.nextep", p);
87 snprintf(base, sizeof(base), "%s/%s", buf, dir);
88 snprintf(fpath, sizeof(fpath), "%s/%s", base, file);
90 if((fp = fopen(fpath, "r")) == NULL) {
92 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
96 while(fgets(buf, sizeof(buf), fp) != NULL)
101 if(unlink(fpath) && (errno != ENOENT)) {
102 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
107 if(ensuredir(buf, base)) {
108 fprintf(stderr, "nextep: %s: %s\n", base, strerror(errno));
111 if((fp = fopen(fpath, "w")) == NULL) {
112 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
115 fprintf(fp, "%s\n", value);
123 * compile-command: "gcc -Wall -g -o nextep nextep.c"