cur.c (1444B)
1 /* 2 * Copy me if you can. 3 * by 20h 4 */ 5 6 #include <unistd.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <stdarg.h> 10 11 #include "ind.h" 12 #include "arg.h" 13 #include "cfg.h" 14 #include "llist.h" 15 #include "cur.h" 16 #include "mark.h" 17 #include "inc.h" 18 19 void 20 curusage(char *argv0) 21 { 22 die("usage: %s [-h] [-c cfg] [msgid]\n", argv0); 23 } 24 25 int 26 curmain(int argc, char *argv[]) 27 { 28 config_t *cfg; 29 mark_t *marks; 30 llistelem_t *curseq; 31 char *msgss, *selected, *cfgn, *argv0; 32 int msgs, setn; 33 34 cfgn = NULL; 35 36 ARGBEGIN(argv0) { 37 case 'c': 38 cfgn = EARGF(curusage(argv0)); 39 break; 40 default: 41 curusage(argv0); 42 } ARGEND; 43 44 cfg = config_init(cfgn); 45 marks = mark_cfg(cfg); 46 config_free(cfg); 47 48 if (marks == NULL) { 49 fprintf(stderr, "Can not proceed without a selected " 50 "mailbox.\n"); 51 return 1; 52 } 53 54 if (argc > 0) { 55 setn = atoi(argv[0]); 56 if (setn < 1) 57 setn = 1; 58 59 selected = config_checkgetstr(cfg, "selected"); 60 msgss = inc_getstr(cfgn, selected, "messages"); 61 if (msgss != NULL) { 62 msgs = atoi(msgss); 63 if (msgs > 0 && setn > msgs) 64 setn = msgs; 65 free(msgss); 66 } 67 free(selected); 68 69 msgss = smprintf("%d", setn); 70 mark_set(marks, "cur", msgss); 71 free(msgss); 72 llistelem_printdata(mark_get(marks, "cur")); 73 mark_stop(marks); 74 } else { 75 curseq = mark_get(marks, "cur"); 76 if (curseq == NULL) { 77 fprintf(stderr, "No current message id is set at " 78 "all.\n"); 79 return 1; 80 } 81 llistelem_printdata(curseq); 82 } 83 84 return 0; 85 } 86