home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume23
/
mlpd
/
pfile.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-01-08
|
5KB
|
197 lines
/*
* Copyright (c) 1990 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Riverside.
*
* NOTE : That's Riverside. Not Berkeley, not Santa Cruz, not even
* Irvine. Riverside. Ri - ver - side.
*
* The name of the University may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* MLPD -- Multiple Line Printer Daemon (Version 1.3)
* SCCS keywords: @(#)pfile.c 1.3 12/1/90
*/
#include "config.h"
extern char *LP_SPOOL_DIRECTORY;
extern int TIMEOUT;
/*
// parse_arguments() -- Get printer information from argv.
//
// This function when called will return a structure containing
// the printers and their respective spooling directories in
// a structure.
//
// Arguments : printerstruct *printers (The printer structure)
*/
int parse_arguments(argc, argv, printers)
int argc;
char *argv[];
printerstruct *printers;
{
int i, origargc;
int no_of_printers, ncnt, can_exit;
char base[1024];
can_exit = FALSE;
i = 0;
origargc = argc;
if (argc < 5)
{
usage(argv[0], "Not enough arguments.\n");
}
while (--argc)
{
if (!strcmp("-t", argv[i]))
{
if ((atoi(argv[i+1])) && (i+1 != argc))
{
TIMEOUT = atoi(argv[i + 1]);
}
else
{
usage(argv[0], "Bad timeout value.\n");
}
}
if (!strcmp("-p", argv[i]))
{
can_exit = TRUE;
no_of_printers = 0;
for (ncnt = i + 1; ncnt < origargc; ++ncnt)
{
if (!strcmp(argv[ncnt], "-t"))
{
ncnt = origargc;
}
else if (ncnt == i + 1)
{
(void)strcpy(base, argv[ncnt]);
find_pr(argv[ncnt]);
}
else
{
add_pr(printers, argv[ncnt],
no_of_printers);
++no_of_printers;
}
}
if (no_of_printers < 2)
{
usage(argv[0], "Less than 2 printers specified.\n");
}
}
++i;
}
if (!TIMEOUT) TIMEOUT = 2;
if (can_exit == FALSE)
{
usage(argv[0], "Bad arguments used. Must use -p option.\n");
}
init_daemon(base);
return(no_of_printers);
}
usage(arg, str)
char *arg, *str;
{
(void)fprintf(stderr, "%sUsage : %s [-p <main printer> <pr1> <pr2> ...] [-t <timeout>]\n", str, arg);
exit(1);
}
/*
// add_pr() - add a printer to the list of printers.
//
// Arguments : printerstruct *printers -- the printer structure.
// char *str -- the name of the printer specified on command line.
// int nop -- the number of the printer item;
*/
int add_pr(printers, str, nop)
printerstruct *printers;
char *str;
int nop;
{
char line[BUFSIZ];
char *tmpbuf;
int status;
char *bp;
#ifndef lint
printers->directory[nop] = (char *) malloc(512);
printers->name[nop] = (char *) malloc(512);
tmpbuf = (char *)malloc(BUFSIZ);
bp = (char *)malloc(BUFSIZ/2);
#else
printers->directory[nop] = NULL;
printers->name[nop] = NULL;
tmpbuf = NULL;
bp = NULL;
#endif
if ((status = pgetent(line, str)) < 0)
{
bomb("pgetent");
}
if (status == 0)
{
(void)fprintf(stderr, "No such printer (%s) exists on this system.\n", str);
exit(1);
}
if ((tmpbuf = (char *)pgetstr((char *)"sd", (char *)&bp)) == NULL)
{
bomb("get_prdir_from_printcap:pgetstr");
}
(void)strcpy((char *)printers->directory[nop], (char *)tmpbuf);
(void)strcpy((char *)printers->name[nop], (char *)str);
}
/*
// find_pr() - find the base printer in the printcap file.
//
// This function will set the LP_SPOOL_DIRECTORY to the base printer
// spooling directory specified in /etc/printcap.
//
// Arguments : char *str -- the name of the printer specified on command line.
*/
find_pr(str)
char *str;
{
char line[BUFSIZ];
#ifndef lint
char *newdir;
char *bp;
#endif
int status;
#ifndef lint
newdir = (char *)malloc(BUFSIZ);
bp = (char *)malloc(BUFSIZ/2);
#endif
if ((status = pgetent(line, str)) < 0)
{
bomb("pgetent");
}
if (status == 0)
{
(void)fprintf(stderr, "No such printer (%s) exists on this system.\n", str);
exit(1);
}
if ((newdir = (char *)pgetstr("sd", &bp)) == NULL)
{
bomb("get_prdir_from_printcap:pgetstr");
}
LP_SPOOL_DIRECTORY = (char *)strdup(newdir);
}