- int c; /* current character */
- int n; /* number of bytes in buffer */
- char *p; /* buffer pointer */
-
- /*
- * Get a line from the input file, growing the field buffer as needed. We do this
- * so that we can fit an entire line in the buffer making it easy to do a string
- * comparison on both the field name and the field body which might be a long path
- * name.
- */
-
- for (n = 0, p = field; (c = getc(draft_file)) != EOF; *p++ = c) {
- if (c == '\n' && (c = getc(draft_file)) != ' ' && c != '\t') {
- (void)ungetc(c, draft_file);
- c = '\n';
- break;
- }
-
- if (++n >= field_size - 1) {
- if ((field = (char *)realloc((void *)field, field_size += 256)) == (char *)0)
- adios(NULL, "can't grow field buffer.");
-
- p = field + n - 1;
- }
- }
-
- /*
- * NUL-terminate the field..
- */
-
- *p = '\0';
-
- return (c);
-}
-
-static void
-make_mime_composition_file_entry(char *file_name)
-{
- int binary; /* binary character found flag */
- int c; /* current character */
- char cmd[MAXPATHLEN + 6]; /* file command buffer */
- char *content_type; /* mime content type */
- FILE *fp; /* content and pipe file pointer */
- struct node *np; /* context scan node pointer */
- char *p; /* miscellaneous string pointer */
- struct stat st; /* file status buffer */
-
- content_type = (char *)0;
-
- /*
- * Check the file name for a suffix. Scan the context for that suffix on a
- * mhshow-suffix- entry. We use these entries to be compatible with mhnshow,
- * and there's no reason to make the user specify each suffix twice. Context
- * entries of the form "mhshow-suffix-contenttype" in the name have the suffix
- * in the field, including the dot.
- */
-
- if ((p = strrchr(file_name, '.')) != (char *)0) {
- for (np = m_defs; np; np = np->n_next) {
- if (strncasecmp(np->n_name, "mhshow-suffix-", 14) == 0 && strcasecmp(p, np->n_field) == 0) {
- content_type = np->n_name + 14;
- break;
- }
- }
- }
-
- /*
- * No content type was found, either because there was no matching entry in the
- * context or because the file name has no suffix. Open the file and check for
- * non-ASCII characters. Choose the content type based on this check.
- */
-
- if (content_type == (char *)0) {
- if ((fp = fopen(file_name, "r")) == (FILE *)0) {
- clean_up_temporary_files();
- adios((char *)0, "unable to access file \"%s\"", file_name);
- }
-
- binary = 0;
-
- while ((c = getc(fp)) != EOF) {
- if (c > 127 || c < 0) {
- binary = 1;
- break;
- }
- }
-
- (void)fclose(fp);
-
- content_type = binary ? "application/octet-stream" : "text/plain";
- }
-
- /*
- * Make sure that the attachment file exists and is readable. Append a mhbuild
- * directive to the draft file. This starts with the content type. Append a
- * file name attribute and a private x-unix-mode attribute. Also append a
- * description obtained (if possible) by running the "file" command on the file.
- */
-
- if (stat(file_name, &st) == -1 || access(file_name, R_OK) != 0) {
- clean_up_temporary_files();
- adios((char *)0, "unable to access file \"%s\"", file_name);
- }
-
- (void)fprintf(composition_file, "#%s; name=\"%s\"; x-unix-mode=0%.3ho",
- content_type, ((p = strrchr(file_name, '/')) == (char *)0) ? file_name : p + 1, (unsigned short)(st.st_mode & 0777));
-
- if (strlen(file_name) > MAXPATHLEN) {
- clean_up_temporary_files();
- adios((char *)0, "attachment file name `%s' too long.", file_name);
- }
-
- (void)sprintf(cmd, "file '%s'", file_name);
-
- if ((fp = popen(cmd, "r")) != (FILE *)0 && fgets(cmd, sizeof (cmd), fp) != (char *)0) {
- *strchr(cmd, '\n') = '\0';