chiark / gitweb /
File descriptor passing.
[mLib] / pquis.c
1 /* -*-c-*-
2  *
3  * $Id: pquis.c,v 1.1 1999/10/04 21:43:29 mdw Exp $
4  *
5  * Print strings, substituting the program name
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  * 
19  * mLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mLib; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: pquis.c,v $
33  * Revision 1.1  1999/10/04 21:43:29  mdw
34  * New function `pquis'.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <stdio.h>
41 #include <string.h>
42
43 #include "quis.h"
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @pquis@ --- *
48  *
49  * Arguments:   @FILE *fp@ = output stream to write on
50  *              @const char *p@ = pointer to string to write
51  *
52  * Returns:     Zero if everything worked, EOF if not.
53  *
54  * Use:         Writes the string @p@ to the output stream @fp@.  Occurrences
55  *              of the character `$' in @p@ are replaced by the program name
56  *              as reported by @quis@.  A `$$' is replaced by a single `$'
57  *              sign.
58  */
59
60 int pquis(FILE *fp, const char *p)
61 {
62   size_t sz;
63
64   while (*p) {
65     sz = strcspn(p, "$");
66     if (sz) {
67       if (fwrite(p, 1, sz, fp) < sz)
68         return (EOF);
69       p += sz;
70     }
71     if (*p == '$') {
72       p++;
73       if (*p == '$') {
74         if (fputc('$', fp) == EOF)
75           return (EOF);
76         p++;
77       } else {
78         if (fputs(pn__name, fp) == EOF)
79           return (EOF);
80       }
81     }
82   }
83   return (0);
84 }
85
86 /*----- That's all, folks -------------------------------------------------*/