void uri_free(Uri *uri) { if (uri->protocol) free_null(uri->protocol); if (uri->host) free_null(uri->host); if (uri->path) free_null(uri->path); free_null(uri); }
void http_request_free(HttpRequest *request) { if (request->method) free_null(request->method); if (request->protocol) free_null(request->protocol); if (request->uri) free_null(request->uri); if (request->host) free_null(request->host); free_null(request); }
/* Adds information for a newly added file. Actually the name * is misleading, as the file is added in filedb_open() and we * only add information in here. */ static void filedb_add(FILE * fdb, char *filename, char *nick) { filedb_entry *fdbe = NULL; filedb_readtop(fdb, NULL); /* When the filedb was opened, a record was already created. */ fdbe = filedb_matchfile(fdb, ftell(fdb), filename); if (!fdbe) return; free_null(fdbe->uploader); realloc_strcpy(fdbe->uploader, nick); fdbe->uploaded = now; filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL); free_fdbe(&fdbe); }
END_TEST START_TEST(test_three) { pbs_attribute attr; attribute_def def; pbs_attribute attrA; pbs_attribute attrB; memset(&attr,(unsigned)~0,sizeof(pbs_attribute)); memset(&def,0,sizeof(attribute_def)); memset(&attrA,0,sizeof(pbs_attribute)); memset(&attrB,0,sizeof(pbs_attribute)); def.at_type = ATR_TYPE_LIST; clear_attr(&attr,&def); fail_unless(attr.at_type == ATR_TYPE_LIST); attribute_def defa[3]; memset(defa,0,sizeof(defa)); defa[0].at_name = "Hello There."; defa[1].at_name = "Howdy Pard."; defa[2].at_name = "hello"; fail_unless(find_attr(defa,"HeLlo",3) == 2); fail_unless(find_attr(defa,"Hello there. ",3) == -1); attrA.at_val.at_long = 5; attrB.at_val.at_long = 6; fail_unless(attr_ifelse_long(&attrA,&attrB,3) == 3); attrB.at_flags = ATR_VFLAG_SET; fail_unless(attr_ifelse_long(&attrA,&attrB,3) == 6); attrA.at_flags = ATR_VFLAG_SET; fail_unless(attr_ifelse_long(&attrA,&attrB,3) == 5); free_null(&attrA); fail_unless(attrA.at_flags == 0); free_noop(&attrA); comp_null(&attrA,&attrB); }
int main(int argc, char **argv) { char memory[4096]; struct smart_alloc *sa = smart_alloc_init(memory, sizeof(memory)); basic_test(sa); two_alloc(sa); oom(sa); basic_free(sa); free_null(sa); checkered_alloc(sa); /* try free'ing a pointer from one sa in another? */ smart_alloc_destroy(sa); return 0; }
//devuelvo el archivo data.bin mappeado void* data_get(char* filename) { if (!file_exists(filename)) { FILE* file = NULL; file = fopen(filename, "w"); if (file == NULL) { handle_error("fopen"); } printf("creado\n"); //lo creo con el tamaño maximo void* dump = NULL; dump = malloc(DATA_SIZE); memset(dump, 0, DATA_SIZE); fwrite(dump, DATA_SIZE, 1, file); free_null(dump); fclose(file); } //el archivo ya esta creado con el size maximo return file_get_mapped(filename); /* void* mapped=NULL; struct stat st; int fd=0; fd = open(filename, O_RDWR); if(fd==-1){ handle_error("open"); } stat(filename, &st); //printf("%ld\n", st.st_size); int size = st.st_size; mapped = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0); close(fd); if(mapped==MAP_FAILED){ handle_error("mmap"); } return mapped; */ }
static void filedb_setowner(char *dir, char *fn, char *owner) { filedb_entry *fdbe = NULL; FILE *fdb = NULL; fdb = filedb_open(dir, 0); if (!fdb) return; filedb_readtop(fdb, NULL); fdbe = filedb_matchfile(fdb, ftell(fdb), fn); if (fdbe) { free_null(fdbe->uploader); realloc_strcpy(fdbe->uploader, owner); filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL); free_fdbe(&fdbe); } filedb_close(fdb); }
/* Frees a filedb entry and all it's elements. */ static void free_fdbe(filedb_entry **fdbe) { if (!fdbe || !*fdbe) return; if ((*fdbe)->filename) free_null((*fdbe)->filename); if ((*fdbe)->desc) free_null((*fdbe)->desc); if ((*fdbe)->chan) free_null((*fdbe)->chan); if ((*fdbe)->uploader) free_null((*fdbe)->uploader); if ((*fdbe)->flags_req) free_null((*fdbe)->flags_req); free_null(*fdbe); }
~QP_rule_text() { for(int i=0; i<16; i++) { free_null(pta[i]); } free(pta); }
~QP_rule_text_hitsonly() { for(int i=0; i<2; i++) { free_null(pta[i]); } free(pta); }
/* Outputs a sorted list of files/directories matching the mask, * to idx. */ static void filedb_ls(FILE *fdb, int idx, char *mask, int showall) { int ok = 0, cnt = 0, is = 0; char s1[81], *p = NULL; struct flag_record user = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0}; filedb_entry *fdbe = NULL; filelist_t *flist = NULL; flist = filelist_new(); filedb_readtop(fdb, NULL); fdbe = filedb_getfile(fdb, ftell(fdb), GET_FULL); while (fdbe) { ok = 1; if (fdbe->stat & FILE_UNUSED) ok = 0; if (ok && (fdbe->stat & FILE_DIR) && fdbe->flags_req) { /* Check permissions */ struct flag_record req = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0}; break_down_flags(fdbe->flags_req, &req, NULL); get_user_flagrec(dcc[idx].user, &user, dcc[idx].u.file->chat->con_chan); if (!flagrec_ok(&req, &user)) { ok = 0; } } if (ok) is = 1; if (ok && !wild_match_file(mask, fdbe->filename)) ok = 0; if (ok && (fdbe->stat & FILE_HIDDEN) && !(showall)) ok = 0; if (ok) { /* Display it! */ if (cnt == 0) { dprintf(idx, _("Filename Size Sent by/Date # Gets\n")); dprintf(idx, _("------------------------------ ---- ------------------- ------\n")); } filelist_add(flist, fdbe->filename); if (fdbe->stat & FILE_DIR) { char *s2 = NULL, *s3 = NULL; /* Too long? */ if (strlen(fdbe->filename) > 45) { /* Display the filename on its own line. */ s2 = malloc(strlen(fdbe->filename) + 3); sprintf(s2, "%s/\n", fdbe->filename); filelist_addout(flist, s2); free_null(s2); } else { s2 = malloc(strlen(fdbe->filename) + 2); sprintf(s2, "%s/", fdbe->filename); } /* Note: You have to keep the sprintf and the malloc statements * in sync, i.e. always check that you allocate enough * memory. */ if ((fdbe->flags_req) && (user.global &(USER_MASTER | USER_JANITOR))) { s3 = malloc(42 + strlen(s2 ? s2 : "") + 6 + strlen(_("requires")) + strlen(fdbe->flags_req) + 1 + strlen(fdbe->chan ? fdbe->chan : "") + 1); sprintf(s3, "%-30s <DIR> (%s %s%s%s)\n", s2, _("requires"), fdbe->flags_req, fdbe->chan ? " " : "", fdbe->chan ? fdbe->chan : ""); } else { s3 = malloc(38 + strlen(s2 ? s2 : "")); sprintf(s3, "%-30s <DIR>\n", s2 ? s2 : ""); } if (s2) free_null(s2); filelist_addout(flist, s3); free_null(s3); } else { char s2[41], t[50], *s3 = NULL, *s4; s2[0] = 0; if (showall) { if (fdbe->stat & FILE_HIDDEN) strcat(s2, " (hid)"); } strftime(t, 10, "%d%b%Y", localtime(&fdbe->uploaded)); if (fdbe->size < 1024) sprintf(s1, "%5d", fdbe->size); else sprintf(s1, "%4dk", (int) (fdbe->size / 1024)); /* Too long? */ if (strlen(fdbe->filename) > 30) { s3 = malloc(strlen(fdbe->filename) + 2); sprintf(s3, "%s\n", fdbe->filename); filelist_addout(flist, s3); free_null(s3); /* Causes filename to be displayed on its own line */ } else realloc_strcpy(s3, fdbe->filename); s4 = malloc(69 + strlen(s3 ? s3 : "") + strlen(s1) + strlen(fdbe->uploader) + strlen(t) + strlen(s2)); sprintf(s4, "%-30s %s %-9s (%s) %6d%s\n", s3 ? s3 : "", s1, fdbe->uploader, t, fdbe->gots, s2); if (s3) free_null(s3); filelist_addout(flist, s4); free_null(s4); } if (fdbe->desc) { p = strchr(fdbe->desc, '\n'); while (p != NULL) { *p = 0; if ((fdbe->desc)[0]) { char *sd; sd = malloc(strlen(fdbe->desc) + 5); sprintf(sd, " %s\n", fdbe->desc); filelist_addout(flist, sd); free_null(sd); } strcpy(fdbe->desc, p + 1); p = strchr(fdbe->desc, '\n'); } if ((fdbe->desc)[0]) { char *sd; sd = malloc(strlen(fdbe->desc) + 5); sprintf(sd, " %s\n", fdbe->desc); filelist_addout(flist, sd); free_null(sd); } } cnt++; } free_fdbe(&fdbe); fdbe = filedb_getfile(fdb, ftell(fdb), GET_FULL); } if (is == 0) dprintf(idx, _("No files in this directory.\n")); else if (cnt == 0) dprintf(idx, _("No matching files.\n")); else { filelist_sort(flist); filelist_idxshow(flist, idx); dprintf(idx, P_("--- %d file.\n", "--- %d files.\n", cnt), cnt); } filelist_free(flist); }
/* Opens the filedb responsible to the specified directory. */ static FILE *filedb_open(char *path, int sort) { char *s, *npath; FILE *fdb; filedb_top fdbt; struct stat st; if (count >= 2) putlog(LOG_MISC, "*", "(@) warning: %d open filedb's", count); npath = malloc(strlen(dccdir) + strlen(path) + 1); simple_sprintf(npath, "%s%s", dccdir, path); /* Use alternate filename if requested */ if (filedb_path[0]) { char *s2; s2 = make_point_path(path); s = malloc(strlen(filedb_path) + strlen(s2) + 8); simple_sprintf(s, "%sfiledb.%s", filedb_path, s2); free_null(s2); } else { s = malloc(strlen(npath) + 10); simple_sprintf(s, "%s/.filedb", npath); } fdb = fopen(s, "r+b"); if (!fdb) { if (convert_old_files(npath, s)) { fdb = fopen(s, "r+b"); if (fdb == NULL) { putlog(LOG_MISC, "*", _("(!) Broken convert to filedb in %s"), npath); free_null(s); free_null(npath); return NULL; } lockfile(fdb); filedb_update(npath, fdb, sort); count++; free_null(s); free_null(npath); return fdb; } else { filedb_top fdbt; /* Create new database and fix it up */ fdb = fopen(s, "w+b"); if (!fdb) { free_null(s); free_null(npath); return NULL; } lockfile(fdb); fdbt.version = FILEDB_NEWEST_VER; fdbt.timestamp = now; filedb_writetop(fdb, &fdbt); filedb_update(npath, fdb, sort); count++; free_null(s); free_null(npath); return fdb; } } lockfile(fdb); /* Lock it from other bots */ filedb_readtop(fdb, &fdbt); if (fdbt.version < FILEDB_NEWEST_VER) { if (!convert_old_db(&fdb, s)) { /* Conversion failed. Unlock file again and error out. * (convert_old_db() could have modified fdb, so check * for fdb != NULL.) */ if (fdb) unlockfile(fdb); free_null(npath); free_null(s); return NULL; } filedb_update(npath, fdb, sort); } stat(npath, &st); /* Update filedb if: * + it's been 6 hours since it was last updated * + the directory has been visibly modified since then * (6 hours may be a bit often) */ if (sort || ((now - fdbt.timestamp) > (6 * 3600)) || (fdbt.timestamp < st.st_mtime) || (fdbt.timestamp < st.st_ctime)) /* File database isn't up-to-date! */ filedb_update(npath, fdb, sort & 1); else if ((now - fdbt.timestamp) > 300) filedb_mergeempty(fdb); count++; free_null(npath); free_null(s); return fdb; }
/* Updates the specified filedb in several ways: * * 1. Adds all new files from the directory to the db. * 2. Removes all stale entries from the db. * 3. Optimises the db. */ static void filedb_update(char *path, FILE *fdb, int sort) { struct dirent *dd = NULL; struct stat st; filedb_entry *fdbe = NULL; DIR *dir = NULL; long where = 0; char *name = NULL, *s = NULL; /* * FIRST: make sure every real file is in the database */ dir = opendir(path); if (dir == NULL) { putlog(LOG_MISC, "*", _("filedb-update: cant open directory!")); return; } dd = readdir(dir); while (dd != NULL) { realloc_strcpy(name, dd->d_name); if (name[0] != '.') { s = malloc(strlen(path) + strlen(name) + 2); sprintf(s, "%s/%s", path, name); stat(s, &st); free_null(s); filedb_readtop(fdb, NULL); fdbe = filedb_matchfile(fdb, ftell(fdb), name); if (!fdbe) { /* new file! */ fdbe = malloc_fdbe(); realloc_strcpy(fdbe->filename, name); realloc_strcpy(fdbe->uploader, myname); fdbe->uploaded = now; fdbe->size = st.st_size; if (S_ISDIR(st.st_mode)) fdbe->stat |= FILE_DIR; filedb_addfile(fdb, fdbe); } else if (fdbe->size != st.st_size) { /* update size if needed */ fdbe->size = st.st_size; filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_HEADER); } free_fdbe(&fdbe); } dd = readdir(dir); } if (name) free_null(name); closedir(dir); /* * SECOND: make sure every db file is real */ filedb_readtop(fdb, NULL); fdbe = filedb_getfile(fdb, ftell(fdb), GET_FILENAME); while (fdbe) { where = ftell(fdb); if (!(fdbe->stat & FILE_UNUSED) && !(fdbe->stat & FILE_ISLINK) && fdbe->filename) { s = malloc(strlen(path) + 1 + strlen(fdbe->filename) + 1); sprintf(s, "%s/%s", path, fdbe->filename); if (stat(s, &st) != 0) /* gone file */ filedb_delfile(fdb, fdbe->pos); free_null(s); } free_fdbe(&fdbe); fdbe = filedb_getfile(fdb, where, GET_FILENAME); } /* * THIRD: optimise database * * Instead of sorting, we only clean up the db, because sorting is now * done on-the-fly when we display the file list. */ if (sort) filedb_cleanup(fdb); /* Cleanup DB */ filedb_timestamp(fdb); /* Write new timestamp */ }