|
| 1 | +#include<fcntl.h> |
| 2 | +#include<stdio.h> |
| 3 | +#include<stdlib.h> |
| 4 | +#include<string.h> |
| 5 | +#include<gdbm-ndbm.h> |
| 6 | + |
| 7 | +int main(void){ |
| 8 | + DBM *counterdb; |
| 9 | + datum key, value; |
| 10 | + char *query_string; |
| 11 | + int value_body; |
| 12 | + fputs("Content-Type: text/plain; charset=utf-8\n\n", stdout); |
| 13 | + query_string = getenv("QUERY_STRING"); |
| 14 | + if(!query_string) query_string = ""; |
| 15 | + counterdb = dbm_open("counterdb", O_RDWR | O_CREAT, |
| 16 | + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 17 | + if(!counterdb){ |
| 18 | + fputs("database error.\n", stdout); |
| 19 | + return 0; |
| 20 | + } |
| 21 | + if(query_string[0] == '\0'){ |
| 22 | + for(key = dbm_firstkey(counterdb); key.dptr != NULL; |
| 23 | + key = dbm_nextkey(counterdb)){ |
| 24 | + value = dbm_fetch(counterdb, key); |
| 25 | + fwrite(key.dptr, 1, key.dsize, stdout); |
| 26 | + fputs(" = ", stdout); |
| 27 | + printf("%d\n", *(int *)value.dptr); |
| 28 | + } |
| 29 | + } |
| 30 | + else{ |
| 31 | + key.dptr = query_string; |
| 32 | + key.dsize = strlen(query_string); |
| 33 | + value = dbm_fetch(counterdb, key); |
| 34 | + if(value.dptr) |
| 35 | + (*(int *)value.dptr)++; |
| 36 | + else{ |
| 37 | + value_body = 1; |
| 38 | + value.dptr = (char *)&value_body; |
| 39 | + value.dsize = sizeof(int); |
| 40 | + } |
| 41 | + dbm_store(counterdb, key, value, DBM_REPLACE); |
| 42 | + printf("%d\n", *(int *)value.dptr); |
| 43 | + } |
| 44 | + dbm_close(counterdb); |
| 45 | + return 0; |
| 46 | +} |
0 commit comments