File: damnit.c

package info (click to toggle)
gnarwl 3.6.dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 856 kB
  • ctags: 288
  • sloc: ansic: 1,163; makefile: 119; sh: 70; php: 44
file content (197 lines) | stat: -rw-r--r-- 4,401 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <gdbm.h>
#include <errno.h>
#include <time.h>
#include <string.h>

#include <conf.h>
#include "dbaccess.h"
#include "config.h"
#include "util.h"

// How to display data
char *format="%time -> %entry\n";

// Whether to add a timestamp to the file
int timestamp=1;

/**
 * Print usage information
 */
void printUsage() {
  printf("\nDAMNIT(v%s) - DAtabase MAnagement InTerface\n\n\
  Damnit is a tool for listing/editing gnarwl's database files.\n\
  Options:\n\n\
  \t-h\t\t\t print usage information\n\
  \t-n\t\t\t don't add a timestamp to the file\n\
  \t-d <file> [<value>]\t delete <value> from <file>\n\
  \t-a <file> [<value>]\t add <value> to <file>\n\
  \t-l <file>\t\t list database file\n\
  \t-f <string>\t\t select output format (use with -l)\n\n",VERSION);
  exit(EXIT_SUCCESS);
}

/**
 * Print the contents of a db file (formated)
 * @param key key in the database
 * @param val value in the database
 */
void printFormated(const char* key, const time_t val) {
  char *tmp=NULL;
  char *tmp2=NULL;
  
  cpyStr(&tmp,format);
  cpyStr(&tmp2,ctime(&val));
  if (tmp2[strlen(tmp2)-1]=='\n') tmp2[strlen(tmp2)-1]='\0';
  
  expandVars(&tmp,"%entry",(char*)key);
  expandVars(&tmp,"%time",tmp2);
  expandVars(&tmp,"%tstamp","%d");
  expandVars(&tmp,"\\n","\n");
  expandVars(&tmp,"\\t","\t");
  
  /*
  free(tmp2);
  tmp2=(char*)calloc(strlen(tmp)+2,sizeof(char));
  if(tmp2==NULL) oom();
  strcpy(tmp2,tmp);
  if (tmp2[strlen(tmp2)]!='\n') tmp2[strlen(tmp2)]='\n';
  */
  printf(tmp,(int)val);
  free(tmp);
  free(tmp2);
}

/**
 * List the contents of a db file
 */
void listFile(char *name) {
  GDBM_FILE dbf;
  datum key;
  datum val;
  time_t t;
  
  dbf=dbOpen(name,GDBM_READER);
  
  if (dbf==NULL) {
    printf("IO Error: %s\n",name);
    exit(EXIT_FAILURE);
  }
    
  key = gdbm_firstkey ( dbf );
  while (key.dptr!=NULL) {
    val = gdbm_fetch(dbf,key);
    if (val.dsize==sizeof(time_t)) {
      memcpy(&t,val.dptr,sizeof(t));
      printFormated(key.dptr,t);
    }
    free(val.dptr);
    val.dptr=key.dptr;
    key = gdbm_nextkey(dbf,key);
    free(val.dptr);
  }
  dbClose(dbf);
}

void addToFile(char *fname, char *entry) {
  GDBM_FILE dbf;
  datum key;
  datum val;
  time_t t;
  
  dbf=dbOpen(fname,GDBM_WRCREAT);
  
  if (dbf==NULL) {
    printf("IO error: %s\n",fname);
    exit(EXIT_FAILURE);
  }

  t=timestamp ? time(NULL) : 0;
  key.dptr=entry;
  key.dsize=strlen(entry)+1;
  val.dptr=(char*)malloc(sizeof(t));
  if (val.dptr==NULL) {
    printf("Out of memory");
    exit(EXIT_FAILURE);
  }
  memcpy(val.dptr,&t,sizeof(t));
  val.dsize=sizeof(t);
  if (gdbm_store(dbf,key,val,GDBM_REPLACE)!=0) {
    printf("IO error: %s\n",fname);
    dbClose(dbf);
    exit(EXIT_FAILURE);
  }
  dbClose(dbf);
}

void delFromFile(char* fname, char* entry) {
  GDBM_FILE dbf;
  datum key;
  
  dbf=dbOpen(fname,GDBM_WRITER);
  
  if (dbf==NULL) {
    printf("IO error: %s\n",fname);
    exit(EXIT_FAILURE);
  }

  key.dptr=entry;
  key.dsize=strlen(entry)+1;
  if (!gdbm_exists(dbf,key)) {
    printf("Key not in database: %s\n",entry);
    exit(EXIT_FAILURE);
  }
  
  if (gdbm_delete(dbf,key)!=0) {
    printf("IO error: %s",fname);
    dbClose(dbf);
    exit(EXIT_FAILURE);
  }
  dbClose(dbf);
}

int main(int argc,char **argv) {
  int ch;
  char* fname=NULL;
  char* val=NULL;
  char buf[MAXLINE];
  
  setDefaults();
  
  if (argc==1) printUsage();
  
  while ((ch = getopt(argc, argv, "hnf:l:a:d:")) != EOF) {
    switch((char)ch) {
      case 'h': printUsage();
      case 'n': {timestamp=0;};
      case 'f': {format=optarg;break;}
      case 'l': {
        listFile(optarg);
        exit(EXIT_SUCCESS);
      }
      case 'a': {
        fname=optarg;
        val=argv[optind];
        if (val!=NULL) addToFile(fname,val); 
        else while (fgets(buf,(int)sizeof(buf),stdin) && (*buf != '\n')) {
          if (buf[strlen(buf)-1]=='\n') buf[strlen(buf)-1]='\0';
          addToFile(fname,buf);
        }
        exit(EXIT_SUCCESS);
      }
      case 'd': {
        fname=optarg;
        val=argv[optind];
        if (val!=NULL) delFromFile(fname,val);
        else while (fgets(buf,(int)sizeof(buf),stdin) && (*buf != '\n')) {
          if (buf[strlen(buf)-1]=='\n') buf[strlen(buf)-1]='\0';
          delFromFile(fname,buf);
        }
        exit(EXIT_SUCCESS);
      }
    }
  }
  return EXIT_SUCCESS;
}