chiark / gitweb /
doc: Document summary values of TOFU_STATS
[gnupg2.git] / g10 / plaintext.c
1 /* plaintext.c -  process plaintext packets
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3  *               2006, 2009, 2010 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <https://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #ifdef HAVE_DOSISH_SYSTEM
28 # include <fcntl.h> /* for setmode() */
29 #endif
30
31 #include "gpg.h"
32 #include "util.h"
33 #include "options.h"
34 #include "packet.h"
35 #include "ttyio.h"
36 #include "filter.h"
37 #include "main.h"
38 #include "status.h"
39 #include "i18n.h"
40
41
42 /* Get the output filename.  On success, the actual filename that is
43    used is set in *FNAMEP and a filepointer is returned in *FP.
44
45    EMBEDDED_NAME AND EMBEDDED_NAMELEN are normally stored in a
46    plaintext packet.  EMBEDDED_NAMELEN should not include any NUL
47    terminator (EMBEDDED_NAME does not need to be NUL terminated).
48
49    DATA is the iobuf containing the input data.  We just use it to get
50    the input file's filename.
51
52    On success, the caller is responsible for calling xfree on *FNAMEP
53    and calling es_close on *FPP.  */
54 gpg_error_t
55 get_output_file (const byte *embedded_name, int embedded_namelen,
56                  iobuf_t data, char **fnamep, estream_t *fpp)
57 {
58   gpg_error_t err = 0;
59   char *fname = NULL;
60   estream_t fp = NULL;
61   int nooutput = 0;
62
63   /* Create the filename as C string.  */
64   if (opt.outfp)
65     {
66       fname = xtrystrdup ("[FP]");
67       if (!fname)
68         {
69           err = gpg_error_from_syserror ();
70           goto leave;
71         }
72     }
73   else if (opt.outfile)
74     {
75       fname = xtrystrdup (opt.outfile);
76       if (!fname)
77         {
78           err = gpg_error_from_syserror ();
79           goto leave;
80         }
81     }
82   else if (embedded_namelen == 8 && !memcmp (embedded_name, "_CONSOLE", 8))
83     {
84       log_info (_("data not saved; use option \"--output\" to save it\n"));
85       nooutput = 1;
86     }
87   else if (!opt.flags.use_embedded_filename)
88     {
89       if (data)
90         fname = make_outfile_name (iobuf_get_real_fname (data));
91       if (!fname)
92         fname = ask_outfile_name (embedded_name, embedded_namelen);
93       if (!fname)
94         {
95           err = gpg_error (GPG_ERR_GENERAL);    /* Can't create file. */
96           goto leave;
97         }
98     }
99   else
100     fname = utf8_to_native (embedded_name, embedded_namelen, 0);
101
102   if (nooutput)
103     ;
104   else if (opt.outfp)
105     {
106       fp = opt.outfp;
107       es_set_binary (fp);
108     }
109   else if (iobuf_is_pipe_filename (fname) || !*fname)
110     {
111       /* Special file name, no filename, or "-" given; write to the
112        * file descriptor or to stdout. */
113       int fd;
114       char xname[64];
115
116       fd = check_special_filename (fname, 1, 0);
117       if (fd == -1)
118         {
119           /* Not a special filename, thus we want stdout.  */
120           fp = es_stdout;
121           es_set_binary (fp);
122         }
123       else if (!(fp = es_fdopen_nc (fd, "wb")))
124         {
125           err = gpg_error_from_syserror ();
126           snprintf (xname, sizeof xname, "[fd %d]", fd);
127           log_error (_("can't open '%s': %s\n"), xname, gpg_strerror (err));
128           goto leave;
129         }
130     }
131   else
132     {
133       while (!overwrite_filep (fname))
134         {
135           char *tmp = ask_outfile_name (NULL, 0);
136           if (!tmp || !*tmp)
137             {
138               xfree (tmp);
139               /* FIXME: Below used to be GPG_ERR_CREATE_FILE */
140               err = gpg_error (GPG_ERR_GENERAL);
141               goto leave;
142             }
143           xfree (fname);
144           fname = tmp;
145         }
146     }
147
148 #ifndef __riscos__
149   if (opt.outfp && is_secured_file (es_fileno (opt.outfp)))
150     {
151       err = gpg_error (GPG_ERR_EPERM);
152       log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err));
153       goto leave;
154     }
155   else if (fp || nooutput)
156     ;
157   else if (is_secured_filename (fname))
158     {
159       gpg_err_set_errno (EPERM);
160       err = gpg_error_from_syserror ();
161       log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err));
162       goto leave;
163     }
164   else if (!(fp = es_fopen (fname, "wb")))
165     {
166       err = gpg_error_from_syserror ();
167       log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err));
168       goto leave;
169     }
170 #else /* __riscos__ */
171   /* If no output filename was given, i.e. we constructed it, convert
172      all '.' in fname to '/' but not vice versa as we don't create
173      directories! */
174   if (!opt.outfile)
175     for (c = 0; fname[c]; ++c)
176       if (fname[c] == '.')
177         fname[c] = '/';
178
179   if (fp || nooutput)
180     ;
181   else
182     {
183       /* Note: riscos stuff is not expected to work anymore.  If we
184          want to port it again to riscos we should do most of the suff
185          in estream.  FIXME: Consider to remove all riscos special
186          cases.  */
187       fp = fopen (fname, "wb");
188       if (!fp)
189         {
190           log_error (_("error creating '%s': %s\n"), fname, gpg_strerror (err));
191           err = GPG_ERR_CREATE_FILE;
192           if (errno == 106)
193             log_info ("Do output file and input file have the same name?\n");
194           goto leave;
195         }
196
197       /* If there's a ,xxx extension in the embedded filename,
198          use that, else check whether the user input (in fname)
199          has a ,xxx appended, then use that in preference */
200       if ((c = riscos_get_filetype_from_string (embedded_name,
201                                                 embedded_namelen)) != -1)
202         filetype = c;
203       if ((c = riscos_get_filetype_from_string (fname, strlen (fname))) != -1)
204         filetype = c;
205       riscos_set_filetype_by_number (fname, filetype);
206     }
207 #endif /* __riscos__ */
208
209  leave:
210   if (err)
211     {
212       if (fp && fp != es_stdout && fp != opt.outfp)
213         es_fclose (fp);
214       xfree (fname);
215       return err;
216     }
217
218   *fnamep = fname;
219   *fpp = fp;
220   return 0;
221 }
222
223 /* Handle a plaintext packet.  If MFX is not NULL, update the MDs
224  * Note: We should have used the filter stuff here, but we have to add
225  * some easy mimic to set a read limit, so we calculate only the bytes
226  * from the plaintext.  */
227 int
228 handle_plaintext (PKT_plaintext * pt, md_filter_context_t * mfx,
229                   int nooutput, int clearsig)
230 {
231   char *fname = NULL;
232   estream_t fp = NULL;
233   static off_t count = 0;
234   int err = 0;
235   int c;
236   int convert;
237 #ifdef __riscos__
238   int filetype = 0xfff;
239 #endif
240
241   if (pt->mode == 't' || pt->mode == 'u' || pt->mode == 'm')
242     convert = pt->mode;
243   else
244     convert = 0;
245
246   /* Let people know what the plaintext info is. This allows the
247      receiving program to try and do something different based on the
248      format code (say, recode UTF-8 to local). */
249   if (!nooutput && is_status_enabled ())
250     {
251       char status[50];
252
253       /* Better make sure that stdout has been flushed in case the
254          output will be written to it.  This is to make sure that no
255          not-yet-flushed stuff will be written after the plaintext
256          status message.  */
257       es_fflush (es_stdout);
258
259       snprintf (status, sizeof status,
260                 "%X %lu ", (byte) pt->mode, (ulong) pt->timestamp);
261       write_status_text_and_buffer (STATUS_PLAINTEXT,
262                                     status, pt->name, pt->namelen, 0);
263
264       if (!pt->is_partial)
265         {
266           snprintf (status, sizeof status, "%lu", (ulong) pt->len);
267           write_status_text (STATUS_PLAINTEXT_LENGTH, status);
268         }
269     }
270
271   if (! nooutput)
272     {
273       err = get_output_file (pt->name, pt->namelen, pt->buf, &fname, &fp);
274       if (err)
275         goto leave;
276     }
277
278   if (!pt->is_partial)
279     {
280       /* We have an actual length (which might be zero). */
281
282       if (clearsig)
283         {
284           log_error ("clearsig encountered while not expected\n");
285           err = gpg_error (GPG_ERR_UNEXPECTED);
286           goto leave;
287         }
288
289       if (convert) /* Text mode.  */
290         {
291           for (; pt->len; pt->len--)
292             {
293               if ((c = iobuf_get (pt->buf)) == -1)
294                 {
295                   err = gpg_error_from_syserror ();
296                   log_error ("problem reading source (%u bytes remaining)\n",
297                              (unsigned) pt->len);
298                   goto leave;
299                 }
300               if (mfx->md)
301                 gcry_md_putc (mfx->md, c);
302 #ifndef HAVE_DOSISH_SYSTEM
303               /* Convert to native line ending. */
304               /* fixme: this hack might be too simple */
305               if (c == '\r' && convert != 'm')
306                 continue;
307 #endif
308               if (fp)
309                 {
310                   if (opt.max_output && (++count) > opt.max_output)
311                     {
312                       log_error ("error writing to '%s': %s\n",
313                                  fname, "exceeded --max-output limit\n");
314                       err = gpg_error (GPG_ERR_TOO_LARGE);
315                       goto leave;
316                     }
317                   else if (es_putc (c, fp) == EOF)
318                     {
319                       if (es_ferror (fp))
320                         err = gpg_error_from_syserror ();
321                       else
322                         err = gpg_error (GPG_ERR_EOF);
323                       log_error ("error writing to '%s': %s\n",
324                                  fname, gpg_strerror (err));
325                       goto leave;
326                     }
327                 }
328             }
329         }
330       else  /* Binary mode.  */
331         {
332           byte *buffer = xmalloc (32768);
333           while (pt->len)
334             {
335               int len = pt->len > 32768 ? 32768 : pt->len;
336               len = iobuf_read (pt->buf, buffer, len);
337               if (len == -1)
338                 {
339                   err = gpg_error_from_syserror ();
340                   log_error ("problem reading source (%u bytes remaining)\n",
341                              (unsigned) pt->len);
342                   xfree (buffer);
343                   goto leave;
344                 }
345               if (mfx->md)
346                 gcry_md_write (mfx->md, buffer, len);
347               if (fp)
348                 {
349                   if (opt.max_output && (count += len) > opt.max_output)
350                     {
351                       log_error ("error writing to '%s': %s\n",
352                                  fname, "exceeded --max-output limit\n");
353                       err = gpg_error (GPG_ERR_TOO_LARGE);
354                       xfree (buffer);
355                       goto leave;
356                     }
357                   else if (es_fwrite (buffer, 1, len, fp) != len)
358                     {
359                       err = gpg_error_from_syserror ();
360                       log_error ("error writing to '%s': %s\n",
361                                  fname, gpg_strerror (err));
362                       xfree (buffer);
363                       goto leave;
364                     }
365                 }
366               pt->len -= len;
367             }
368           xfree (buffer);
369         }
370     }
371   else if (!clearsig)
372     {
373       if (convert)
374         {                       /* text mode */
375           while ((c = iobuf_get (pt->buf)) != -1)
376             {
377               if (mfx->md)
378                 gcry_md_putc (mfx->md, c);
379 #ifndef HAVE_DOSISH_SYSTEM
380               if (c == '\r' && convert != 'm')
381                 continue;       /* fixme: this hack might be too simple */
382 #endif
383               if (fp)
384                 {
385                   if (opt.max_output && (++count) > opt.max_output)
386                     {
387                       log_error ("Error writing to '%s': %s\n",
388                                  fname, "exceeded --max-output limit\n");
389                       err = gpg_error (GPG_ERR_TOO_LARGE);
390                       goto leave;
391                     }
392                   else if (es_putc (c, fp) == EOF)
393                     {
394                       if (es_ferror (fp))
395                         err = gpg_error_from_syserror ();
396                       else
397                         err = gpg_error (GPG_ERR_EOF);
398                       log_error ("error writing to '%s': %s\n",
399                                  fname, gpg_strerror (err));
400                       goto leave;
401                     }
402                 }
403             }
404         }
405       else
406         {                       /* binary mode */
407           byte *buffer;
408           int eof_seen = 0;
409
410           buffer = xtrymalloc (32768);
411           if (!buffer)
412             {
413               err = gpg_error_from_syserror ();
414               goto leave;
415             }
416
417           while (!eof_seen)
418             {
419               /* Why do we check for len < 32768:
420                * If we won't, we would practically read 2 EOFs but
421                * the first one has already popped the block_filter
422                * off and therefore we don't catch the boundary.
423                * So, always assume EOF if iobuf_read returns less bytes
424                * then requested */
425               int len = iobuf_read (pt->buf, buffer, 32768);
426               if (len == -1)
427                 break;
428               if (len < 32768)
429                 eof_seen = 1;
430               if (mfx->md)
431                 gcry_md_write (mfx->md, buffer, len);
432               if (fp)
433                 {
434                   if (opt.max_output && (count += len) > opt.max_output)
435                     {
436                       log_error ("error writing to '%s': %s\n",
437                                  fname, "exceeded --max-output limit\n");
438                       err = gpg_error (GPG_ERR_TOO_LARGE);
439                       xfree (buffer);
440                       goto leave;
441                     }
442                   else if (es_fwrite (buffer, 1, len, fp) != len)
443                     {
444                       err = gpg_error_from_syserror ();
445                       log_error ("error writing to '%s': %s\n",
446                                  fname, gpg_strerror (err));
447                       xfree (buffer);
448                       goto leave;
449                     }
450                 }
451             }
452           xfree (buffer);
453         }
454       pt->buf = NULL;
455     }
456   else /* Clear text signature - don't hash the last CR,LF.   */
457     {
458       int state = 0;
459
460       while ((c = iobuf_get (pt->buf)) != -1)
461         {
462           if (fp)
463             {
464               if (opt.max_output && (++count) > opt.max_output)
465                 {
466                   log_error ("error writing to '%s': %s\n",
467                              fname, "exceeded --max-output limit\n");
468                   err = gpg_error (GPG_ERR_TOO_LARGE);
469                   goto leave;
470                 }
471               else if (es_putc (c, fp) == EOF)
472                 {
473                   err = gpg_error_from_syserror ();
474                   log_error ("error writing to '%s': %s\n",
475                              fname, gpg_strerror (err));
476                   goto leave;
477                 }
478             }
479           if (!mfx->md)
480             continue;
481           if (state == 2)
482             {
483               gcry_md_putc (mfx->md, '\r');
484               gcry_md_putc (mfx->md, '\n');
485               state = 0;
486             }
487           if (!state)
488             {
489               if (c == '\r')
490                 state = 1;
491               else if (c == '\n')
492                 state = 2;
493               else
494                 gcry_md_putc (mfx->md, c);
495             }
496           else if (state == 1)
497             {
498               if (c == '\n')
499                 state = 2;
500               else
501                 {
502                   gcry_md_putc (mfx->md, '\r');
503                   if (c == '\r')
504                     state = 1;
505                   else
506                     {
507                       state = 0;
508                       gcry_md_putc (mfx->md, c);
509                     }
510                 }
511             }
512         }
513       pt->buf = NULL;
514     }
515
516   if (fp && fp != es_stdout && fp != opt.outfp && es_fclose (fp))
517     {
518       err = gpg_error_from_syserror ();
519       log_error ("error closing '%s': %s\n", fname, gpg_strerror (err));
520       fp = NULL;
521       goto leave;
522     }
523   fp = NULL;
524
525  leave:
526   /* Make sure that stdout gets flushed after the plaintext has been
527      handled.  This is for extra security as we do a flush anyway
528      before checking the signature.  */
529   if (es_fflush (es_stdout))
530     {
531       /* We need to check the return code to detect errors like disk
532          full for short plaintexts.  See bug#1207.  Checking return
533          values is a good idea in any case.  */
534       if (!err)
535         err = gpg_error_from_syserror ();
536       log_error ("error flushing '%s': %s\n", "[stdout]",
537                  gpg_strerror (err));
538     }
539
540   if (fp && fp != es_stdout && fp != opt.outfp)
541     es_fclose (fp);
542   xfree (fname);
543   return err;
544 }
545
546
547 static void
548 do_hash (gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode)
549 {
550   text_filter_context_t tfx;
551   int c;
552
553   if (textmode)
554     {
555       memset (&tfx, 0, sizeof tfx);
556       iobuf_push_filter (fp, text_filter, &tfx);
557     }
558   if (md2)
559     {                           /* work around a strange behaviour in pgp2 */
560       /* It seems that at least PGP5 converts a single CR to a CR,LF too */
561       int lc = -1;
562       while ((c = iobuf_get (fp)) != -1)
563         {
564           if (c == '\n' && lc == '\r')
565             gcry_md_putc (md2, c);
566           else if (c == '\n')
567             {
568               gcry_md_putc (md2, '\r');
569               gcry_md_putc (md2, c);
570             }
571           else if (c != '\n' && lc == '\r')
572             {
573               gcry_md_putc (md2, '\n');
574               gcry_md_putc (md2, c);
575             }
576           else
577             gcry_md_putc (md2, c);
578
579           if (md)
580             gcry_md_putc (md, c);
581           lc = c;
582         }
583     }
584   else
585     {
586       while ((c = iobuf_get (fp)) != -1)
587         {
588           if (md)
589             gcry_md_putc (md, c);
590         }
591     }
592 }
593
594
595 /****************
596  * Ask for the detached datafile and calculate the digest from it.
597  * INFILE is the name of the input file.
598  */
599 int
600 ask_for_detached_datafile (gcry_md_hd_t md, gcry_md_hd_t md2,
601                            const char *inname, int textmode)
602 {
603   progress_filter_context_t *pfx;
604   char *answer = NULL;
605   IOBUF fp;
606   int rc = 0;
607
608   pfx = new_progress_context ();
609   fp = open_sigfile (inname, pfx);      /* Open default file. */
610
611   if (!fp && !opt.batch)
612     {
613       int any = 0;
614       tty_printf (_("Detached signature.\n"));
615       do
616         {
617           char *name;
618
619           xfree (answer);
620           tty_enable_completion (NULL);
621           name = cpr_get ("detached_signature.filename",
622                           _("Please enter name of data file: "));
623           tty_disable_completion ();
624           cpr_kill_prompt ();
625           answer = make_filename (name, (void *) NULL);
626           xfree (name);
627
628           if (any && !*answer)
629             {
630               rc = gpg_error (GPG_ERR_GENERAL); /*G10ERR_READ_FILE */
631               goto leave;
632             }
633           fp = iobuf_open (answer);
634           if (fp && is_secured_file (iobuf_get_fd (fp)))
635             {
636               iobuf_close (fp);
637               fp = NULL;
638               gpg_err_set_errno (EPERM);
639             }
640           if (!fp && errno == ENOENT)
641             {
642               tty_printf ("No such file, try again or hit enter to quit.\n");
643               any++;
644             }
645           else if (!fp)
646             {
647               rc = gpg_error_from_syserror ();
648               log_error (_("can't open '%s': %s\n"), answer,
649                          strerror (errno));
650               goto leave;
651             }
652         }
653       while (!fp);
654     }
655
656   if (!fp)
657     {
658       if (opt.verbose)
659         log_info (_("reading stdin ...\n"));
660       fp = iobuf_open (NULL);
661       log_assert (fp);
662     }
663   do_hash (md, md2, fp, textmode);
664   iobuf_close (fp);
665
666 leave:
667   xfree (answer);
668   release_progress_context (pfx);
669   return rc;
670 }
671
672
673
674 /* Hash the given files and append the hash to hash contexts MD and
675  * MD2.  If FILES is NULL, stdin is hashed.  */
676 int
677 hash_datafiles (gcry_md_hd_t md, gcry_md_hd_t md2, strlist_t files,
678                 const char *sigfilename, int textmode)
679 {
680   progress_filter_context_t *pfx;
681   IOBUF fp;
682   strlist_t sl;
683
684   pfx = new_progress_context ();
685
686   if (!files)
687     {
688       /* Check whether we can open the signed material.  We avoid
689          trying to open a file if run in batch mode.  This assumed
690          data file for a sig file feature is just a convenience thing
691          for the command line and the user needs to read possible
692          warning messages. */
693       if (!opt.batch)
694         {
695           fp = open_sigfile (sigfilename, pfx);
696           if (fp)
697             {
698               do_hash (md, md2, fp, textmode);
699               iobuf_close (fp);
700               release_progress_context (pfx);
701               return 0;
702             }
703         }
704       log_error (_("no signed data\n"));
705       release_progress_context (pfx);
706       return gpg_error (GPG_ERR_NO_DATA);
707     }
708
709
710   for (sl = files; sl; sl = sl->next)
711     {
712       fp = iobuf_open (sl->d);
713       if (fp && is_secured_file (iobuf_get_fd (fp)))
714         {
715           iobuf_close (fp);
716           fp = NULL;
717           gpg_err_set_errno (EPERM);
718         }
719       if (!fp)
720         {
721           int rc = gpg_error_from_syserror ();
722           log_error (_("can't open signed data '%s'\n"),
723                      print_fname_stdin (sl->d));
724           release_progress_context (pfx);
725           return rc;
726         }
727       handle_progress (pfx, fp, sl->d);
728       do_hash (md, md2, fp, textmode);
729       iobuf_close (fp);
730     }
731
732   release_progress_context (pfx);
733   return 0;
734 }
735
736
737 /* Hash the data from file descriptor DATA_FD and append the hash to hash
738    contexts MD and MD2.  */
739 int
740 hash_datafile_by_fd (gcry_md_hd_t md, gcry_md_hd_t md2, int data_fd,
741                      int textmode)
742 {
743   progress_filter_context_t *pfx = new_progress_context ();
744   iobuf_t fp;
745
746   if (is_secured_file (data_fd))
747     {
748       fp = NULL;
749       gpg_err_set_errno (EPERM);
750     }
751   else
752     fp = iobuf_fdopen_nc (data_fd, "rb");
753
754   if (!fp)
755     {
756       int rc = gpg_error_from_syserror ();
757       log_error (_("can't open signed data fd=%d: %s\n"),
758                  data_fd, strerror (errno));
759       release_progress_context (pfx);
760       return rc;
761     }
762
763   handle_progress (pfx, fp, NULL);
764
765   do_hash (md, md2, fp, textmode);
766
767   iobuf_close (fp);
768
769   release_progress_context (pfx);
770   return 0;
771 }
772
773
774 /* Set up a plaintext packet with the appropriate filename.  If there
775    is a --set-filename, use it (it's already UTF8).  If there is a
776    regular filename, UTF8-ize it if necessary.  If there is no
777    filenames at all, set the field empty. */
778
779 PKT_plaintext *
780 setup_plaintext_name (const char *filename, IOBUF iobuf)
781 {
782   PKT_plaintext *pt;
783
784   if ((filename && !iobuf_is_pipe_filename (filename))
785        || (opt.set_filename && !iobuf_is_pipe_filename (opt.set_filename)))
786     {
787       char *s;
788
789       if (opt.set_filename)
790         s = make_basename (opt.set_filename, iobuf_get_real_fname (iobuf));
791       else if (filename && !opt.flags.utf8_filename)
792         {
793           char *tmp = native_to_utf8 (filename);
794           s = make_basename (tmp, iobuf_get_real_fname (iobuf));
795           xfree (tmp);
796         }
797       else
798         s = make_basename (filename, iobuf_get_real_fname (iobuf));
799
800       pt = xmalloc (sizeof *pt + strlen (s) - 1);
801       pt->namelen = strlen (s);
802       memcpy (pt->name, s, pt->namelen);
803       xfree (s);
804     }
805   else
806     {
807       /* no filename */
808       pt = xmalloc (sizeof *pt - 1);
809       pt->namelen = 0;
810     }
811
812   return pt;
813 }