sj3lib, sj3stat: KNF

Formatter: knfmt 4.2.0
Signed-off-by: Masanori Ogino <mogino@acm.org>
This commit is contained in:
Masanori Ogino 2023-12-07 12:49:10 +09:00
commit 9d06a2203f
4 changed files with 1136 additions and 1040 deletions

1158
level1.c

File diff suppressed because it is too large Load diff

625
sj3lib.c

File diff suppressed because it is too large Load diff

View file

@ -16,18 +16,17 @@
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Software Research
* Associates shall not be used in advertising or otherwise to
* promote the sale, use or other dealings in this Software without prior
* Except as contained in this notice, the name of Software Research
* Associates shall not be used in advertising or otherwise to
* promote the sale, use or other dealings in this Software without prior
* written authorization from Software Research Associates.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -37,42 +36,45 @@
#include "sj3lib.h"
int
main (int argc, char** argv)
main(int argc, char **argv)
{
SJ3_CLIENT_ENV clnt;
SJ3_WHO_STRUCT who[64];
char ver[256];
char pname[256];
SJ3_CLIENT_ENV clnt;
SJ3_WHO_STRUCT who[64];
char ver[256];
char pname[256];
char *hname;
char *uname;
int i, n;
char *p;
int i, n;
char *p;
if ((argc > 1 && argv[1][0] == '-') || argc > 2) {
fprintf (stderr, "usage: %s [server]\n", argv[0]);
exit (1);
fprintf(stderr, "usage: %s [server]\n", argv[0]);
exit(1);
}
hname = getenv ("SJ3SERV");
uname = getenv ("USER");
hname = getenv("SJ3SERV");
uname = getenv("USER");
if (argc > 1) hname = argv[1];
if (uname == NULL) uname = getlogin();
if (uname == NULL) uname = "unknown";
sprintf (pname, "%d.sj3stat", getpid());
if (argc > 1)
hname = argv[1];
if (uname == NULL)
uname = getlogin();
if (uname == NULL)
uname = "unknown";
sprintf(pname, "%d.sj3stat", getpid());
if (sj3_make_connection(&clnt, hname, uname, pname) == ERROR) {
char *server, *status;
extern int sj3_error_number;
char *server, *status;
extern int sj3_error_number;
server = hname ? hname : "localhost";
switch (sj3_error_number) {
case SJ3_ServerDown: status = "down"; break;
case SJ3_GetHostByName: status = "unknown"; break;
default: status = "dead"; break;
case SJ3_ServerDown: status = "down"; break;
case SJ3_GetHostByName: status = "unknown"; break;
default: status = "dead"; break;
}
fprintf (stderr, "Server '%s' is %s.\n", server, status);
fprintf(stderr, "Server '%s' is %s.\n", server, status);
return 1;
}
@ -80,22 +82,23 @@ main (int argc, char** argv)
n = sj3_who(&clnt, who, sizeof(who)/sizeof(who[0]));
for (p = ver; *p; p++) {
while (*p) putc(*p++, stdout);
fprintf (stdout, "%%, ");
while (*p)
putc(*p++, stdout);
fprintf(stdout, "%%, ");
}
fprintf (stdout, "%d users.\n", n);
fprintf(stdout, "%d users.\n", n);
/*
* Change hostname entry's length.
* Because it's so short.
* Patched by Hidekazu Kuroki(hidekazu@cs.titech.ac.jp) 1996/8/10
*/
fprintf(stdout, "%-16s %-32s %-8s %s\n",
"(USER)", "(HOST)", "(FD)", "(CLIENT)");
fprintf(stdout, "%-16s %-32s %-8s %s\n",
"(USER)", "(HOST)", "(FD)", "(CLIENT)");
for (i = 0; i < n; i++) {
fprintf(stdout, "%-16s %-32s %-8d %s\n", who[i].username,
who[i].hostname, who[i].fd, who[i].progname);
fprintf(stdout, "%-16s %-32s %-8d %s\n", who[i].username,
who[i].hostname, who[i].fd, who[i].progname);
}
(void) sj3_erase_connection(&clnt);
(void)sj3_erase_connection(&clnt);
return 0;
}

