File: misc.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (240 lines) | stat: -rw-r--r-- 6,333 bytes parent folder | download | duplicates (4)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* misc.c:
 *
 * miscellaneous funcs
 *
 * jim frost 10.05.89
 *
 * Copyright 1989, 1990, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "xloadimage.h"
#include "misc.h"
#ifdef VMS
#include "patchlevel."
#else
#include "patchlevel"
#endif
#include <signal.h>

extern int      _Xdebug;
extern char    *ProgramName;
extern Display *Disp;
extern int      Scrn;
extern char    *BuildDate;
extern char    *BuildUser;
extern char    *BuildSystem;

static char *signalName(sig)
     int sig;
{ static char buf[32];

  switch (sig) {
  case SIGSEGV:
    return("SEGV");
#ifdef	SIGBUS
  case SIGBUS:
    return("BUS");
#endif
  case SIGFPE:
    return("FPE");
  case SIGILL:
    return("ILL");
  default:
    sprintf(buf, "Signal %d", sig);
    return(buf);
  }
}

void memoryExhausted()
{
  fprintf(stderr,
	  "\n\nMemory has been exhausted; operation cannot continue (sorry).\n");
  if (_Xdebug)
    abort();
  else
    exit(1);
}

void internalError(sig)
     int sig;
{ static int handling_error= 0;
  int a, b;
  Screen *screen;

  switch(handling_error++) {
  case 0:
    printf("\n\n\
An internal error (%s) has occurred.  If you would like to file a bug\n\
report, please send email to %s\n\
with a description of how you triggered the bug, the output of xloadimage\n\
before the failure, and the following information:\n\n", signalName(sig),
	   AUTHOR_EMAIL);
    printf("Xloadimage Version %s.%s\n\n", VERSION, PATCHLEVEL);
    if (BuildUser)
      printf("Built by:     %s\n", BuildUser);
    if (BuildDate)
      printf("Built on:     %s\n", BuildDate);
    printf("Build system: %s\n", BuildSystem);
    
    if (Disp) {
      screen= ScreenOfDisplay(Disp, Scrn);
      printf("Server: %s Version %d\n", ServerVendor(Disp), VendorRelease(Disp));
      printf("Depths and visuals supported:\n");
      for (a= 0; a < screen->ndepths; a++) {
	printf("%2d:", screen->depths[a].depth);
	for (b= 0; b < screen->depths[a].nvisuals; b++)
	  printf(" %s", nameOfVisualClass(screen->depths[a].visuals[b].class));
	printf("\n");
      }
    }
    else
      printf("[No information on server; error occurred before connection]\n");
    break;
  case 1:
    fprintf(stderr, "\n\n\
An internal error has occurred within the internal error handler.  No more\n\
information about the error is available, sorry.\n");
    exit(1);
    break;
  }
  if (_Xdebug) /* dump core if -debug is on */
    abort();
  exit(1);
}

void version()
{
  printf("Xloadimage version %s.%s by Jim Frost.\n",
	 VERSION, PATCHLEVEL);
  printf("Built on %s\n", BuildSystem);
  printf("Please send email to %s for\npraise or bug reports.\n",
	 AUTHOR_EMAIL);
}

void usageHelp()
{
  printf("\nUsage: %s [global options] {[image options] image_name ...}\n\n",
	 tail(ProgramName));
  printf("\
Type `%s -help [option ...]' for information on a particular option, or\n\
`%s -help' to enter the interactive help facility.\n",
	 tail(ProgramName), tail(ProgramName));
  exit(1);
}

void usage()
{
  version();
  usageHelp();
  /* NOTREACHED */
}

char *tail(path)
     char *path;
{ int   s;
  char *t;

  t= path;
  for (s= 0; *(path + s) != '\0'; s++)
    if (*(path + s) == '/')
      t= path + s + 1;
  return(t);
}

/* simple error handler.  this provides us with some kind of error recovery.
 */

int errorHandler(disp, error)
     Display *disp;
     XErrorEvent *error;
{ char errortext[BUFSIZ];

  XGetErrorText(disp, error->error_code, errortext, BUFSIZ);
  fprintf(stderr, "xloadimage: X Error: %s on 0x%lx\n",
	  errortext, error->resourceid);
  if (_Xdebug) /* if -debug mode is enabled, dump a core when we hit this */
    abort();
  else
    return(0);
}

/*
  findstr - public-domain implementation of standard C 'strstr' library
            function (renamed and slightly modified to avoid naming
            conflicts - jimf)

  last edit:	02-Sep-1990	D A Gwyn

  This is an original implementation based on an idea by D M Sunday,
  essentially the "quick search" algorithm described in CACM V33 N8.
  Unlike Sunday's implementation, this one does not wander past the
  ends of the strings (which can cause malfunctions under certain
  circumstances), nor does it require the length of the searched
  text to be determined in advance.  There are numerous other subtle
  improvements too.  The code is intended to be fully portable, but in
  environments that do not conform to the C standard, you should check
  the sections below marked "configure as required".  There are also
  a few compilation options, as follows:
*/

#define BYTE_MAX 255

#define EOS '\0'		/* C string terminator */

char *					/* returns -> leftmost occurrence,
					   or null pointer if not present */
findstr( s1, s2 )
     char	*s1;		/* -> string to be searched */
     char	*s2;		/* -> search-pattern string */
{
  register byte	*t;		/* -> text character being tested */
  register byte	*p;		/* -> pattern char being tested */
  register byte	*tx;		/* -> possible start of match */
  register unsigned int	m;      /* length of pattern */
  register byte	*top;		/* -> high water mark in text */
  unsigned int  shift[BYTE_MAX + 1];	/* pattern shift table */

  if ( s1 == NULL || s2 == NULL )
    return NULL;		/* certainly, no match is found! */

  /* Precompute shift intervals based on the pattern;
     the length of the pattern is determined as a side effect: */

  bzero(&shift[1], (BYTE_MAX * sizeof(unsigned int)));

  /* Note: shift[0] is undefined at this point (fixed later). */

  for ( m = 1, p = (byte *)s2; *p != EOS; ++m, ++p )
    shift[(byte)*p] = m;

  {
    register byte c;

    c = BYTE_MAX;
    do
      shift[c] = m - shift[c];
    while ( --c > 0 );
    /* Note: shift[0] is still undefined at this point. */
  }

  shift[0] = --m; 		/* shift[EOS]; important details! */

  /* Try to find the pattern in the text string: */

  for ( top = tx = (byte *)s1; ; tx += shift[*(top = t)] ) {
    for ( t = tx, p = (byte *)s2; ; ++t, ++p ) {
      if ( *p == EOS )       /* entire pattern matched */
	return (char *)tx;
      if ( *p != *t )
	break;
    }
    if ( t < top ) /* idea due to ado@elsie.nci.nih.gov */
      t = top;	   /* already scanned this far for EOS */
    do	{
      if ( *t == EOS )
	return NULL;	/* no match */
    } while ( ++t - tx != m );	/* < */
  }
}