⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 操作系统SunOS 4.1.3版本的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*	@(#)main.c 1.1 92/07/30 SMI 	*//*	Copyright (c) 1984 AT&T	*//*	  All Rights Reserved  	*//*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*//*	The copyright notice above does not evidence any   	*//*	actual or intended publication of such source code.	*/#ident	"@(#)nlsadmin:main.c	1.18.2.1"#include "sys/types.h"#include "sys/param.h"#include "tiuser.h"#include "sys/stat.h"#include "dirent.h"#include "stdio.h"#include "ctype.h"#include "signal.h"#include "string.h"#include "fcntl.h"#include "nlsadmin.h"#include "errno.h"#include "pwd.h"#include "grp.h"char    *arg0;int Errno;char serv_str[16];char id_str[MAXPATHLEN];char res_str[MAXPATHLEN];char flag_str[10];char mod_str[MAXPATHLEN];char path_str[MAXPATHLEN];char cmnt_str[MAXPATHLEN];char homedir[MAXPATHLEN];char tempfile[MAXPATHLEN];#ifdef S4static char *tempname = "/usr/tmp/.nlstmp";#elsestatic char *tempname = "/.nlstmp";#endifFILE *tfp;int nlsuid, nlsgid;int uid;char *Netspec;extern char *erropen;extern char *errclose;extern char *errmalloc;extern char *errchown;extern char *errscode;extern char *errperm;extern char *errdbf;extern char *errsvc;extern char *errarg;extern char *init[];extern char *malloc();main(argc, argv)int   argc;char *argv[];{	register int     flag = 0;	register int     c;	register char    *net_spec = NULL;	register char    *serv_code = NULL;	int     qflag = 0;	int     nflag = 0;	int     errflg = 0;	int     adrflg = 0;	int	mflag = 0;	char    *laddr = NULL;	char    *taddr = NULL;	char	*id = NULL;	char	*res = NULL;	char    *pathname = NULL;	char    *command = NULL;	char    *comment = NULL;	char	*modules = NULL;	struct passwd *pwdp;	struct group *grpp;	extern struct passwd *getpwnam();	extern struct group *getgrnam();	extern void exit(), endpwent(), endgrent();#ifdef	S4	char	*starname = NULL;	char	*pstarname = NULL;	extern char *nlsname();	extern int _nlslog;#endif	arg0 = argv[0];	Errno = NLSOK;	if (argc == 1)		usage();	uid = getuid();	if (!(grpp = getgrnam(LSGRPNAME)))  {	/* get group entry	*/		fprintf(stderr, "%s: no group entry for %s?\n",arg0,LSGRPNAME);		exit(NLSNOGRP);	}	nlsgid = grpp->gr_gid;	endgrent();	/* get password entry for name LSUIDNAME.  homedir = LSUIDNAME's home */	if (!(pwdp = getpwnam(LSUIDNAME)) || !(strlen(pwdp->pw_dir)))  {		fprintf(stderr,"%s: could not find correct password entry for %s\n",arg0,LSUIDNAME);		exit(NLSNOPASS);	}	nlsuid = pwdp->pw_uid;	strcpy(homedir, HOMEDIR);	endpwent();	umask(022);	/*	 *      Process arguments.	 */#ifndef	S4	while ((c = getopt(argc, argv, "a:c:d:e:ikl:mnp:qr:st:vw:xy:z:")) != EOF) {#else	while ((c = getopt(argc, argv, "a:c:d:e:ikl:mnqr:st:vw:xy:z:S:")) != EOF) {#endif		switch (c) {			case 'a':				flag |= ADD;				if (*optarg != '-')					serv_code = optarg;				break;			case 'c':				flag |= ADD;				if (*optarg != '-')					command = optarg;				break;			case 'd':				flag |= DISBL;				if (*optarg != '-')					serv_code = optarg;				break;			case 'e':				flag |= ENABL;				if (*optarg != '-')					serv_code = optarg;				break;			case 'i':				flag |= INIT;				break;			case 'k':				flag |= KILL;				break;			case 'l':				flag |= ADDR;				adrflg |= LISTEN;				if (*optarg != '-')					laddr = optarg;				else if (strlen(optarg) != 1)					errflg = 1;				break;			case 'm':				mflag = 1;				break;			case 'n':				nflag = 1;				break;#ifndef	S4			case 'p':				flag |= ADD;				if (*optarg != '-')					modules = optarg;				break;#endif			case 'q':				qflag = 1;				break;			case 'r':				flag |= REM;				if (*optarg != '-')					serv_code = optarg;				break;			case 's':				flag |= START;				break;			case 't':				flag |= ADDR;				adrflg |= TTY;				if (*optarg != '-')					taddr = optarg;				else if (strlen(optarg) != 1)					errflg = 1;				break;			case 'v':				flag |= VERBOSE;				break;			case 'w':				flag |= ADD;				if (*optarg != '-')					id = optarg;				break;			case 'x':				flag |= ALL;				break;			case 'y':				flag |= ADD;				if (*optarg != '-')					comment = optarg;				break;			case 'z':				flag |= ZCODE;				if (*optarg != '-')					serv_code = optarg;				break;#ifdef S4			case 'S':				flag |= STARLAN;				if (*optarg != '-')					starname = optarg;				break;#endif			case '?':				errflg = 1;		}	}	if (errflg)		usage();	if (optind < argc)		Netspec = net_spec = argv[optind++];	if (optind < argc)		usage();	/*	 * validate service code, if given.	 * code follows algorithm in listen:lsdbf.c:get_dbf().	 */	if (serv_code) {		c = strlen(serv_code);		if ((c == 0) || (c >= SVC_CODE_SZ))			error(errsvc, NLSSERV);		if ((c < 5) && isnum(serv_code)) {			c = atoi(serv_code);			if (c == 0)				error(errsvc, NLSSERV);			if (flag == ADD) {				if (mflag) {					if (c > N_DBF_ADMIN)						error(errsvc, NLSSERV);				} else {					if (c < MIN_REG_CODE)						error(errsvc, NLSSERV);				}			}		} else {			if ((flag == ADD) && mflag)				error(errsvc, NLSSERV);		}	}	if (!net_spec) {		if (flag != ALL)			usage();	} else {		if (!ok_netspec(net_spec))			usage();		if ((pathname = (char *)malloc(strlen(homedir)+strlen(net_spec)+strlen(DBFILE)+3)) == NULL)			error(errmalloc, NLSMEM);		sprintf(pathname, "%s/%s/%s", homedir, net_spec, DBFILE);	}	if (net_spec) {#ifdef S4		(void) strcpy(tempfile, tempname);#else		(void) strcpy(tempfile, homedir);		(void) strcat(tempfile, "/");		(void) strcat(tempfile, net_spec);		(void) strcat(tempfile, tempname);#endif	}	if ((flag != ALL) && (flag != INIT) && check_version(pathname))		error("database file is not current version, please convert", NLSVER);	switch (flag) {		case ALL:			if (mflag || qflag || nflag || adrflg || net_spec)				usage();			print_all(homedir);			exit(Errno);		case INIT:			if (mflag || qflag || nflag || adrflg || !net_spec)				usage();			if (uid != 0)				error(errperm, NLSPERM);			if (access(pathname, 0) < 0) {/*				if (strchr(net_spec, '\n'))					error(errarg, NLSCMD);*/				if (!make_db(pathname, net_spec))					error("error in making database file", Errno);			} else {				error("net_spec already initialized",NLSINIT);			}			exit(NLSOK);		case ADD:			if (qflag || nflag || adrflg || !net_spec)				usage();			if (!serv_code || !command || !comment)				usage();			if (uid != 0)				error(errperm, NLSPERM);/*			if (strchr(serv_code, '\n') || strchr(modules, '\n') ||			    strchr(command, '\n') || strchr(comment, '\n') ||			    strchr(id, '\n') || strchr(res, '\n'))				error(errarg, NLSCMD);*/			if (!add_nls(pathname,serv_code,id,res,modules,command,comment,mflag))				error("could not add service", Errno);			break;		case REM:			if (mflag || qflag || nflag || adrflg || !net_spec)				usage();			if (!serv_code)				usage();			if (uid != 0)				error(errperm, NLSPERM);			if (!rem_nls(pathname, serv_code))				error("could not remove service",Errno);			break;		case ENABL:			if (mflag || qflag || nflag || adrflg || !net_spec)				usage();			if (!serv_code)				usage();			if (uid != 0)				error(errperm, NLSPERM);			if (!enable_nls(pathname, serv_code))				error("could not enable service",Errno);			break;		case DISBL:			if (mflag || qflag || nflag || adrflg || !net_spec)				usage();			if (!serv_code)				usage();			if (uid != 0)				error(errperm, NLSPERM);			if (!disable_nls(pathname, serv_code))				error("could not disable service",Errno);			break;		case START:			if (mflag || qflag || adrflg || !net_spec)				usage();			if (uid != 0)				error(errperm, NLSPERM);			start_nls(net_spec, nflag);			/* will exit by itself */			break;		case KILL:			if (mflag || qflag || nflag || adrflg || !net_spec)				usage();			if (uid != 0)				error(errperm, NLSPERM);			if (!kill_nls(net_spec))				exit(Errno);			exit(NLSOK);		case NONE:			if (mflag || nflag || adrflg || !net_spec)				usage();			if (qflag)				exit(!isactive(net_spec));			else				printf("%s\t%s\n",net_spec,isactive(net_spec)?"ACTIVE":"INACTIVE");			exit(NLSOK);		case VERBOSE:			if (mflag || qflag || nflag || adrflg || !net_spec)				usage();			print_spec(pathname);			exit(NLSOK);		case ADDR:			if (mflag || qflag || nflag || !net_spec)				usage();			if ((laddr || taddr) && (uid != 0))				error(errperm, NLSPERM);/*			if (strchr(laddr, '\n') || strchr(taddr, '\n'))				error(errarg, NLSCMD);*/			if (!chg_addr(net_spec, laddr, taddr, adrflg))				error("error in accessing address file",Errno);			exit(NLSOK);#ifdef S4		case STARLAN:			if (mflag || qflag || nflag || adrflg || !net_spec)				usage();			if (!starname)				usage();			if (uid != 0)				error(errperm, NLSPERM);			/*			 * Nlsname returns null if pstarname will be longer 			 * than NAMEBUFSZ (currently 64)			 */			_nlslog = 1;	/* allows nlsname to use stderr */			if (!(pstarname = nlsname(starname)))				error("error permuting name", NLSADRF);			if (!chg_addr(net_spec, pstarname, starname, LISTEN|TTY))				error("error in accessing address file", Errno);			exit(NLSOK);#endif		case ZCODE:			if (mflag || nflag || adrflg || !net_spec)				usage();			if (!serv_code)				usage();			print_serv(pathname, serv_code, qflag);			/* will exit by itself */			break;		default:			usage();	}	exit(Errno);	/* NOTREACHED */}#ifdef S4static char *usagemsg = "\%s: usage: %s -x  |  %s [ options ] net_spec\n\\twhere [ options ] are as follows:\n\n\status:		[-q]  |  [-v]  |\n\start/stop:	[-s -n]  |  [-k]  |\n\initialize:	[-i]  | \n\set addresses:	[ [-l address|-]  [-t address|-] ]  |\n\		[-S STARLAN_NETWORK_name]  |\n\query servers:	[[-q] -z service_code]  |\n\add server:	[ [-m] -a service_code -c \"command\" [-w id] -y \"comment\" ]  |\n\remove server:	[-r service_code]  |\n\enable/disable:	[-d service_code]  |  [-e service_code]\n\";#elsestatic char *usagemsg = "\%s: usage: %s -x   or   %s [ options ] net_spec\n\\twhere [ options ] are:\n\[-q]  |  [-v]  |  [-s]  |  [-k]  |  [-i]  | \n\[ [-l address|-]  [-t address|-] ]  |\n\[ [-m] -a svc_code -c \"cmd\" -y \"comment\"  [-p module_list] [-w id]]  |\n\[[-q] -z svc_code]  |  [-r svc_code]  |  [-d svc_code]  |  [-e svc_code]\n\";#endifusage(){	fprintf(stderr, usagemsg, arg0, arg0, arg0);	exit(NLSCMD);}error(s, ec)char *s;int ec;{	fprintf(stderr, "%s: %s\n", arg0, s);	exit(ec);}/* * add an entry to the database, res is reserved for future use, ignore for now */add_nls(fname, serv, id, res, mods, path, comm, flag)char *fname;char *serv;char *id;char *res;char *mods;register char *path;char *comm;int flag;{	register FILE *fp = NULL;#ifndef S4	struct stat sbuf;#endif	for (; *path ; path++)		/* clean up beginning white space */		if (!isspace(*path))			break;	if ((fp = fopen(fname, "r")) == NULL) {		fprintf(stderr, erropen, arg0, Netspec);		Errno = NLSOPEN;		return(0);	}	if (strlen(serv) > (sizeof(serv_str)-1))		error("service code too long", NLSSERV);	if (find_serv(fp, serv) != -1) {		fprintf(stderr,"%s: service code %s already exists in %s\n",arg0,serv,fname);		Errno = NLSEXIST;		return(0);	}	(void) fclose(fp);	if ((fp = fopen(fname, "a")) == NULL) {		fprintf(stderr, erropen, arg0, Netspec);		Errno = NLSOPEN;		return(0);	}	if (!id)		id = DEFAULTID;	if (mods)		fprintf(fp,"%s:%s:%s:reserved:NULL,%s,:%s\t#%s\n",serv,flag?"na":"n",id,mods,path,comm);	else		fprintf(fp,"%s:%s:%s:reserved:NULL,:%s\t#%s\n",serv,flag?"na":"n",id,path,comm);	if (fclose(fp) != 0) {		fprintf(stderr,errclose,arg0,fname,errno);		Errno = NLSCLOSE;		return(0);	}#ifndef S4	(void) strtok(path, DBFWHITESP);	if (access(path, 0) == 0) {		(void) stat(path, &sbuf);		if (!(sbuf.st_mode & 0111))			fprintf(stderr,"%s: warning - %s not executable\n", arg0, path);		if (!(sbuf.st_mode & S_IFREG))			fprintf(stderr, "%s: warning - %s not a regular file\n", arg0, path);	} else {		fprintf(stderr,"%s: warning - %s not found\n", arg0, path);	}#endif	return(1);}rem_nls(fname, serv_code)	       /* remove an entry from the database */char *fname;char *serv_code;{	register FILE *fp;	int n;	if ((fp = fopen(fname, "r")) == NULL) {		fprintf(stderr, erropen, arg0, Netspec);		Errno = NLSOPEN;		return(0);	}	if ((n = find_serv(fp, serv_code)) == -1) {		fprintf(stderr,errscode, arg0, serv_code, fname);		Errno = NLSNOCODE;		return(0);	}	if (!open_temp())		return(0);	if (n != 0)		copy_file(fp, 0, n-1);	copy_file(fp, n+1, -1);	if (!close_temp(fname))		return(0);	return(1);}enable_nls(fname, serv_code)	    /* enable a specific service code */char *fname;char *serv_code;{	register FILE *fp;	register char *to, *from;	register char *limit;	char *bp, *copy;	int n;	if ((fp = fopen(fname, "r")) == NULL) {		fprintf(stderr, erropen, arg0, Netspec);		Errno = NLSOPEN;		return(0);	}	if ((n = find_serv(fp, serv_code)) == -1) {		fprintf(stderr,errscode,arg0,serv_code,fname);		Errno = NLSNOCODE;		return(0);	}	if (!open_temp())		return(0);	if (n != 0)		copy_file(fp, 0, n-1);	else		fseek(fp, 0L, 0);	if ((bp = (char *)malloc(DBFLINESZ)) == NULL)		error(errmalloc, NLSMEM);	if ((copy = (char *)malloc(DBFLINESZ)) == NULL)		error(errmalloc, NLSMEM);	if (!fgets(bp,DBFLINESZ,fp)) {		(void) unlink(tempfile);		error(errdbf, NLSRDFIL);	}	limit = strchr(bp, (int)':');	for (to = copy, from = bp; from < limit; )		*to++ = *from++;	*to++ = *from++;	/* copy ":" */	limit = strchr(from, (int)':');	for ( ; from < limit ; ) {		if (*from == 'x' || *from == 'X') {			from++;			continue;		}		*to++ = *from++;	}	for ( ; *from ; )		*to++ = *from++;	*to = '\0';	fprintf(tfp, "%s", copy);	free(bp);	free(copy);	copy_file(fp, n+1, -1);	(void) fclose(fp);	if (!close_temp(fname))		return(0);	return(1);}disable_nls(fname, serv_code)	   /* disable a specific service code */char *fname;char *serv_code;{	register FILE *fp;	register char *to, *from;	register char *limit;	char *bp, *copy;	int n;	if ((fp = fopen(fname, "r")) == NULL) {		fprintf(stderr, erropen, arg0, Netspec);		Errno = NLSOPEN;		return(0);	}	if ((n = find_serv(fp, serv_code)) == -1) {		fprintf(stderr,errscode,arg0,serv_code,fname);		Errno = NLSNOCODE;		return(0);	}	if (!open_temp())		return(0);	if (n != 0)		copy_file(fp, 0, n-1);	else		fseek(fp, 0L, 0);	if ((bp = (char *)malloc(DBFLINESZ)) == NULL)		error(errmalloc, NLSMEM);	if ((copy = (char *)malloc(DBFLINESZ)) == NULL)		error(errmalloc, NLSMEM);	if (!fgets(bp,DBFLINESZ,fp)) {		(void) unlink(tempfile);		error(errdbf, NLSRDFIL);	}	limit = strchr(bp, (int)':');	for (to = copy, from = bp; from < limit; )		*to++ = *from++;	*to++ = *from++;	/* copy ":" */	limit = strchr(from, (int)':');	for ( ; from < limit ; ) {		if (*from == 'x' || *from == 'X') {			from++;			continue;		}		*to++ = *from++;	}	*to++ = 'x';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -