Last active
July 10, 2019 22:27
-
-
Save chris124567/55db6440edaab02186788deede207413 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <getopt.h> | |
#include <string.h> | |
#define TODO_LOC "/home/christopher/.local/share/todo" | |
#define TODO_LOC2 "/home/christopher/.local/share/todo.tmp" | |
#define ERR_CLOSEFILE "Error closing file" | |
#define ERR_OPENFILE "Error opening file" | |
static void todo_append(FILE *todo, char *str); | |
static void todo_edit(void); | |
static void todo_print(FILE *todo); | |
static void todo_del(FILE *todo, int line); | |
static void todo_clear(void); | |
static void todo_line(FILE *todo, int line); | |
static void __attribute__((noreturn)) die(const char * msg); | |
static void printhelp(void); | |
static void __attribute__((noreturn)) die(const char * msg) { | |
perror(msg); | |
exit(EXIT_FAILURE); /* splint warnings about possibly null values are false positives, for some reason it doesn't consider this line when seeing the die() function */ | |
} | |
static void printhelp(void) { | |
printf("todo: [-a string|-p|-d int|-l int|-c|-h|-e]\n"); | |
} | |
static void todo_append(FILE *todo, char *str) { | |
if(fgetc(todo) == EOF) { | |
fprintf(todo, "%s\n", str); | |
} | |
/* don't create new line at top of file if it's a new file*/ | |
else { | |
fprintf(todo, "%s\n", str); | |
} | |
} | |
static void todo_edit(void) { | |
char command[128]; | |
char *ed; | |
memset(command, '\0', sizeof(command)); | |
ed = getenv("EDITOR"); | |
if(ed == NULL) { | |
die("$EDITOR not set"); | |
} | |
sprintf(command, "%s %s", ed, TODO_LOC); | |
if(system(command) != EXIT_SUCCESS) { | |
die("Failed to execute $EDITOR"); | |
} | |
} | |
static void todo_print(FILE *todo) { | |
int prev, ret, i; | |
i = prev = 2; /* prevs value doesn't really matter just need it not to be random data */ | |
ret = fgetc(todo); | |
if(ret != EOF) printf("1. "); /* aaaaaaa */ | |
while(ret != EOF) { | |
if(prev == '\n') { | |
printf("%d. ", i); | |
++i; | |
} | |
(void)putchar(ret); /* checking the return value of this is pointless */ | |
prev = ret; | |
ret = fgetc(todo); | |
} | |
} | |
static void todo_del(FILE *todo, int line) { | |
int temp, c; | |
FILE *todo2; | |
temp = 1; | |
todo2 = fopen(TODO_LOC2, "w+"); | |
if(todo2 == NULL) { | |
die(ERR_OPENFILE); | |
} | |
while((c = fgetc(todo)) != EOF) { | |
if(temp != line) { /* if its the line that's supposed to be deleted, don't include it */ | |
(void)fputc(c, todo2); /* checking return value of this is pointless */ | |
} | |
if(c == '\n') { | |
temp++; | |
} | |
} | |
if(fclose(todo2) == EOF) { | |
die(ERR_CLOSEFILE); | |
} | |
if(rename(TODO_LOC2, TODO_LOC) == -1) { | |
die("Failed to rename temp file to todo file"); | |
} | |
} | |
static void todo_line(FILE *todo, int line) { | |
int ln, c; | |
ln = 1; | |
while((c = fgetc(todo)) != EOF) { | |
if(ln == line) { /* if its the line that's supposed to be printed, include it */ | |
(void)putchar(c); /* checking return value of this is pointless */ | |
} | |
if(c == '\n') { | |
ln++; | |
} | |
if(ln > line) { | |
return; /* don't waste time checking any lines beyond the specified one */ | |
} | |
} | |
} | |
static void todo_clear(void) { | |
if(remove(TODO_LOC) == -1) { | |
die("Failed to remove todo file"); | |
} | |
} | |
int main(int argc, char *argv[]) { | |
int c; | |
FILE *fp; | |
fp = fopen(TODO_LOC, "a+"); | |
if(fp == NULL) { | |
die(ERR_OPENFILE); | |
} | |
while ((c = getopt (argc, argv, "a:cd:ehl:p")) != -1) { | |
switch(c) { /* callers need to close */ | |
case 'a': | |
todo_append(fp, optarg); | |
break; | |
case 'c': | |
todo_clear(); | |
break; | |
case 'd': | |
optind--; | |
todo_del(fp, atoi(optarg)); | |
break; | |
case 'e': | |
todo_edit(); | |
break; | |
case 'l': | |
todo_line(fp, atoi(optarg)); | |
break; | |
case 'p': | |
todo_print(fp); | |
break; | |
case 'h': | |
printhelp(); | |
break; | |
default: | |
printhelp(); | |
break; | |
} | |
} | |
if(fclose(fp) == EOF) { | |
die(ERR_CLOSEFILE); | |
} | |
exit(EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment