char *file_uncompress(char *filename) { static char newfile[MAXFNAMELEN]; char command[BUFSIZ], *ptr, *suffix = NULL; /* Filename to be returned */ strcpy(newfile, filename); /* Uncompress depending on compression method */ if(is_gzipped(filename)) { if(!env_inpath("gunzip")) return(NULL); sprintf(command, "gunzip -f %s", filename); if(!system(command)) return(NULL); suffix = ".gz"; } else if(is_compressed(filename)) { if(!env_inpath("uncompress")) return(NULL); sprintf(command, "uncompress -f %s", filename); if(!system(command)) return(NULL); suffix = ".Z"; } /* Strip of suffix if it was there */ if(suffix != NULL) { if((ptr = strstr(newfile, suffix)) != NULL) *ptr = 0; } /* Return uncomprssed filename */ return(newfile); }
// Load a Lua "chunk" from a binary resource (for execution). Similar to // luaL_loadbuffer(), but this function knows how to handle (gzip-)decompression. int load_decompressed_buffer(lua_State *L, const char *data, size_t len, const char *name) { char chunkname[PATH_MAX]; // explicitly prefix chunk name with '=', so Lua doesn't tamper with it // (see e.g. http://lua.2524044.n2.nabble.com/Error-reporting-the-chunk-name-td4034634.html) snprintf(chunkname, sizeof(chunkname), "=%s", strip_pwd(name)); // (but strip PWD first) if (!is_gzipped(data)) { // this should be a plain(text) buffer, so we pass it to luaL_loadbuffer() directly return luaL_loadbuffer(L, data, len, chunkname); } // gzipped data, use decompressor before loading it size_t decompressed_size; char *decompressed = gzip_decompress(data, len, &decompressed_size); if (decompressed) { // decompression successful, continue with loading int result = luaL_loadbuffer(L, decompressed, decompressed_size, chunkname); free(decompressed); return result; } return luaL_error(L, "%s(%s): decompression of gzipped resource FAILED", __func__, name); }
/* * Scan the man section directory for pages and process each one, * then check for junk in the corresponding cat section. */ static void scan_section(char *mandir, char *section, char *cat_section) { struct dirent **entries; char **expected = NULL; int npages; int nexpected = 0; int i, e; enum Ziptype zipped; char *page_name; char page_path[MAXPATHLEN]; char cat_path[MAXPATHLEN]; char zip_path[MAXPATHLEN]; /* * scan the man section directory for pages */ npages = scandir(section, &entries, NULL, alphasort); if (npages < 0) { warn("%s/%s", mandir, section); exit_code = 1; return; } if (verbose || rm_junk) { /* * Maintain a list of all cat pages that should exist, * corresponding to existing man pages. */ expected = (char **) calloc(npages, sizeof(char *)); } for (i = 0; i < npages; free(entries[i++])) { page_name = entries[i]->d_name; snprintf(page_path, sizeof page_path, "%s/%s", section, page_name); if (!is_manpage_name(page_name)) { if (!(test_path(page_path, NULL) & TEST_DIR)) { junk(mandir, page_path, "invalid man page name"); } continue; } zipped = is_bzipped(page_name) ? BZIP : is_gzipped(page_name) ? GZIP : NONE; if (zipped != NONE) { snprintf(cat_path, sizeof cat_path, "%s/%s", cat_section, page_name); if (expected != NULL) expected[nexpected++] = strdup(page_name); process_page(mandir, page_path, cat_path, zipped); } else { /* * We've got an uncompressed man page, * check to see if there's a (preferred) * compressed one. */ snprintf(zip_path, sizeof zip_path, "%s%s", page_path, GZ_EXT); if (test_path(zip_path, NULL) != 0) { junk(mandir, page_path, "man page unused due to existing " GZ_EXT); } else { if (verbose) { fprintf(stderr, "warning, %s is uncompressed\n", page_path); } snprintf(cat_path, sizeof cat_path, "%s/%s", cat_section, page_name); if (expected != NULL) { asprintf(&expected[nexpected++], "%s", page_name); } process_page(mandir, page_path, cat_path, NONE); } } } free(entries); if (expected == NULL) return; /* * scan cat sections for junk */ npages = scandir(cat_section, &entries, NULL, alphasort); e = 0; for (i = 0; i < npages; free(entries[i++])) { const char *junk_reason; int cmp = 1; page_name = entries[i]->d_name; if (strcmp(page_name, ".") == 0 || strcmp(page_name, "..") == 0) continue; /* * Keep the index into the expected cat page list * ahead of the name we've found. */ while (e < nexpected && (cmp = strcmp(page_name, expected[e])) > 0) free(expected[e++]); if (cmp == 0) continue; /* we have an unexpected page */ snprintf(cat_path, sizeof cat_path, "%s/%s", cat_section, page_name); if (!is_manpage_name(page_name)) { if (test_path(cat_path, NULL) & TEST_DIR) continue; junk_reason = "invalid cat page name"; } else if (!is_gzipped(page_name) && e + 1 < nexpected && strncmp(page_name, expected[e + 1], strlen(page_name)) == 0 && strlen(expected[e + 1]) == strlen(page_name) + 3) { junk_reason = "cat page unused due to existing " GZ_EXT; } else junk_reason = "cat page without man page"; junk(mandir, cat_path, junk_reason); } free(entries); while (e < nexpected) free(expected[e++]); free(expected); }