File: progname.c

package info (click to toggle)
shhmsg 1.4.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 148 kB
  • ctags: 78
  • sloc: ansic: 319; makefile: 134; sh: 10
file content (121 lines) | stat: -rw-r--r-- 3,462 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* $Id: progname.c,v 1.5 2002/03/02 19:37:36 sverrehu Exp $ */
/*------------------------------------------------------------------------
 |  FILE            progname.c
 |  MODULE OF       shhmsg - library for displaying messages.
 |
 |  DESCRIPTION     Routines for handling the name of the program.
 |                  This is used when displaying error messages,
 |                  according to the Unix tradition.
 |
 |  WRITTEN BY      Sverre H. Huseby <shh@thathost.com>
 +----------------------------------------------------------------------*/

#include <string.h>

#include "internal.h"
#include "shhmsg.h"

/*-----------------------------------------------------------------------+
|  PRIVATE DATA                                                          |
+-----------------------------------------------------------------------*/

static char *Argv0 = NULL;      /* the name of the program. */

/*-----------------------------------------------------------------------+
|  PUBLIC DATA                                                           |
+-----------------------------------------------------------------------*/

int _msgShowNameAlways = 0;     /* show program name always */

/*-----------------------------------------------------------------------+
|  PUBLIC FUNCTIONS                                                      |
+-----------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 |
 |  NAME          msgSetName
 |
 |  FUNCTION      Specify the name of this program (from argv[0]).
 |
 |  SYNOPSIS      #include "shhmsg.h"
 |                void msgSetName(const char *name);
 |
 |  INPUT         name   Name of this program, possibly including path.
 |
 |  DESCRIPTION   The program name specified is used by the errorfunctions
 |                to identify this program.
 */
void
msgSetName(const char *name)
{
    Argv0 = (char *) name;
}

/*------------------------------------------------------------------------
 |
 |  NAME          msgGetName
 |
 |  FUNCTION      Return the name of this program.
 |
 |  SYNOPSIS      #include "shhmsg.h"
 |                char *msgGetName(void);
 |
 |  RETURNS       Pointer to the name of this program, without any path.
 |
 |  DESCRIPTION   The name depends on the string passed to msgSetName().
 */
char *
msgGetName(void)
{
    char *ret;

  #ifdef __MSDOS__
    static char name[15];
    #define PATHSEP '\\'
  #else
    #define PATHSEP '/'
  #endif

    if (!Argv0)
        return("!!!");

  #ifdef __MSDOS__
    if ((ret = strrchr(Argv0, PATHSEP)) == NULL)
        ret = Argv0;
    else
        ++ret;
    strcpy(name, ret);
    if ((ret = strchr(name, '.')) != NULL)
        *ret = '\0';
    strlwr(name);
    ret = name;

  #else
    if ((ret = strrchr(Argv0, PATHSEP)) == NULL)
        ret = Argv0;
    else
        ++ret;
  #endif

    return ret;
}

/*------------------------------------------------------------------------
 |
 |  NAME          msgSetShowNameAlways
 |
 |  FUNCTION      Enable or disable the name of the program in all output.
 |
 |  SYNOPSIS      #include "shhmsg.h"
 |                void msgSetShowNameAlways(int onoff);
 |
 |  INPUT         onoff   1 or 0, 1 to show name always.
 |
 |  DESCRIPTION   This enables or disables the forcing of the name to show
 |                in all output.
 */
void
msgSetShowNameAlways(int onoff)
{
    _msgShowNameAlways = onoff;
}