📄 lprsetup.c
字号:
* find val ******************/ for (i = 0; tab[i].name; ++i) if (strcmp (tab[i].name, val) == 0) break; if (tab[i].name == 0) { printf("internal error: cannot find symbol %s in table\n", val); return (BAD); } /* * Next assignment is needed for the case when the parameter was not set * in the template. * It is set here because the parameter may be switched off again in * the call to UseDefault. */ tab[i].used = YES; (void) UseDefault (line, (tab[i].nvalue ? tab[i].nvalue : buf), i); /* * By ignoring the return value of UseDefault we may do a superfluous * copy if the default value was held in tab[i].nvalue. * The win is that we now have only one flow of control. */ if (validate(i, line) < 0) { return(BAD); } if (tab[i].nvalue != NULL) { free(tab[i].nvalue); tab[i].nvalue = NULL; } if ((tab[i].nvalue = (char *) malloc (strlen (line) + 1)) == NULL) { printf ("\nmalloc: no space for %s\n", tab[i].name); return (ERROR); } strcpy (tab[i].nvalue, line); /* This is so caller can examine the chosen value */ strcpy(buf, line); return(OK);}/********************************************** view current contents of printcap file**********************************************/ViewPrintcap (){ FILE *pfp; int cnt, done; char line[BUF_LINE]; GetRows (); /* determine current size of window or screen - DJG#12 */ if ((pfp = fopen (PRINTCAP, "r")) == NULL ) { badfile (PRINTCAP); } cnt = 0; done = FALSE; while (!done) { cnt ++; if (fgets (line, BUF_LINE, pfp) == NULL) { done = TRUE; } if (!done) { if (cnt <= rows) { printf ("%s", line); } else { printf ("\nPress 'RETURN' to continue or 'quit RETURN' to quit: "); switch (getcmd()) { case QUIT: case GOT_SYMBOL: done = TRUE; break; case NOREPLY: default: printf ("%s", line); cnt = 1; break; } } } }}/*********************************************** write new printcap entry to printcap file***********************************************/AddEntry (){ FILE *ofp, *lfp; /* output and log file pointers */ long timeval; /* time for log file */ char buf[LEN]; /* temp buffer */ /**************************************** * open output and log files ****************************************/ if ((ofp = fopen (PRINTCAP, "a")) == NULL) { badfile(PRINTCAP); } if ((lfp = fopen (LOGCAP, "a")) == NULL) { badfile(LOGCAP); } WriteComments (ofp); WriteEntry (ofp); /**************************************** * write time stamp and entry to log ****************************************/ timeval = time(0); strcpy (buf, "\nAdded "); strcat (buf, ctime (&timeval)); fputs (buf, lfp); WriteEntry (lfp); fclose (ofp); fclose (lfp); return (OK);}/**************************************** create special device, if necessary***************************************/AddDevice (){ struct passwd *passwd; /* password file entry */ char *device; /* parameter value ptr */ int mode; /* chmod mode */ int i; /* temp index */ /******************************* * get daemon id *******************************/ if ((passwd = getpwnam (DAEMON)) == 0) { printf ("\ngetpwnam: cannot get id for %s\n", DAEMON); perror (); leave (ERROR); } /************************************ * chown and chmod device to daemon * * **** removed for ULTRIX-32 **** ************************************/ /******************************* * create spooling directory *******************************/ MakeSpool (passwd); /******************************* * create accounting file *******************************/ MakeAcct (passwd); /******************************* * create errorlog file *******************************/ MakeErrorlog (passwd); /*********************************** * inform user of completion ***********************************/ DisableGetty(); setupdone();}/* * UnLinkSpool - optionally unlink old spool directory when modifying entry * * Description: * We use existing function UnLinkSpooler by faking up the entry * We prompt system manager who may want to keep old spool directory */static intUnLinkSpool(tabent, oldpath) struct table *tabent; char *oldpath;{ char *newpath; int used; int yn; printf("\nChanging spool directory\n"); printf("Do you want to delete the old spool directory? [n] "); yn = 'n'; if (YesNo (yn) == TRUE) { /* save current values */ newpath = tabent->nvalue; used = tabent->used; /* Fake up for deletion */ tabent->nvalue = oldpath; tabent->used = YES; /* Use existing function to unlink spool directory */ UnLinkSpooler(); /* uses value stored in tabent */ /* restore current values */ tabent->nvalue = newpath; tabent->used = used; }}/* * UnLinkFile - optionally unlink old account or log file * when modifying printcap entry * * Description: * We use existing function UnLinkSymFile by faking up the entry * We prompt system manager who may want to keep the old files * which may be shared with other queues. */static intUnLinkFile(tabent, oldpath) struct table *tabent; char *oldpath;{ char *newpath; int used; /* save current values */ newpath = tabent->nvalue; used = tabent->used; /* Fake up for deletion */ tabent->nvalue = oldpath; tabent->used = YES; /* Use existing function to unlink file */ UnLinkSymFile(tabent->name); /* restore current values */ tabent->nvalue = newpath; tabent->used = used;}/******************************* Modify selected entry* If the entry for sd, lf, or af is * changed delete the old files - DJG#6** The old logic for deciding whether to delete old files* was fairly broken.* Since the logic for dealing with modifying sd, lf and af* is the same for each I have removed the triplication and* provided an enumeration and table of appropriate functions* to do the deletion and creation - 03-Mar-91 Adrian Thoms********************************/ModifyEntry (){ struct passwd *passwd; /* passwd entry ptr */ int done; /* flag */ FILE *ifp, *ofp; /* input/ouput file pointers */ char keyname[LEN]; /* match name */ char buf[BUFLEN]; /* read/write buffer */ char oldvalue[MAXPATH]; /* old value of af, lf, or sd */ int i,j; /* temp index */ int old_used; /* were these previously in use */ int old_and_new_different; int yn; enum which_symbol_e { any_other_symbol, sd_symbol, lf_symbol, af_symbol } which_symbol; static struct delete_and_create_fns { int (*delete_fn)(), (*create_fn)(); } fn_tab[] = { NULL, NULL, UnLinkSpool, MakeSpool, UnLinkFile, MakeErrorlog, UnLinkFile, MakeAcct }; /******************************* * get daemon id *******************************/ if ((passwd = getpwnam (DAEMON)) == 0) { printf ("\ngetpwnam: cannot find %s uid in passwd file.\n", DAEMON); leave (ERROR); } /********************** * modify fields **********************/ CopyEntry (); /* affects longname */ for (;;) { done = FALSE; printf("\nEnter the name of the symbol you wish to change.\n"); printf("Enter 'p' to print the current values, 'l' to list\n"); printf("all printcap values or 'q' to quit.\n"); while (!done) { old_used = NO; old_and_new_different = 0; which_symbol = any_other_symbol; printf ("\nEnter symbol name: "); switch (getcmd ()) { case GOT_SYMBOL: if ((strcmp("sd", symbolname) == 0 && (which_symbol = sd_symbol)) || (strcmp("lf", symbolname) == 0 && (which_symbol = lf_symbol)) || (strcmp("af", symbolname) == 0 && (which_symbol = af_symbol))) { for (i = 0; tab[i].name != 0; i++) { if (strcmp (tab[i].name, symbolname) == 0) { if ((old_used = tab[i].used) == YES) { strcpy (oldvalue,(tab[i].nvalue ? tab[i].nvalue : tab[i].svalue)); } break; } } /* * No point in processing symbol if it wasn't found * in the table */ if (!tab[i].name) continue; } DoSymbol (); /**** NOTE: i still points to the correct table entry ****/ switch(which_symbol) { case sd_symbol: case lf_symbol: case af_symbol: if (old_used == YES && tab[i].used == YES) { old_and_new_different = strcmp(oldvalue, (tab[i].nvalue ? tab[i].nvalue : tab[i].svalue)); } if (old_used == YES && (tab[i].used == NO || old_and_new_different)) { fn_tab[(int)which_symbol].delete_fn(&tab[i], oldvalue); } if (!(old_used == YES && tab[i].used == YES && !old_and_new_different)) { /* * Always call create function unless there was no * change. * This is inefficient, but makes use of * warning message in MakeSpool if spool * directory switched off. */ fn_tab[(int)which_symbol].create_fn(passwd); } break; case any_other_symbol: default: break; } break; case HELP: PrintHelp (h_symsel); for (i = 0,j=0; tab[i].name != 0; ++i){ printf (" %s ", tab[i].name); if (j++ > 14){ printf ("\n"); j = 0; } } printf ("\n"); break; case NOREPLY: break; case PRINT: Print (USED); break; case LIST: Print (ALL); break; case QUIT: done = TRUE; break; default: printf ("\nInvalid choice, try again.\n"); break; } } if (Verified() == TRUE) break; else { printf("Do you wish to continue with this entry? [y] "); yn = 'y'; if (YesNo (yn) == FALSE) { return(QUIT); /* no, they wish to abort, although at this point, the return value (from here) is not checked. We just return early without actually do anything. */ } else done = FALSE; /* not done yet */ } } /************************** * save pname for match * and longname to rewrite * PNAME changed to PNUM **************************/ strcpy (keyname, pnum); /* can search for any name, like "lp2" or "two" */ strcpy (pname, longname); /* new pname contains the entire first line */ /****************************** * open original and copy *******************************/ if ((ifp = fopen (PRINTCAP, "r")) == NULL) { badfile(PRINTCAP); } if ((ofp = fopen (COPYCAP, "w")) == NULL) { badfile(COPYCAP); } /******************************************* * copy printcap to copy until entry DJG#11 ********************************************/ while (fgets (buf, BUFLEN, ifp) != 0) { if (CommentOrEmpty (buf)) fputs (buf, ofp); else { if (strpbrk (buf, ":|\\") != 0) { if (SynonymMatch (buf, keyname) == 0) { /* wrong entry so just copy */ fputs (buf, ofp); while (fgets (buf, BUFLEN, ifp) != 0) { fputs (buf, ofp); if (buf[strlen(buf) - 2] != '\\') break; } } else { /* right entry so replace old entry with modified one */ while (fgets (buf, BUFLEN, ifp) != 0) { if (buf[strlen(buf) - 2] != '\\') { WriteEntry (ofp); break; } } } } else /* not sure what it is, but we better copy it */ fputs (buf, ofp); } } fclose (ofp); fclose (ifp); /*************************** * mv new file to old file ***************************/ if (rename (COPYCAP, PRINTCAP) < 0) { printf ("\nCannot rename %s to %s (errno = %d).\n", COPYCAP, PRINTCAP, errno); /* don't know what best to do here...*/ } /*************************************** * inform user of completion ***************************************/ DisableGetty(); setupdone();}/*********************************** delete existing printcap entry**********************************/DeleteEntry (){ FILE *ifp, *ofp, *lfp; /* input/output file pointers */ char buf[LEN]; /* read/write buffer */ long timeval; /* time in seconds for logfile */ char tempfile[LEN]; /* file name buffer */ /********************************* * open original, copy, and log **********************************/ if ((ifp = fopen (PRINTCAP, "r")) == NULL) { badfile(PRINTCAP); } (void) sprintf (tempfile, "%s%d", COPYCAP, getpid()); if ((ofp = fopen (tempfile, "w")) == NULL) { printf("\nCannot open intermediate file: %s.\n", tempfile); perror (tempfile); leave (ERROR); } if ((lfp = fopen (LOGCAP, "a")) == NULL) { badfile(LOGCAP); } timeval = time(0); strcpy (buf, "\nDeleted "); strcat (buf, ctime(&timeval)); fputs (buf, lfp); /***************************************** * copy printcap to copy until next entry *****************************************/ while (fgets (buf, BUFLEN, ifp) != 0) { if (!findpname (pname, buf)) fputs (buf, ofp); else { fputs (buf, lfp); while ((fgets (buf, BUFLEN, ifp) != 0) && (buf[strlen(buf) - 2] != ':')) { fputs (buf, lfp); } /* write line with colon */ fputs (buf, lfp); } } if (rename (tempfile, PRINTCAP) < 0) { printf("\nCannot rename %s to %s (errno=%d).\n", tempfile, PRINTCAP, errno); } fclose (ofp); fclose (ifp); fclose (lfp); UnLinkSpooler(); /* Delete Spooling directory DJG#5 */ UnLinkSymFile("af"); /* Delete Accounting file DJG#10*/ UnLinkSymFile("lf"); /* Delete Error log file DJG#10*/ return(OK);}/************************ copy entry into tab************************/CopyEntry (){ char line[LEN]; /* read buffer */ char tmpline[LEN]; /* temporary buffer */ char *lineptr; /* read buffer ptr */ char *ptr; /* temp pointer */ int num; /* pgetnum return */ int i; /* temp index */ char s[ALPHANUMLEN]; char *p; int status; strcpy(s, pnum); status = pgetent(bp, s); if (status == -1) { badfile(PRINTCAP); } /*************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -