Category : Files from Magazines
Archive   : PCTJ8808.ZIP
Filename : NBSEND.C

 
Output of file : NBSEND.C contained in archive : PCTJ8808.ZIP
/**************
*
* NBSEND.C
*
* by Ralph Davis
*
* SYNTAX: NBSEND [ ...]
*
***************/

#include "network.h" /* See Listing 2 */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


/* Just a formality, to avert a compiler warning message */

FORWARD void main(int argc, char *argv[]);


void main(int argc, char *argv[])
{
char *SendBuff;
char *SourceUser, *TargetUser;
int ThisStation;
char *filename;
struct stat FileStat;
unsigned long FileSize;
int File1;
int retcode;
unsigned int bytesread;
int NameNumber1;
int SessionNumber1;
struct Nb_Status StatusBuff;
NCB ResetNCB, AddNameNCB, CallNCB, StatusNCB,
DeleteNameNCB, HangupNCB, SendNCB;
int NumConnections;
char ConnectionList[100];
int TransmissionNum;
char *tempptr;
unsigned int len;
register int i;
register int FilesSent = 0;
char OutMessage[61];
char *BroadcastMessage;
int OldBroadcastMode;
long ltime;

if (argc < 3)
{
fprintf(stderr,
"\nUsage: NBSEND "
"[...]\n");
exit(1);
}

/* Allocate a nice big buffer.

(unsigned int) is necessary--otherwise the compiler
interprets the bit pattern representing 64000 as a
negative number, and malloc() fails. */

SendBuff = malloc((unsigned int)64000);

if (SendBuff == NULL)
{
perror("");
exit(1);
}

TargetUser = strupr(argv[1]);

/* Reset NetBIOS adapter zero, requesting the default
number of sessions and command blocks (0 indicates default)
*/
retcode = NbReset(&ResetNCB, 0, 0, 0);

NbEvaluateRetcode(retcode);

/* Zero out the status buffer before checking the
status */

memset((char *)&StatusBuff, '\0', sizeof(struct Nb_Status));
retcode = NbStatus(&StatusNCB, 0, "*", &StatusBuff, 0L,
WAIT_YES);

/* If the PossibleNCB field of the status buffer is
still zero, NetBIOS is probably not installed. */

if (StatusBuff.PossibleNCBs == 0)
{
fprintf(stderr,"\nNetBIOS not installed\n");
exit(1);
}

NbEvaluateRetcode(retcode);

/* Find out where the target user is logged on. */

retcode = NetObjectToConnections(TargetUser, NET_USER,
&NumConnections, ConnectionList);

if (NumConnections == 0)
{
fprintf(stderr, "\nUser %s not logged onto network\n",
TargetUser);
exit(1);
}

/* Get this station's user name and station number. */

SourceUser = NetGetUserName(0);
ThisStation = NetGetStationNumber();

/* Add the user's name to the NetBIOS name table. */

retcode = NbAddName(&AddNameNCB, SourceUser, 0, 0L,

&NameNumber1, WAIT_YES, 0);
NbEvaluateRetcode(retcode);

/* Get current broadcast mode.

Also, set broadcast mode so that the target user's
answer will not display on the screen; only this
program will be able to read it. */

OldBroadcastMode = NetSetBroadcastMode(3);
sprintf(OutMessage, "CAN YOU RECEIVE FILES FROM %s[%-d] "
"(YES/NO)?",
SourceUser, ThisStation);

/* Send a message to every station where TargetUser
is logged in */

retcode = NetBroadcastMessage(OutMessage, &NumConnections,
ConnectionList);

if (retcode == 0 && NumConnections > 0)
{
/* TargetUser got the message, wait 60 seconds for
an answer */
time(<ime);
BroadcastMessage = NULL;

while ((time(NULL) - ltime < 60L) &&
(BroadcastMessage == NULL))
BroadcastMessage = NetGetBroadcastMessage();
}
else
{
/* Couldn't contact TargetUser--abort the program */

fprintf(stderr, "\nUnable to contact %s\n", TargetUser);
NetSetBroadcastMode(OldBroadcastMode);
exit(1);
}

NetSetBroadcastMode(OldBroadcastMode);

/* Message begins with Target user name and station
number, followed by a colon and a space. Find
the colon, then move 2 characters past it. */

if (BroadcastMessage != NULL)
tempptr = strrchr(BroadcastMessage, ':') + 2;
else
tempptr = "N";

if (BroadcastMessage == NULL || *tempptr != 'Y')
{
/* Either no answer was received, or the answer
wasn't YES */
fprintf(stderr, "%s unable to receive transmission\n",
TargetUser);
exit(1);
}

time(<ime);

retcode = -1;

/* Try 60 seconds to establish a session with TargetUser,
using WAIT option. Send and receive timeouts are 1 minute
(120 half-seconds). */

while ((time(NULL) - ltime < 60L) && retcode != 0)
retcode = NbCall(&CallNCB, 0, SourceUser, TargetUser,
120, 120, 0L, &SessionNumber1, WAIT_YES);

NbEvaluateRetcode(retcode);

for (i = 2; i < argc; i++)
{
/* Pull file name from the command line */
filename = strupr(argv[i]);

/* Make sure that a) file exists, and b) it's
not a directory */

retcode = stat(filename, &FileStat);

if (retcode)
{
perror(filename);
exit(1);
}

if (FileStat.st_mode & S_IFDIR)
continue;

/* We'll use the size of the file to determine how
many bytes to read in */

FileSize = FileStat.st_size;

/* Lock the file */

if (NetLockFile(filename) != 0)
{
fprintf(stderr, "Unable to lock file %s\n", filename);
continue;
}

/* Open the file for shared, read-only access.
Also, open it in binary mode so the data
comes in as is. */

File1 = sopen(filename, (int)(O_RDONLY | O_BINARY),
(int)(SH_DENYNO));

if (File1 == (-1))
{
perror(filename);
exit(1);
}

TransmissionNum = 0;

/* See if a full path or a drive letter was specified */

tempptr = strrchr(filename, '\\');

if (tempptr == NULL)
tempptr = strrchr(filename, ':');

if (tempptr != NULL)
++tempptr;
else
tempptr = filename;

len = strlen(tempptr) + 1;

do
{
++TransmissionNum;

/* If this is the first transmission for this file,
preface the file contents with the file name */

if (TransmissionNum == 1)
strcpy(SendBuff, tempptr);
else
len = 0;

/* If the file's too big for our buffer, read in
64000 bytes minus the length of the filename.
Otherwise, read in the whole file. */

if (FileSize > (64000L - (long)len))
bytesread = read(File1, &SendBuff[len],
((unsigned int)64000) - len);
else
bytesread = read(File1, &SendBuff[len],
(unsigned int)FileSize);

if ((int)bytesread == (-1))
{
perror(filename);
exit(1);
}

retcode = NbSend(&SendNCB, 0, CallNCB.NCB_LSN, SendBuff,
bytesread+len, 0L, WAIT_YES);

NbEvaluateRetcode(retcode);
}
while (bytesread == (((unsigned int)64000) - len));
printf("%s\n", strupr(filename));
++FilesSent;
close(File1);

/* Unlock the file */
NetClearFile(filename);
}

if (FilesSent == 1)
/* It's not too much trouble to make this program use
good grammar */
printf("1 file transmitted\n");
else
printf("%-d files transmitted\n", FilesSent);

/* Signal the receiver that we're finished */
strcpy(SendBuff, "END OF TRANSMISSION");
retcode = NbSend(&SendNCB, 0, CallNCB.NCB_LSN, SendBuff,
strlen(SendBuff) + 1, 0L, WAIT_YES);

NbEvaluateRetcode(retcode);
free(SendBuff);

/* Bye-bye */
retcode = NbHangup(&HangupNCB, 0, CallNCB.NCB_LSN, 0L,
WAIT_YES);

if (retcode != 0x0a)
NbEvaluateRetcode(retcode);

retcode = NbDeleteName(&DeleteNameNCB, SourceUser, 0, 0L, 1);
NbEvaluateRetcode(retcode);
}


  3 Responses to “Category : Files from Magazines
Archive   : PCTJ8808.ZIP
Filename : NBSEND.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/