home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. CD ROM (Annual Premium Edition)
/
premium.zip
/
premium
/
IBMOS2_2
/
MAILMIND.ZIP
/
NETADDUS.C
< prev
next >
Wrap
Text File
|
1992-03-11
|
5KB
|
174 lines
/* netaddus.c - add %username% & %home_server% as NETBIOS names
* Thomas A. Marciniak, M.D. = ytm@nihccpc1.bitnet
* Division of Cancer Prevention & Control, NCI
*/
/* Revision history:
* 1.03 ytm 05/21/91 don't wipe out high netbios name numbers
* 1.02 ytm 02/01/91 add both user & server, check for existing
* 1.01 ytm 01/31/91 eliminate post routine
* 1.00 ytm 01/31/91 first release
*/
/* Program notes:
* Current versions are specific for TurboC.
*/
#include <local.h> /* standard definitions */
#include "netbios.h"
/* defines */
#define ADDUSER 1
#define ADDSERV 2
#define USER_NUM 4
#define SERV_NUM 5
#define MIN_SERV_LEN 5 /* minimum length of server name */
/* status structure */
#define STATUS_NOUSER 100
#define STATUS_NOSERV 101
#define STATUS_BADUSER 102
#define STATUS_BADSERV 103
#define STATUS_UNKNOWN 0xff
#define STATUS_STRUCT struct status
typedef STATUS_STRUCT
{
word wCode;
string sText;
};
STATUS_STRUCT aStatus[] =
{
{0, "OK"},
{13, "already present"},
{22, "at another station"},
{STATUS_NOUSER, "NO username!"},
{STATUS_NOSERV, "NO home_server!"},
{STATUS_BADUSER, "user number bad"},
{STATUS_BADSERV, "server number bad"},
{STATUS_UNKNOWN, "?"}
};
/* globals */
NB_STATUS Nb;
NCB Ncb;
/* function prototypes */
short main(void);
word NetAddName(string sName, boolean bGroup);
word NetAdapterStatus(string sName);
word NetDelName(string sName);
void SetNbName(string sTarget, string sSource);
/* main */
short main(void)
{
char caServGroup[NETBIOS_NAME_LEN];
char caUser[NETBIOS_NAME_LEN];
short i;
short iAddName = 0;
word wStatus;
printf("Netaddus v1.03\n");
if (getenv("USERNAME") == NULL) wStatus = STATUS_NOUSER;
else if (strlen(getenv("HOME_SERVER")) < MIN_SERV_LEN)
wStatus = STATUS_NOSERV;
else if ((wStatus = NetAdapterStatus("*")) == SUCCESS)
{
SetNbName(caUser, getenv("USERNAME"));
SetNbName(caServGroup, getenv("HOME_SERVER"));
caServGroup[0] = 'U';
caServGroup[1] = 'S';
caServGroup[2] = 'E';
caServGroup[3] = 'R';
iAddName = ADDUSER + ADDSERV;
for (i = 0; i < Nb.wNames && !wStatus; i++)
{
if (Nb.aNameEntry[i].byNcbNum == USER_NUM)
{
if (strcmp(Nb.aNameEntry[i].caName, caUser) SAME)
iAddName -= ADDUSER;
else wStatus = NetDelName(Nb.aNameEntry[i].caName);
}
else if (Nb.aNameEntry[i].byNcbNum == SERV_NUM)
{
if (strcmp(Nb.aNameEntry[i].caName, caServGroup) SAME)
iAddName -= ADDSERV;
else wStatus = NetDelName(Nb.aNameEntry[i].caName);
}
} /* for */
if (!wStatus && (iAddName & ADDUSER))
{
wStatus = NetAddName(caUser, FALSE);
if (!wStatus && (Ncb.NCB_NUM != USER_NUM)) wStatus = STATUS_BADUSER;
}
if (!wStatus && (iAddName & ADDSERV))
{
wStatus = NetAddName(caServGroup, TRUE);
if (!wStatus && (Ncb.NCB_NUM != SERV_NUM)) wStatus = STATUS_BADSERV;
}
}
for (i = 0; *(aStatus[i].sText) != '?'; i++)
if (aStatus[i].wCode == wStatus) break;
printf("Netaddus status: %s (%d)\n", aStatus[i].sText, Ncb.NCB_CMD_CPLT);
return(i);
} /* main */
/* check the adapter status */
word NetAdapterStatus(string sName)
{
memset(&Ncb, 0, sizeof(NCB));
Ncb.NCB_COMMAND = STATUS_WAIT;
SetNbName(Ncb.NCB_CALLNAME, sName);
Ncb.NCB_BUFFER = (void far *) &Nb;
Ncb.NCB_LENGTH = sizeof(Nb);
Ncb.NCB_CMD_CPLT = STATUS_UNKNOWN;
_ES = FP_SEG(&Ncb);
_BX = FP_OFF(&Ncb);
_AX = 0x0100;
geninterrupt(0x5c);
return(Ncb.NCB_CMD_CPLT);
} /* NetAdapterStatus */
/* add a name to the network */
word NetAddName(string sName, boolean bGroup)
{
printf("Adding %s ", sName);
memset(&Ncb, 0, sizeof(NCB));
if (bGroup) Ncb.NCB_COMMAND = ADD_GROUP_NAME_WAIT;
else Ncb.NCB_COMMAND = ADD_NAME_WAIT;
strncpy(Ncb.NCB_NAME, sName, NETBIOS_NAME_LEN);
Ncb.NCB_CMD_CPLT = STATUS_UNKNOWN;
_ES = FP_SEG(&Ncb);
_BX = FP_OFF(&Ncb);
_AX = 0x0100;
geninterrupt(0x5c);
if (Ncb.NCB_CMD_CPLT) printf("FAILED\n");
else printf("NETBIOS number %d\n", Ncb.NCB_NUM);
return(Ncb.NCB_CMD_CPLT);
} /* NetAddName */
/* delete a name from NETBIOS */
word NetDelName(string sName)
{
memset(&Ncb, 0, sizeof(NCB));
Ncb.NCB_COMMAND = DELETE_NAME_WAIT;
strncpy(Ncb.NCB_NAME, sName, NETBIOS_NAME_LEN);
Ncb.NCB_CMD_CPLT = STATUS_UNKNOWN;
_ES = FP_SEG(&Ncb);
_BX = FP_OFF(&Ncb);
_AX = 0x0100;
geninterrupt(0x5c);
return(Ncb.NCB_CMD_CPLT);
} /* NetDelName */
/* copy & blank fill a string to NB name length */
void SetNbName(string sTarget, string sSource)
{
string s, t;
memset(sTarget, 0, NETBIOS_NAME_LEN);
strncpy(sTarget, sSource, NETBIOS_NAME_LEN);
for (s = sTarget, t = s + NETBIOS_NAME_LEN - 1; s < t; s++)
*s = ((*s) ? toupper(*s) : ' ');
} /* SetNbName */