318
string.c
View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 1994 Sony Corporation
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
@ -8,10 +8,10 @@
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@ -19,7 +19,7 @@
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
@ -55,25 +55,27 @@ void sj_sjis2jis(), sj_jis2sjis();
int
sj3_str_sjistoeuc(out, outlen, in, sjis_default, useflag)
unsigned char *out, *in, *sjis_default;
int outlen, *useflag;
unsigned char *out, *in, *sjis_default;
int outlen, *useflag;
{
unsigned char *sjis = in, *euc = out;
int n = 0;
unsigned char *sjis = in, *euc = out;
int n = 0;
unsigned short code, default_code;
euc[0] = '\0';
*useflag = 0;
if (!sjis) return 0;
if (!sjis)
return 0;
while (*sjis && n < outlen) {
if (issjis1(*sjis)) {
code = sjis2euc( (*sjis << 8) | *(sjis+1) );
if (n + 2 > outlen) return -1;
if (code == 0) {
default_code = sjis2euc((sjis_default[0] << 8)
| sjis_default[1]);
code = sjis2euc((*sjis << 8) | *(sjis + 1));
if (n + 2 > outlen)
return -1;
if (code == 0) {
default_code = sjis2euc((sjis_default[0] << 8) |
sjis_default[1]);
euc[n++] = default_code >> 8;
euc[n++] = default_code & 0xff;
(*useflag)++;
@ -83,40 +85,42 @@ int outlen, *useflag;
}
sjis += 2;
} else {
if( *sjis >= 0xa1 && *sjis <= 0xdf ) {
if (n + 2 > outlen) return -1;
euc[n++] = SS2;
if (*sjis >= 0xa1 && *sjis <= 0xdf) {
if (n + 2 > outlen)
return -1;
euc[n++] = SS2;
}
euc[n++] = *sjis;
sjis++;
}
}
if (n > outlen) return -1;
if (n > outlen)
return -1;
euc[n] = '\0';
return n;
}
int
sj3_str_euctosjis(out, outlen, in, sjis_default, useflag)
unsigned char *out, *in, *sjis_default;
int outlen, *useflag;
unsigned char *out, *in, *sjis_default;
int outlen, *useflag;
{
unsigned char *euc = in, *sjis = out;
int n = 0;
unsigned char *euc = in, *sjis = out;
int n = 0;
unsigned short code, default_code;
sjis[0] = '\0';
*useflag = 0;
if (!euc) return 0;
if (!euc)
return 0;
while (*euc && n < outlen) {
if (iseuc(*euc)) {
code = euc2sjis( (*euc << 8) | *(euc+1) );
if (n + 2 > outlen) return -1;
code = euc2sjis((*euc << 8) | *(euc + 1));
if (n + 2 > outlen)
return -1;
if (code == 0) {
sjis[n++] = sjis_default[0];
sjis[n++] = sjis_default[1];
@ -127,7 +131,8 @@ int outlen, *useflag;
}
euc += 2;
} else if (*euc == SS3) {
if (n + 2 > outlen) return -1;
if (n + 2 > outlen)
return -1;
sjis[n++] = sjis_default[0];
sjis[n++] = sjis_default[1];
euc += 3;
@ -140,56 +145,59 @@ int outlen, *useflag;
euc++;
}
}
if (n > outlen) return -1;
if (n > outlen)
return -1;
sjis[n] = '\0';
return n;
}
int
sj3_sjistoeuclen(str, max)
unsigned char *str;
int max;
unsigned char *str;
int max;
{
register int len = 0, bytes = 1;
register int len = 0, bytes = 1;
if (!max) return 0;
if (!max)
return 0;
while ((bytes <= max) && *str) {
if (issjis1(*str)) {
str++;
bytes++;
if (!*str) break;
len += 2;
str++;
bytes++;
if (!*str)
break;
len += 2;
} else if (*str & 0x80) {
len += 2;
} else {
len++;
len++;
}
str++;
bytes++;
str++;
bytes++;
}
return len;
return len;
}
#define MASK 0x7f
#define MSB 0x80
static unsigned char def_char[] = {0x81, 0x40};
static unsigned char def_char[] = {0x81, 0x40};
int
int
sj3_sjistoeuc(e, elen, s, slen)
unsigned char *s, *e;
int slen, elen;
unsigned char *s, *e;
int slen, elen;
{
int dummy;
return sj3_str_sjistoeuc(e, elen, s, def_char, &dummy);
}
int
int
sj3_euctosjis(s, slen, e, elen)
unsigned char *s, *e;
int slen, elen;
unsigned char *s, *e;
int slen, elen;
{
int dummy;
@ -197,153 +205,151 @@ int slen, elen;
}
void
sj_euc2sjis (s)
unsigned char *s;
sj_euc2sjis(s)
unsigned char *s;
{
s[0] &= MASK;
s[1] &= MASK;
sj_jis2sjis (s);
s[0] &= MASK;
s[1] &= MASK;
sj_jis2sjis(s);
}
void
sj_jis2sjis (s)
unsigned char *s;
sj_jis2sjis(s)
unsigned char *s;
{
register int high, low;
register int nh, nl;
register int high, low;
register int nh, nl;
high = s[0];
low = s[1];
nh = ((high - 0x21) >> 1) + 0x81;
if (nh > 0x9f)
nh += 0x40;
high = s[0];
low = s[1];
nh = ((high - 0x21) >> 1) + 0x81;
if (nh > 0x9f)
nh += 0x40;
if (high & 1) {
nl = low + 0x1f;
if (low > 0x5f)
nl ++;
nl = low + 0x1f;
if (low > 0x5f)
nl++;
} else
nl = low + 0x7e;
s[0] = nh;
s[1] = nl;
}
void
sj_sjis2euc(s)
unsigned char *s;
{
sj_sjis2jis(s);
s[0] |= MSB;
s[1] |= MSB;
}
void
sj_sjis2jis(s)
unsigned char *s;
{
register int byte1, byte2;
register unsigned char *sp;
sp = s;
byte1 = *sp++;
byte2 = *sp--;
if (byte1 <= 0x80 || byte1 >= 0xf0 ||
(byte1 >= 0xa0 && byte1 <= 0xdf) ||
byte2 <= 0x3f || byte2 >= 0xfe || byte2 == 0x7f) {
byte1 = 0x81;
byte2 = 0x40;
}
else
nl = low + 0x7e;
s[0] = nh;
s[1] = nl;
}
void
sj_sjis2euc (s)
unsigned char *s;
{
sj_sjis2jis (s);
s[0] |= MSB;
s[1] |= MSB;
}
void
sj_sjis2jis (s)
unsigned char *s;
{
register int byte1, byte2;
register unsigned char *sp;
sp = s;
byte1 = *sp++;
byte2 = *sp--;
if (byte1 <= 0x80 || byte1 >= 0xf0
|| ( byte1>=0xa0 && byte1<=0xdf )
|| byte2 <= 0x3f || byte2 >= 0xfe || byte2==0x7f ) {
byte1 = 0x81;
byte2 = 0x40;
}
byte1 -= (byte1 >= 0xa0) ? 0xc1 : 0x81;
byte1 -= (byte1 >= 0xa0) ? 0xc1 : 0x81;
if (byte2 >= 0x9f) {
*sp++ = (byte1 << 1) + 0x22;
*sp = byte2 - 0x7e;
}
else {
*sp++ = (byte1 << 1) + 0x21;
*sp = byte2 - ((byte2 <= 0x7e) ? 0x1f : 0x20 );
*sp++ = (byte1 << 1) + 0x22;
*sp = byte2 - 0x7e;
} else {
*sp++ = (byte1 << 1) + 0x21;
*sp = byte2 - ((byte2 <= 0x7e) ? 0x1f : 0x20);
}
}
unsigned short
sj3_jis2sjis(code)
unsigned short code;
unsigned short code;
{
unsigned char tmp[3];
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
sj_jis2sjis((unsigned char *) tmp);
code = (tmp[0] << 8) + tmp[1];
return(code);
unsigned char tmp[3];
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
sj_jis2sjis((unsigned char *)tmp);
code = (tmp[0] << 8) + tmp[1];
return (code);
}
unsigned short
sj3_jis2euc(code)
unsigned short code;
unsigned short code;
{
unsigned char tmp[3];
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
code = ((tmp[0] | 0x80) << 8) + (tmp[1] | 0x80);
return(code);
unsigned char tmp[3];
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
code = ((tmp[0] | 0x80) << 8) + (tmp[1] | 0x80);
return (code);
}
unsigned short
sj3_sjis2jis(code)
unsigned short code;
unsigned short code;
{
unsigned char tmp[3];
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
unsigned char tmp[3];
sj_sjis2jis((unsigned char *) tmp);
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
code = (tmp[0] << 8) + tmp[1];
sj_sjis2jis((unsigned char *)tmp);
return(code);
code = (tmp[0] << 8) + tmp[1];
return (code);
}
unsigned short
sj3_euc2sjis(code)
unsigned short code;
unsigned short code;
{
unsigned char tmp[3];
unsigned char tmp[3];
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
sj_euc2sjis((unsigned char *) tmp);
code = (tmp[0] << 8) + tmp[1];
sj_euc2sjis((unsigned char *)tmp);
return(code);
code = (tmp[0] << 8) + tmp[1];
return (code);
}
unsigned short
sj3_sjis2euc(code)
unsigned short code;
unsigned short code;
{
unsigned char tmp[3];
unsigned char tmp[3];
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
tmp[0] = (code >> 8) & 0xff;
tmp[1] = code & 0xff;
tmp[2] = '\0';
sj_sjis2euc((unsigned char *) tmp);
sj_sjis2euc((unsigned char *)tmp);
code = (tmp[0] << 8) + tmp[1];
code = (tmp[0] << 8) + tmp[1];
return(code);
return (code);
}