Files
ncpfs/contrib/testing/dsstream.c
ncpfs archive import 82706139bf Import ncpfs 2.2.1
2026-04-28 20:39:59 +02:00

292 lines
6.8 KiB
C

/*
dsstream.c - NWDSOpenStream() API demo
Copyright (C) 1999, 2000, 2002 Petr Vandrovec
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Revision history:
1.00 2002, January 20 Petr Vandrovec <vandrove@vc.cvut.cz>
Initial revision, copied from readattr.c.
*/
#ifdef N_PLAT_MSW4
#include <nwcalls.h>
#include <nwnet.h>
#include <nwclxcon.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "getopt.h"
#define _(X) X
typedef unsigned int u_int32_t;
typedef unsigned long NWObjectID;
typedef u_int32_t Time_T;
#else
#include <ncp/nwcalls.h>
#include <ncp/nwnet.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <wchar.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "private/libintl.h"
#define _(X) gettext(X)
#endif
static char *progname;
static void
usage(void)
{
fprintf(stderr, _("usage: %s [options]\n"), progname);
}
static void
help(void)
{
printf(_("\n"
"usage: %s [options]\n"), progname);
printf(_("\n"
"-h Print this help text\n"
"-v value Context DCK_FLAGS value (default 0)\n"
"-o object_name Name of object (default [Root])\n"
"-c context_name Name of current context (default OrgUnit.Org.Country)\n"
"-C confidence DCK_CONFIDENCE value (default 0)\n"
"-S server Server to start with (default CDROM)\n"
"-a attrname Stream name\n"
"-q flags Open flags\n"
"-w filename Write file to stream\n"
"\n"));
}
#ifdef N_PLAT_MSW4
char* strnwerror(int err) {
static char errc[200];
sprintf(errc, "error %u / %d / 0x%X", err, err, err);
return errc;
}
#endif
int main(int argc, char *argv[]) {
NWDSCCODE dserr;
NWDSContextHandle ctx;
NWCONN_HANDLE conn;
const char* objectname = "[Root]";
const char* context = "OrgUnit.Org.Country";
const char* server = "CDROM";
const char* streamname = "Login Script";
int writefile = -1;
int opt;
u_int32_t ctxflag = 0;
u_int32_t confidence = 0;
u_int32_t req = 0;
#ifndef N_PLAT_MSW4
setlocale(LC_ALL, "");
bindtextdomain(NCPFS_PACKAGE, LOCALEDIR);
textdomain(NCPFS_PACKAGE);
#endif
progname = argv[0];
NWCallsInit(NULL, NULL);
#ifndef N_PLAT_MSW4
NWDSInitRequester();
#endif
dserr = NWDSCreateContextHandle(&ctx);
if (dserr) {
fprintf(stderr, "NWDSCretateContextHandle failed with %s\n", strnwerror(dserr));
return 123;
}
while ((opt = getopt(argc, argv, "h?o:c:v:S:C:a:q:w:")) != EOF)
{
switch (opt)
{
case 'w':
writefile = open(optarg, O_RDONLY);
if (writefile == -1) {
fprintf(stderr, "Cannot open %s: %s\n", optarg, strerror(errno));
return 111;
}
break;
case 'a':
streamname = optarg;
break;
case 'q':
req = strtoul(optarg, NULL, 0);
break;
case 'C':
confidence = strtoul(optarg, NULL, 0);
break;
case 'o':
objectname = optarg;
break;
case 'c':
context = optarg;
break;
case 'v':
ctxflag = strtoul(optarg, NULL, 0);
break;
case 'h':
case '?':
help();
goto finished;
case 'S':
server = optarg;
break;
default:
usage();
goto finished;
}
}
ctxflag |= DCV_XLATE_STRINGS;
dserr = NWDSSetContext(ctx, DCK_FLAGS, &ctxflag);
if (dserr) {
fprintf(stderr, "NWDSSetContext(DCK_FLAGS) failed: %s\n",
strnwerror(dserr));
return 123;
}
dserr = NWDSSetContext(ctx, DCK_NAME_CONTEXT, context);
if (dserr) {
fprintf(stderr, "NWDSSetContext(DCK_NAME_CONTEXT) failed: %s\n",
strnwerror(dserr));
return 122;
}
dserr = NWDSSetContext(ctx, DCK_CONFIDENCE, &confidence);
if (dserr) {
fprintf(stderr, "NWDSSetContext(DCK_CONFIDENCE) failed: %s\n",
strnwerror(dserr));
return 122;
}
#ifndef N_PLAT_MSW4
{
static const u_int32_t add[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
dserr = NWDSSetTransport(ctx, 16, add);
if (dserr) {
fprintf(stderr, "NWDSSetTransport failed: %s\n",
strnwerror(dserr));
return 124;
}
}
#endif
#ifdef N_PLAT_MSW4
dserr = NWDSOpenConnToNDSServer(ctx, server, &conn);
#else
if (server[0] == '/') {
dserr = ncp_open_mount(server, &conn);
if (dserr) {
fprintf(stderr, "ncp_open_mount failed with %s\n",
strnwerror(dserr));
return 111;
}
} else {
struct ncp_conn_spec connsp;
long err;
memset(&connsp, 0, sizeof(connsp));
strcpy(connsp.server, server);
conn = ncp_open(&connsp, &err);
if (!conn) {
fprintf(stderr, "ncp_open failed with %s\n",
strnwerror(err));
return 111;
}
}
dserr = NWDSAddConnection(ctx, conn);
#endif
if (dserr) {
fprintf(stderr, "Cannot bind connection to context: %s\n",
strnwerror(dserr));
}
{
nuint8 hndl[6];
NWCONN_HANDLE fileConn;
ncp_off64_t fileSize;
dserr = __NWDSOpenStream(ctx, objectname, streamname, req, &fileConn, hndl, &fileSize);
if (dserr) {
fprintf(stderr, "Read failed with %s\n",
strnwerror(dserr));
} else {
char buff[1000];
off_t off = 0;
int rd;
printf("File handle: %02X%02X:%02X%02X%02X%02X, size=%llu\n",
hndl[0], hndl[1], hndl[2], hndl[3], hndl[4], hndl[5], fileSize);
if (writefile == -1) {
do {
rd = ncp_read(fileConn, hndl, off, sizeof(buff), buff);
if (rd > 0) {
long l;
l = write(1, buff, rd);
if (l < 0) {
fprintf(stderr, "Cannot print stream content: %s\n", strerror(errno));
} else if (l != rd) {
fprintf(stderr, "Printing resulted in short write\n");
}
off += rd;
} else if (rd == 0) {
printf("\nEnd of stream\n");
} else {
fprintf(stderr, "Read error: %s\n", strnwerror(rd));
}
} while (rd > 0);
} else {
do {
rd = read(writefile, buff, sizeof(buff));
if (rd > 0) {
long l;
l = ncp_write(fileConn, hndl, off, rd, buff);
if (l < 0) {
fprintf(stderr, "Write to stream failed: %s\n", strnwerror(l));
} else if (l != rd) {
fprintf(stderr, "Write to stream resulted in short write\n");
}
off += rd;
}
} while (rd > 0);
}
ncp_close_file(fileConn, hndl);
NWCCCloseConn(fileConn);
}
};
NWCCCloseConn(conn);
dserr = NWDSFreeContext(ctx);
if (dserr) {
fprintf(stderr, "NWDSFreeContext failed with %s\n", strnwerror(dserr));
return 121;
}
finished:
return 0;
}