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

📄 ckuus6.c

📁 linux终端仿真程序
💻 C
📖 第 1 页 / 共 5 页
字号:
	perror(s2);			/* to new (wierd) name. */	goto ddexit;    }#endif /* VMS */    if ((f = fopen(s2,"r")) == NULL) {	/* Reopen old file with wierd name */	dirline = 0;			/* (or in VMS, old version) */	perror(s2);	goto ddexit;    }    if ((f2 = fopen(s,"w")) == NULL) {	/* Create new file with old name */	perror(s);			/* (or in VMS, new version) */	goto ddexit;    }    printf("\nSaving old directory as %s.\nConverting %s...",s2,s);    fprintf(f2,"; %s - Kermit dialing directory\n", s);    fprintf(f2,"%-16s %-20s ; %5s %-6s ; %s\n",	       "; Name","Number","Speed","Parity","Comment"	       );    while (1) {	line[0] = NUL;			/* Read a line */	if (fgets(line,1023,f) == NULL)	  break;	if (!line[0]) {			/* Empty line */	    fprintf(f2,"\n");	    continue;	}	x = (int) strlen(line);		/* Strip line terminator, */	while (x-- > 0) {		/* if any. */	    if (line[x] <= SP)	      line[x] = NUL;	    else	      break;	}	xwords(line,5,info,1);		/* Parse it the old way */	for (x = 1; x < 6; x++)	  if (!info[x]) info[x] = "";	fprintf(f2,"%-16s %-20s ; %5s %-6s %s\n",	       info[1],info[2],info[3],info[4],info[5]	       );    }	    printf(" OK\n\n");    rc = 0;				/* Success */  ddexit:    if (f) fclose(f);    if (f2) fclose(f2);    if (line) free(line);    return(rc);}int					/* s = name to look up   */#ifdef CK_ANSIC				/* cx = index of command */ludial(char *s, int cx)			/* (DIAL, LOOKUP, etc)   */#elseludial(s, cx) char *s; int cx;#endif /* CK_ANSIC *//* ludial */ {    int dd, n1, n2, n3, i, j, t;	/* Workers */    int olddir, newdir, oldentry, newentry;    int pass = 0;    int oldflg = 0;    int ambiguous = 0;			/* Flag for lookup was ambiguous */    char *info[7];			/* Pointers to words from entry */    char *pp;				/* Pointer to element of array */    FILE * f;    char *line;				/* File input buffer *//* #define LUDEBUG */#ifdef LUDEBUGint zz = 1;#endif /* LUDEBUG */    if (!s || ndialdir < 1)		/* Validate arguments */      return(-1);    if ((n1 = (int) strlen(s)) < 1)	/* Length of string to look up */      return(-1);    if (!(line = malloc(1024)))		/* Allocate input buffer */      return(-1);#ifdef LUDEBUGif (zz) printf("LUDIAL 1 s[%s], n1=%d\n",s,n1);#endif /* LUDEBUG */    pass = 0;  lu_again:    f = NULL;				/* Dial directory file descriptor */    t = dncount = 0;			/* Dial-number match count */    dd = 0;				/* Directory counter */    olddir = 0;    newdir = 0;/*  We need to recognize both old- and new-style directories.  But we can't allow old-style and new-style entries in the same  directory because there is no way to tell for sure the difference between  and old-style entry like this:    foo  5551212  9600  and a new-style literal entry like this:    foo  555 9600  I.e. is the "9600" a speed, or part of the phone number?*/    while (1) {				/* We make one pass */	if (!f) {			/* Directory not open */            if (dd >= ndialdir)		/* No directories left? */	      break;			/* Done. */	    if ((f = fopen(dialdir[dd],"r")) == NULL) { /* Open it */		perror(dialdir[dd]);	/* Can't, print message saying why */		if (line) {		    free(line);		    line = NULL;		}		dd++;			/* Go on to next one, if any... */		continue;	    }	    dirline = 0;		/* Directory file line number */	    if (dialdpy && !pass)	      printf("Opening: %s...\n",dialdir[dd]);            dd++;	    if (!oldflg) olddir = 0;	    newdir = 0;	}	oldentry = 0;	newentry = 0;	line[0] = NUL;	if (getnct(line,1023,f,1) < 0) { /* Read a line */	    if (f) {			/* f can be clobbered! */		fclose(f);		/* Close the file */		f = NULL;		/* Indicate next one needs opening */		oldflg = 0;	    }	    continue;	}	if (!line[0])			/* Empty line */	  continue;#ifdef LUDEBUGif (zz) printf("LUDIAL 2 s[%s]\n",s);#endif /* LUDEBUG */	/* Make a copy and parse it the old way */	/* A copy is needed because xwords() pokes NULs into the string */	if (pp = malloc((int)strlen(line) + 1)) {	    strcpy(pp,line);	    xwords(pp,5,info,0);	/* Parse it the old way */#ifdef LUDEBUGif (zz) printf("LUDIAL 3 s[%s]\n",s);#endif /* LUDEBUG */	    if (!info[1])	      continue;	    if (*info[1] == ';') {	/* If full-line comment, */		newdir = 1;		/* (only new directories have them) */		continue;		/* keep reading. */	    }	    if (!info[2]) 	      continue;	    if (*info[2] == '+')	      newentry = 1;	    if (info[4]) {		if ((*info[4] == '=') ||		    !xxstrcmp(info[4],"none", 4) ||		    !xxstrcmp(info[4],"even", 4) ||		    !xxstrcmp(info[4],"space",5) ||		    !xxstrcmp(info[4],"mark", 4) ||		    !xxstrcmp(info[4],"odd",  3)		    )		  oldentry = 1;	    }	}	if (pp) {	    free(pp);	    pp = NULL;	}	/* Check consistency */	if ((oldentry || olddir) && (newentry || newdir)) {	    printf("\nERROR: You seem to have old- and new-format entries mixed in your\n");	    printf("dialing directory.  You'll have to edit it by hand to convert it to the\n");#ifndef NOHELP	    printf("new format.  Type HELP DIAL for further information.\n\n");#else	    printf("new format.\n\n");#endif /* NOHELP */	    if (line) {		free(line);		line = NULL;	    }	    return(-1);	}	if (!olddir && oldentry) {	    int convert = 0;	    olddir = 1;	    if (dialcvt == 2) {		/* 2 == ASK */		printf("\nWARNING: Old-style dialing directory detected:\n%s\n\n", line);		convert = getyesno("Shall I convert it for you? ");	    } else	      convert = dialcvt;	    if (convert) {		if (ddcvt(dialdir[dd-1],f,dd-1) < 0) {		    oldflg = 1;		    printf("  Sorry, can't convert.");		    printf("  Will ignore speed and parity fields, continuing...\n\n");		} else {		    olddir = newdir = 0;		}		dd--;		f = NULL;		continue;	    } else {		if (dialcvt == 2)		  printf("  OK, will ignore speed and parity fields, continuing...\n\n");		olddir = 1;	    }	}#ifdef LUDEBUGif (zz) printf("LUDIAL XX s[%s], n1=%d\n",s,n1);#endif /* LUDEBUG */	/* Now parse again for real */	if (oldentry)			/* Parse it the old way */	  xwords(line,5,info,0);	else				/* Parse it the new way */	  xwords(line,2,info,1);#ifdef LUDEBUGif (zz) printf("LUDIAL YY s[%s], n1=%d\n",s,n1);if (zz) printf("%s [%s]\n",info[1],info[2]);#endif /* LUDEBUG */	if (info[1]) {			/* First word is entry name */	    if ((n3 = (int) strlen(info[1])) < 1) /* Its length */	      continue;			/* If no first word, keep reading. */	    if (n3 < n1)		/* Search name is longer */	      continue;			/* Can't possibly match */	    if (ambiguous && n3 != n1)	      continue;#ifdef LUDEBUGif (zz) printf("MATCHING: [%s] [%s], n1=%d\n",s,info[1],n1);#endif /* LUDEBUG */	    if (xxstrcmp(s,info[1],n1)) /* Caseless string comparison */	      continue;#ifdef LUDEBUGif (zz) printf("MATCH OK: [%s] [%s], n1=%d\n",s,info[1],n1);#endif /* LUDEBUG */	    if (!info[2])		/* No phone number given */	      continue;	    if ((n2 = (int) strlen(info[2])) < 1) /* Length of phone number */	      continue;			/* Ignore empty phone numbers */	    /* Got one */	    if (!(pp = (char *)malloc(n2 + 1))) { /* Allocate storage for it */		printf("?internal error - ludial malloc 1\n");		if (line) {		    free(line);		    line = NULL;		}		dncount = 0;		return(-1);	    }	    strcpy(pp,info[2]);		/* Copy number into malloc'd storage */	    if (dncount > MAXDNUMS) {		printf("Warning: %d matches found, %d max\n",		       dncount,		       MAXDNUMS		       );		dncount = MAXDNUMS;		break;	    }	    dn_p[dncount++] = pp;	/* Add pointer to array. */	    if (dncount == 1) {		/* First one... */		if (d_name) free(d_name);		if (!(d_name = (char *)malloc(n3 + 1))) { /* Save its name */		    printf("?internal error - ludial malloc 2\n");		    if (line) {			free(line);			line = NULL;		    }		    dncount = 0;		    return(-1);		}		t = n3;			/* And its length */		strcpy(d_name,info[1]);	    } else {			/* Second or subsequent one */#ifdef LUDEBUG		if (zz)		  printf("d_name=[%s],info[1]=%s,t=[%d]\n",d_name,info[1],t);#endif /* LUDEBUG */		if ((int) strlen(info[1]) == t) /* Lengths compare */		  if (!xxstrcmp(d_name,info[1],t)) /* Caseless compare OK */		    continue;		/* Name given by user matches entries with different names */		if (ambiguous)		/* Been here before */		  break;		ambiguous = 1;		/* Now an exact match is required */		for (j = 0; j < dncount; j++) { /* Clean out previous list */		    if (dn_p[j]) {			free(dn_p[j]);			dn_p[j] = NULL;		    }		}		pass++;			/* Second pass... */		goto lu_again;		/* Do it all over again. */	    }	}    }    if (line) free(line);    if (dncount == 0 && ambiguous) {	printf(" Lookup: \"%s\" - ambiguous%s\n",	       s,	       cx == XXLOOK ? "" : " - dialing skipped"	       );	return(-2);    }    return(dncount);}static char *dscopy = NULL;intdodial(cx) int cx; {			/* DIAL or REDIAL */    int i = 0, x = 0;			/* Workers */    int sparity = -1;			/* For saving global parity value */    int partial  = 0;			/* For partial dial */    int previous = 0;    int len = 0;    int literal = 0;    char *p = NULL, *s3 = NULL, **s2 = NULL;    int j = 0, t = 0, n = 0;    if (cx == XXPDIA) {			/* Shortcut... */	cx = XXDIAL;	partial = 1;    }    previous = dialsta;			/* Status of previous call, if any */    if (cx != XXLOOK) {	if (mdmtyp < 1) {	    if (network)	      printf("Please SET HOST first, and then SET MODEM\n");	    else	      printf("Sorry, you must SET MODEM first\n");	    dialsta = DIA_NOMO;	    return(0);	}	if (!local) {	    printf("Sorry, you must SET LINE or SET HOST first\n");	    dialsta = DIA_NOLI;	    return(0);	}	if (!network &&	    (speed < 0L)#ifdef UNIX	    && (strcmp(ttname,"/dev/null"))#else#ifdef OSK	    && (strcmp(ttname,"/nil"))#endif /* OSK */#endif /* UNIX */	    ) {	    printf("Sorry, you must SET SPEED first\n");	    dialsta = DIA_NOSP;	    return(0);	}    }    s = NULL;				/* Initialize user's dial string */    if (cx == XXRED) {			/* REDIAL or... */	if ((y = cmcfm()) < 0)	  return(y);    } else if (cx == XXANSW) {		/* ANSWER or ... */	if ((y = cmnum("timeout (seconds)","0",10,&x,xxstring)) < 0)	  return(y);	dialatmo = x;	if ((y = cmcfm()) < 0)	  return(y);    } else {				/* DIAL or LOOKUP */	if (ndialdir > 0)	  s3 = "Number to dial or entry from dial directory";	else	  s3 = "Number to dial";	if ((x = cmtxt(s3, dialnum ? dialnum : "",&s,xxstring)) < 0)	  return(x);	if (s) {	    len = (int) strlen(s);	    if (len > 1) {		/* Strip outer braces if given */		if (*s == '{') {		    if (s[len-1] == '}') {			s[len-1] = NUL;			s++;			len -= 2;		    }		}	    }	}		    }    if (cx != XXANSW) {	for (j = 0; j < MAXDNUMS; j++) { /* Initialize dial-number list */	    if (!dialnum) {		/* First time dialing */		dn_p[j] = NULL;		/* initialize all pointers. */		dn_p2[j] = NULL;	    } else if (dn_p[j]) {	/* Not the first time, */		free(dn_p[j]);		/* free previous, if any, */		dn_p[j] = NULL;		/* then set to NULL. */		if (dn_p2[j])		  free(dn_p2[j]);		dn_p2[j] = NULL;	    } else break;		/* Already NULL */	}	if (len == 0)	  s = NULL;	if (!s)	  s = dialnum;	if (!s) {	    if (cx == XXLOOK)	      printf("?Lookup what?\n");	    else	      printf("%s\n", (cx == XXRED) ?		   "?No DIAL command given yet" :		   "?You must specify a number to dial"		   );	    return(-9);	}    /* Now we have the "raw" dial or lookup string and s is not NULL */	makestr(&dscopy,s);		/* Put it in a safe place */	s = dscopy;	n = 0;	debug(F111,"dodial",s,ndialdir);	if (isalpha(*s)) {	    if (ndialdir > 0) {		/* Do we have a dialing directory? */		n = ludial(s,cx);	/* Look up what the user typed */		if (n == 0)		  printf(" Lookup: \"%s\" - not found%s\n",			 s,			 cx == XXLOOK ? "" : " - dialing as given\n"			 );	    }	    debug(F101,"dodial",s,n);	    if (n < 0 && cx != XXLOOK) { /* Error out if they wanted to dial */		if (n == -1)		/* -2 means ludial already gave msg */		  printf(" Lookup: fatal error - dialing skipped\n");		dialsta = DIA_DIR;		return(-9);	    }    	} else {			/* "=" forces no lookup. */	    n = 0;	    if (*s == '=') {		/* If number starts with = sign */		s++;			/* strip it */		literal = 1;		/* remember this */		while (*s == SP) s++;	/* and then also any leading spaces */	    }	    if (ndialdir > 0)	      printf(" Lookup: skipped\n");	}	/* Save DIAL or successful LOOKUP string for future DIAL or REDIAL */	/* But don't save pieces of partial dial ... */	if ((cx == XXDIAL && partial == 0 && previous != DIA_PART) ||	    (cx == XXLOOK && n > 0)) {	    makestr(&dialnum,dscopy);	}	if (n > 0) {	    if (!quiet && !backgrd && dialdpy) {		if (!strcmp(d_name,s))		  printf(" Lookup: \"%s\" - exact match\n",s);

⌨️ 快捷键说明

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