9 static void usage(FILE *out)
11 fprintf(out, "usage: nextep [-h] [-f FILE] [-s SET-VALUE] [DIR]\n");
14 int main(int argc, char **argv)
17 char buf[1024], dbuf[1024], *p;
18 char *file, *value, *dir;
19 char base[1024], fpath[1024];
24 while((c = getopt(argc, argv, "hf:s:")) >= 0) {
41 if(chdir(argv[optind])) {
42 fprintf(stderr, "nextep: %s: %s\n", argv[optind], strerror(errno));
47 if(!getcwd(dbuf, sizeof(dbuf))) {
48 fprintf(stderr, "nextep: getcwd: %s\n", strerror(errno));
52 if((p = getenv("HOME")) == NULL) {
53 fprintf(stderr, "nextep: $HOME is not set\n");
56 snprintf(buf, sizeof(buf), "%s/.nextep", p);
57 snprintf(base, sizeof(base), "%s/%s", buf, dir);
58 if(access(base, X_OK)) {
59 if((mkdir(buf, 0777) && (errno != EEXIST)) || mkdir(base, 0777)) {
60 fprintf(stderr, "nextep: %s: %s\n", base, strerror(errno));
64 snprintf(fpath, sizeof(fpath), "%s/%s", base, file);
66 if((fp = fopen(fpath, "r")) == NULL) {
68 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
72 while(fgets(buf, sizeof(buf), fp) != NULL)
77 if(unlink(fpath) && (errno != ENOENT)) {
78 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
82 if((fp = fopen(fpath, "w")) == NULL) {
83 fprintf(stderr, "nextep: %s: %s\n", fpath, strerror(errno));
86 fprintf(fp, "%s\n", value);
94 * compile-command: "gcc -Wall -g -o nextep nextep.c"