/* Return a list of lyrics lines loaded from a file, or NULL on error. */ lists_t_strs *lyrics_load_file (const char *filename) { FILE *lyrics_file = NULL; const char *mime; char *line; lists_t_strs *result; assert (filename); lyrics_message = "[No lyrics file!]"; if (!file_exists (filename)) return NULL; mime = file_mime_type (filename); if (mime && strncmp (mime, "text/plain", 10)) return NULL; lyrics_file = fopen (filename, "r"); if (lyrics_file == NULL) { lyrics_message = "[Lyrics file cannot be read!]"; logit ("Error reading '%s': %s", filename, strerror (errno)); return NULL; } result = lists_strs_new (0); while ((line = read_line (lyrics_file)) != NULL) lists_strs_push (result, line); fclose (lyrics_file); lyrics_message = NULL; return result; }
/* Find a preference entry matching the given filename extension and/or * MIME media type, or NULL. */ static decoder_t_preference *lookup_preference (const char *extn, const char *file, char **mime) { char *type, *subtype; decoder_t_preference *result; assert ((extn && extn[0]) || (file && file[0]) || (mime && *mime && *mime[0])); type = NULL; subtype = NULL; for (result = preferences; result; result = result->next) { if (!result->subtype) { if (extn && !strcasecmp (result->type, extn)) break; } else { if (!type) { if (mime && *mime == NULL && file && file[0]) { if (options_get_bool ("UseMimeMagic")) *mime = file_mime_type (file); } if (mime && *mime && strchr (*mime, '/')) type = xstrdup (*mime); if (type) { subtype = strchr (type, '/'); *subtype++ = 0x00; subtype = clean_mime_subtype (subtype); } } if (type) { if (!strcasecmp (result->type, type) && !strcasecmp (result->subtype, subtype)) break; } } } free (type); return result; }
/* @name starts from "/", such as "/index.html" */ void send_file(int32 sockfd, const int8 *name) { int32 fd; int32 r, len; int64 filesize; struct stat st; int8 path[1024], contentlen[128], buf[BUFFSIZ]; snprintf_s_ss(path, sizeof(path), "%s%s", HTTPD_WORK_DIR, (char *)name); if (stat(path, &st) < 0 || (fd = open(path, O_RDONLY)) < 0) { send_error(sockfd, 404, "Not Found", NULL, "File not Found."); close(sockfd); HTTPD_ERR("Not found file %s\n", path); return; } filesize = st.st_size; snprintf_s_ll(contentlen, sizeof(contentlen), "Content-Length: %lld", filesize); len = add_headers(buf, 200, "OK", contentlen, file_mime_type(name)); http_write(sockfd, buf, len); while (filesize > 0) { r = read(fd, buf, sizeof(buf)); if (r < 0 && (errno == EINTR || errno == EAGAIN)) continue; if (r <= 0 || http_write(sockfd, buf, r) != r) break; filesize -= r; } close(fd); }