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

📄 cmds.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
	(void) close (0);			  /* close stdin */	(void) close (pipefd[1]);	(void) dup (pipefd[0]);		  /* connect to pipe input */	(void) signal (SIGINT, SIG_DFL);	  /* reset */	(void) execl ("/bin/sh", "sh", "-c", fname, 0);	exit (-127);    }    else				  /* else parent */    {	*rpid = pid;	if ((f = fdopen (pipefd[1], "w")) == (FILE *)0)	{	    (void) kill (pid, -9);	    error ("Can't fdopen output");	    (void) close (pipefd[1]);	    *rpid = 0;	    return(0);	}    }#endif /* VMS */    return(f);}voidcloseout(f, pid)FILE *f;int pid;{    int temp;    (void) fclose (f);    if (pid) {         while (pid != wait(&temp)) /**/;	 (void) printf("Press RETURN to continue ");	 (void) fflush(stdout);	 (void) nmgetch();	 goraw();    }}voidcopyent(n,p,dr,dc)	    register struct ent *n, *p;	    int dr, dc;{    if(!n||!p){error("internal error");return;}    n -> v = p -> v;    n -> flags = p -> flags;    n -> expr = copye (p -> expr, dr, dc);    n -> label = (char *)0;    if (p -> label) {	n -> label = (char *)		xmalloc  ((unsigned) (strlen (p -> label) + 1));	(void) strcpy (n -> label, p -> label);    }}voidwrite_fd (f, r0, c0, rn, cn)register FILE *f;int r0, c0, rn, cn;{    register struct ent **pp;    register r, c;    (void) fprintf (f, "# This data file was generated by the Spreadsheet ");    (void) fprintf (f, "Calculator.\n");    (void) fprintf (f, "# You almost certainly shouldn't edit it.\n\n");    print_options(f);    for (c=0; c<maxcols; c++)	if (fwidth[c] != DEFWIDTH || precision[c] != DEFPREC)	    (void) fprintf (f, "format %s %d %d\n",coltoa(c),fwidth[c],precision[c]);    for (c=c0; c<cn; c++) {        if (col_hidden[c]) {            (void) fprintf(f, "hide %s\n", coltoa(c));        }    }    for (r=r0; r<=rn; r++) {	if (row_hidden[r]) {	    (void) fprintf(f, "hide %d\n", r);	}    }    write_range(f);    if (mdir) 	    (void) fprintf(f, "mdir \"%s\"\n", mdir);    for (r=r0; r<=rn; r++) {	pp = ATBL(tbl, r, c0);	for (c=c0; c<=cn; c++, pp++)	    if (*pp) {		if ((*pp)->label) {		    edits(r,c);		    (void) fprintf(f, "%s\n",line);		}		if ((*pp)->flags&is_valid) {		    editv (r, c);		    (void) fprintf (f, "%s\n",line);		}	    }    }}intwritefile (fname, r0, c0, rn, cn)char *fname;int r0, c0, rn, cn;{    register FILE *f;    char save[PATHLEN];    int pid;#ifndef VMS    if (Crypt) {	return (cwritefile(fname, r0, c0, rn, cn));    }#endif /* VMS */    if (*fname == '\0') fname = curfile;    (void) strcpy(save,fname);    if ((f= openout(fname, &pid)) == (FILE *)0)    {	error ("Can't create file \"%s\"", fname);	return (-1);    }    write_fd(f, r0, c0, rn, cn);        closeout(f, pid);    if (!pid) {        (void) strcpy(curfile, save);        modflg = 0;        error("File \"%s\" written.",curfile);    }    return (0);}voidreadfile (fname,eraseflg)char *fname;int eraseflg;{    register FILE *f;    char save[PATHLEN];    if (*fname == '*' && mdir) {        (void) strcpy(save, mdir);       *fname = '/';       (void) strcat(save, fname);    } else {        if (*fname == '\0')	    fname = curfile;        (void) strcpy(save,fname);    }#ifndef VMS    if (Crypt)  {	creadfile(save, eraseflg);	return;    }#endif /* VMS */    if (eraseflg && strcmp(fname,curfile) && modcheck(" first")) return;    if ((f = fopen(findhome(save), "r")) == (FILE *)0)    {	error ("Can't read file \"%s\"", save);	return;    }    if (eraseflg) erasedb ();    loading++;    while (fgets(line,sizeof line,f)) {	linelim = 0;	if (line[0] != '#') (void) yyparse ();    }    --loading;    (void) fclose (f);    linelim = -1;    modflg++;    if (eraseflg) {	(void) strcpy(curfile,save);	modflg = 0;    }    EvalAll();}voiderasedb (){    register r, c;    for (c = 0; c<=maxcol; c++) {	fwidth[c] = DEFWIDTH;	precision[c] = DEFPREC;    }    for (r = 0; r<=maxrow; r++) {	register struct ent **pp = ATBL(tbl, r, 0);	for (c=0; c++<=maxcol; pp++)	    if (*pp) {		if ((*pp)->expr) efree (*pp, (*pp) -> expr);		if ((*pp)->label) xfree ((char *)((*pp) -> label));		xfree ((char *)(*pp));		*pp = (struct ent *)0;	    }    }    maxrow = 0;    maxcol = 0;    clean_range();    FullUpdate++;}voidbackcol(arg)	int arg;{    while (--arg>=0) {	if (curcol)	    curcol--;	else	    {error ("At column A"); break;}	while(col_hidden[curcol] && curcol)	    curcol--;    }}voidforwcol(arg)	int arg;{    while (--arg>=0) {	if (curcol < maxcols - 1)	    curcol++;	else	if (!growtbl(GROWCOL, 0, arg))	/* get as much as needed */		break;	while(col_hidden[curcol]&&(curcol<maxcols-1))	    curcol++;    }}voidforwrow(arg)	int arg;{    while (--arg>=0) {	if (currow < maxrows - 1)	    currow++;	else	if (!growtbl(GROWROW, arg, 0))	/* get as much as needed */		break;	while (row_hidden[currow]&&(currow<maxrows-1))	    currow++;    }}voidbackrow(arg)	int arg;{    while (--arg>=0) {	if (currow)	    currow--;	else	    {error ("At row zero"); break;}	while (row_hidden[currow] && currow)	    currow--;    }}/* * Show a cell's label string or expression value.  May overwrite value if * there is one already displayed in the cell.  Created from old code in * update(), copied with minimal changes. */voidshowstring (string, leftflush, hasvalue, row, col, nextcolp, mxcol, fieldlenp, r, c)    char *string;	/* to display */    int leftflush;	/* or rightflush */    int hasvalue;	/* is there a numeric value? */    int row, col;	/* spreadsheet location */    int *nextcolp;	/* value returned through it */    int mxcol;		/* last column displayed? */    int *fieldlenp;	/* value returned through it */    int r, c;		/* screen row and column */{    register int nextcol  = *nextcolp;    register int fieldlen = *fieldlenp;    char field[FBUFLEN];    int  slen;    char *start, *last;    register char *fp;    struct ent *nc;    /* This figures out if the label is allowed to       slop over into the next blank field */    slen = strlen (string);    while ((slen > fieldlen) && (nextcol <= mxcol) &&	   !((nc = lookat (row, nextcol)) -> flags & is_valid) &&	   !(nc->label)) {	if (! col_hidden [nextcol])	    fieldlen += fwidth [nextcol];	nextcol++;    }    if (slen > fieldlen)	slen = fieldlen;    /* Now justify and print */    start = leftflush ? field : field + fieldlen - slen;    last = field+fieldlen;    fp = field;    while (fp < start)	*fp++ = ' ';    while (slen--)	*fp++ = *string++;    if ((! hasvalue) || fieldlen != fwidth[col]) 	while (fp < last)	    *fp++ = ' ';    *fp = '\0';#ifdef VMS    mvaddstr(r, c, field);	/* this is a macro */#else    (void) mvaddstr(r, c, field);#endif    *nextcolp  = nextcol;    *fieldlenp = fieldlen;}intetype(e)register struct enode *e;{    if (e == (struct enode *)0)	return NUM;    switch (e->op) {    case O_SCONST: case '#': case DATE: case FMT: case STINDEX:    case EXT: case SVAL: case SUBSTR:        return (STR);    case '?':    case IF:        return(etype(e->e.o.right->e.o.left));    case 'f':        return(etype(e->e.o.right));    case O_VAR: {	register struct ent *p;	p = e->e.v.vp;	if (p->expr) 	    return(p->flags & is_strexpr ? STR : NUM);	else if (p->label)	    return(STR);	else	    return(NUM);	}    default:	return(NUM);    }}/* return 1 if yes given, 0 otherwise */intyn_ask(msg)char	*msg;{	char ch;	(void) move (0, 0);	(void) clrtoeol ();	(void) addstr (msg);	(void) refresh();	ch = nmgetch();	if ( ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N' ) {		if (ch == ctl('g') || ch == ESC)			return(-1);		error("y or n response required");		return (-1);	}	if (ch == 'y' || ch == 'Y')		return(1);	else		return(0);}#include <pwd.h>char	*findhome(path)char	*path;{	static	char	*HomeDir = NULL;	extern	char	*getenv();	if (*path == '~')	{	char	*pathptr;		char	tmppath[PATHLEN];		if (HomeDir == NULL)		{	HomeDir = getenv("HOME");			if (HomeDir == NULL)				HomeDir = "/";		}		pathptr = path + 1;		if ((*pathptr == '/') || (*pathptr == '\0'))		{	strcpy(tmppath, HomeDir);		}		else		{	struct	passwd *pwent;			extern	struct	passwd *getpwnam();			char	*namep;			char	name[50];			namep = name;			while ((*pathptr != '\0') && (*pathptr != '/'))				*(namep++) = *(pathptr++);			*namep = '\0';			if ((pwent = getpwnam(name)) == NULL)			{	sprintf(path, "Can't find user %s", name);				return(NULL);			}			strcpy(tmppath, pwent->pw_dir);		}		strcat(tmppath, pathptr);		strcpy(path, tmppath);	}	return(path);}#ifdef DOBACKUPS#include <sys/types.h>#include <sys/stat.h>/* * make a backup copy of a file, use the same mode and name in the format * [path/]#file~ * return 1 if we were successful, 0 otherwise */intbackup_file(path)char	*path;{	struct	stat	statbuf;	char	fname[PATHLEN];	char	tpath[PATHLEN];#ifdef sequent	char	*buf;#else	char	buf[BUFSIZ];#endif	char	*tpp;	int	infd, outfd;	int	count;	/* tpath will be the [path/]file ---> [path/]#file~ */	strcpy(tpath, path);	if ((tpp = strrchr(tpath, '/')) == NULL)		tpp = tpath;	else		tpp++;	strcpy(fname, tpp);	sprintf(tpp, "#%s~", fname);	if (stat(path, &statbuf) == 0)	{#ifdef sequent		if ((buf = xmalloc(statbuf.st_blksize)) == (char *)0)			return(0);#endif		if ((infd = open(path, O_RDONLY, 0)) < 0)		{#ifdef sequent			xfree(buf);#endif			return(0);		}		if ((outfd = open(tpath, O_TRUNC|O_WRONLY|O_CREAT,					statbuf.st_mode)) < 0)		{#ifdef sequent			xfree(buf);#endif			return(0);		}#ifdef sequent		while((count = read(infd, buf, statbuf.st_blksize)) > 0)#else		while((count = read(infd, buf, sizeof(buf))) > 0)#endif		{	if (write(outfd, buf, count) != count)			{	count = -1;				break;			}		}		close(infd);		close(outfd);#ifdef sequent		xfree(buf);#endif		return((count < 0) ? 0 : 1);	}	else	if (errno == ENOENT)		return(1);	return(0);}#endif

⌨️ 快捷键说明

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