Esempio n. 1
0
void send_net(net_header_rec *nh, unsigned int *list, char *text, char *byname)
{
  int f;
  char s[100];
  long l1;

  sprintf(s,"%sP0-WT.NET",net_data);
  f=sh_open(s,O_RDWR | O_BINARY | O_APPEND | O_CREAT, S_IREAD | S_IWRITE);
  sh_lseek(f,0L,SEEK_END);
  if (!list)
    nh->list_len=0;
  if (!text)
    nh->length=0;
  if (nh->list_len)
    nh->tosys=0;
  l1=nh->length;
  if (byname && *byname)
    nh->length += strlen(byname)+1;
  sh_write(f,(void *)nh,sizeof(net_header_rec));
  if (nh->list_len)
    sh_write(f,(void *)list,2*(nh->list_len));
  if (byname && *byname)
    sh_write(f,byname, strlen(byname)+1);
  if (nh->length)
    sh_write(f,(void *)text,l1);
  sh_close(f);
}
Esempio n. 2
0
key_t CBUShm::myftok(const char *path, int ID)
{
    int  handle;
    /* ****** Updated by CHENYH at 2005-11-21 21:14:21 ******
    	char buffer[255];
    	char current_path[255];
    	memset(buffer,0,sizeof(buffer));
    	memset(current_path,0,sizeof(current_path));
    	if(NULL != getcwd(current_path,sizeof(current_path)))
    	{
    	    sprintf(buffer,"%s/%s.shm",current_path,path);
    	}
    	else
    		sprintf(buffer,"/tmp/%s.shm",path);
       sprintf(buffer,"%s.shm",path);
    *******************************************************/

    //handle=open(buffer,O_RDWR);
    handle = sh_open(path,O_RDWR,SH_DENYNO);
    if (handle == -1)
    {
        handle = sh_open(path,O_CREAT|O_RDWR,SH_DENYNO);
        if (handle == -1)
        {
            return -2;
        }
        else
        {
            close(handle);
        }
    }
    else
    {
        close(handle);
    }
#ifdef WIN32
    return(1);
#else
    return ftok(path,ID);
#endif
}
Esempio n. 3
0
int open_email(int wrt)
{
  char s[100];
  int f,i;

  sprintf(s,"%sEMAIL.DAT", syscfg.datadir);

  for (i=0; i<5; i++) {
    if (wrt)
      f=sh_open(s, O_RDWR|O_BINARY|O_CREAT, S_IREAD|S_IWRITE);
    else
      f=sh_open1(s, O_RDONLY|O_BINARY);
    if ((f>0) || (errno!=EACCES))
      break;
  }

  return(f);
}
Esempio n. 4
0
void write_user(unsigned int un, userrec *u)
{
  char s[81];
  long pos;
  int f;

  if ((un<1) || (un>syscfg.maxusers))
    return;

  sprintf(s,"%sUSER.LST",syscfg.datadir);
  f=sh_open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  if (f>=0) {
    pos=((long) syscfg.userreclen) * ((long) un);
    sh_lseek(f,pos,SEEK_SET);
    sh_write(f, (void *)u, syscfg.userreclen);
    sh_close(f);
  }
}
Esempio n. 5
0
    static int  acctinit(History_t *hp)
    {
	register char *cp, *acctfile;
	Namval_t *np = nv_search("ACCTFILE",((Shell_t*)hp->histshell)->var_tree,0);

	if(!np || !(acctfile=nv_getval(np)))
		return(0);
	if(!(cp = getlogin()))
	{
		struct passwd *userinfo = getpwuid(getuid());
		if(userinfo)
			cp = userinfo->pw_name;
		else
			cp = "unknown";
	}
	logname = strdup(cp);
	if((acctfd=sh_open(acctfile,
		O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR))>=0 &&
	    (unsigned)acctfd < 10)
	{
		int n;
		if((n = fcntl(acctfd, F_DUPFD, 10)) >= 0)
		{
			close(acctfd);
			acctfd = n;
		}
	}
	if(acctfd < 0)
	{
		acctfd = 0;
		return(0);
	}
	if(sh_isdevfd(acctfile))
	{
		char newfile[16];
		sfsprintf(newfile,sizeof(newfile),"%.8s%d\0",e_devfdNN,acctfd);
		nv_putval(np,newfile,NV_RDONLY);
	}
	else
		fcntl(acctfd,F_SETFD,FD_CLOEXEC);
	return(1);
    }
Esempio n. 6
0
void savebase(int b)
/* saves message information in memory to disk */
{
  int f;
  char s[81];

  sprintf(s, "%s%s.SUB", syscfg.datadir, subboards[b].filename);
  f = sh_open(s, O_RDWR | O_BINARY | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
  sh_lseek(f, 0L, SEEK_SET);
  msgs[0].owneruser = nummsgs;
  sh_write(f, (void *)(&msgs[0]), ((nummsgs+1) * sizeof(postrec)));
  sh_close(f);
  if (nummsgs) {
    sub_dates[b]=msgs[nummsgs].qscan;
  } else {
    sub_dates[b]=1;
  }
    lock_status();
    status.filechange[filechange_posts]++;
  save_status();
}
Esempio n. 7
0
int open_file(char *fn)
/* opens a WWIV Type-2 data file */
{
  int f,i;
  char s[81];

  sprintf(s,"%s%s.DAT",syscfg.msgsdir,fn);
  f=sh_open1(s,O_RDWR | O_BINARY);
  if (f<0) {
    f=sh_open(s,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
    for (i=0; i<2048; i++)
      gat[i]=0;
    sh_write(f,(void *)gat,4096);
    strcpy(gatfn,fn);
/*    chsize(f,4096L + (75L * 1024L)); */
  }
  if (strcmp(gatfn,fn)) {
    sh_lseek(f,0L,SEEK_SET);
    sh_read(f,(void *)gat,4096);
    strcpy(gatfn,fn);
  }
  return(f);
}
Esempio n. 8
0
File: history.c Progetto: att/ast
//
// Open the history file. If HISTNAME is not given and userid==0 then no history file. If login_sh
// and HISTFILE is longer than HIST_MAX bytes then it is cleaned up.
//
// hist_open() returns 1, if history file is opened.
//
int sh_histinit(void *sh_context) {
    Shell_t *shp = sh_context;
    int fd;
    History_t *hp;
    char *histname;
    char *fname = NULL;
    int histmask, maxlines, hist_start = 0;
    char *cp;
    off_t hsize = 0;

    shgd->hist_ptr = hist_ptr;
    if (shgd->hist_ptr) return 1;
    if (!(histname = nv_getval(HISTFILE))) {
        int offset = stktell(shp->stk);
        cp = nv_getval(HOME);
        if (cp) sfputr(shp->stk, cp, -1);
        sfputr(shp->stk, hist_fname, 0);
        stkseek(shp->stk, offset);
        histname = stkptr(shp->stk, offset);
    }

#if 0
// TODO: Figure out if this should be enabled. Originally excluded via `#ifdef future`.
    if (hp = wasopen) {
        // Reuse history file if same name.
        wasopen = 0;
        shgd->hist_ptr = hist_ptr = hp;
        if (strcmp(histname, hp->histname) == 0) {
            return 1;
        } else {
            hist_free();
        }
    }
#endif  // future

retry:
    cp = path_relative(shp, histname);
    if (!histinit) histmode = S_IRUSR | S_IWUSR;
    if ((fd = open(cp, O_BINARY | O_APPEND | O_RDWR | O_CREAT | O_CLOEXEC, histmode)) >= 0) {
        hsize = lseek(fd, (off_t)0, SEEK_END);
    }
    if ((unsigned)fd < 10) {
        int n;
        if ((n = sh_fcntl(fd, F_DUPFD_CLOEXEC, 10)) >= 0) {
            sh_close(fd);
            fd = n;
        }
    }
    // Make sure that file has history file format.
    if (hsize && hist_check(fd)) {
        sh_close(fd);
        hsize = 0;
        if (unlink(cp) >= 0) goto retry;
        fd = -1;
    }

    // Don't allow root a history_file in /tmp.
    if (fd < 0 && shgd->userid) {
        fname = ast_temp_file(NULL, NULL, &fd, O_APPEND | O_CLOEXEC);
        if (!fname) return 0;
    }

    if (fd < 0) return 0;
    // Set the file to close-on-exec.
    (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
    cp = nv_getval(HISTSIZE);
    if (cp) {
        maxlines = (unsigned)strtol(cp, NULL, 10);
    } else {
        maxlines = HIST_DFLT;
    }
    for (histmask = 16; histmask <= maxlines; histmask <<= 1) {
        ;  // empty loop
    }
    histmask -= 1;
    hp = calloc(1, sizeof(History_t) + histmask * sizeof(off_t));
    if (!hp) {
        sh_close(fd);
        return 0;
    }

    shgd->hist_ptr = hist_ptr = hp;
    hp->histshell = shp;
    hp->histsize = maxlines;
    hp->histmask = histmask;
    hp->histfp = sfnew(NULL, NULL, HIST_BSIZE, fd, SF_READ | SF_WRITE | SF_APPENDWR | SF_SHARE);
    hp->histind = 1;
    hp->histcmds[1] = 2;
    hp->histcnt = 2;
    hp->histname = strdup(histname);
    hp->histdisc = hist_disc;
    if (hsize == 0) {
        // Put special characters at front of file.
        sfwrite(hp->histfp, (char *)hist_stamp, 2);
        sfsync(hp->histfp);
    } else {
        // Initialize history list.
        int first, last;
        off_t mark, size = (HIST_MAX / 4) + maxlines * HIST_LINE;
        hp->histind = first = hist_nearend(hp, hp->histfp, hsize - size);
        histinit = 1;
        hist_eof(hp);  // this sets histind to last command
        if ((hist_start = (last = (int)hp->histind) - maxlines) <= 0) hist_start = 1;
        mark = hp->histmarker;
        while (first > hist_start) {
            size += size;
            first = hist_nearend(hp, hp->histfp, hsize - size);
            hp->histind = first;
        }
        histinit = hist_start;
        hist_eof(hp);
        if (!histinit) {
            sfseek(hp->histfp, hp->histcnt = hsize, SEEK_SET);
            hp->histind = last;
            hp->histmarker = mark;
        }
        histinit = 0;
    }
    if (fname) {
        unlink(fname);
        free(fname);
    }
    if (hist_clean(fd) && hist_start > 1 && hsize > HIST_MAX) {
#ifdef DEBUG
        sfprintf(sfstderr, "%d: hist_trim hsize=%d\n", getpid(), hsize);
        sfsync(sfstderr);
#endif  // DEBUG
        hp = hist_trim(hp, (int)hp->histind - maxlines);
    }
    sfdisc(hp->histfp, &hp->histdisc);
    STORE_VT((HISTCUR)->nvalue, i32p, &hp->histind);
    sh_timeradd(1000L * (HIST_RECENT - 30), 1, hist_touch, hp->histname);
    hp->auditfp = NULL;

    char buff[SF_BUFSIZE];
    if (!sh_isstate(shp, SH_INTERACTIVE)) return 1;
    hp->auditmask = sh_checkaudit(hp, AUDIT_FILE, buff, sizeof(buff));
    if (!hp->auditmask) return 1;

    if ((fd = sh_open(buff, O_BINARY | O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC,
                      S_IRUSR | S_IWUSR)) >= 0 &&
        fd < 10) {
        int n;
        if ((n = sh_fcntl(fd, F_DUPFD_CLOEXEC, 10)) >= 0) {
            sh_close(fd);
            fd = n;
        }
    }
    if (fd >= 0) {
        (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
        hp->tty = strdup(isatty(2) ? ttyname(2) : "notty");
        hp->auditfp = sfnew(NULL, NULL, -1, fd, SF_WRITE);
    }

    return 1;
}
Esempio n. 9
0
int scan_mailbox()
{
   int i, z, *area_msg, *area_num, maxareas, line;
   char filename[80];
   struct _sys tsys;

   sprintf (filename, SYSMSG_PATH, config->sys_path);
   if ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
      return (0);

   z = (int)(filelength (i) / SIZEOF_MSGAREA);

   if ((area_msg = (int *)malloc (z * sizeof (int))) == NULL)
      return (0);

   if ((area_num = (int *)malloc (z * sizeof (int))) == NULL) {
      free (area_msg);
      return (0);
   }

   z = 0;
   while (read (i, &tsys, SIZEOF_MSGAREA) == SIZEOF_MSGAREA)
      area_num[z++] = tsys.msg_num;
   maxareas = z;

   close (i);

   memset (area_msg, 0, sizeof (int) * z);
   z = 1;

   if (msg_list != NULL)
      free (msg_list);

   if ((msg_list = (struct _msg_list *)malloc (MAX_PRIV_MAIL * sizeof (struct _msg_list))) == NULL)
      return (0);

   m_print (bbstxt[B_CHECK_MAIL]);

   rotatepos = 0;
   m_print (" \b%c", rotate[rotatepos]);
   rotatepos = (rotatepos + 1) % 4;

   z = fido_index (z, area_msg, area_num, maxareas);
   z = quick_index (z, area_msg, area_num, maxareas);
   z = gold_index (z, area_msg, area_num, maxareas);
   z = pipbase_index (z, area_msg, area_num, maxareas);
   z = squish_index (z, area_msg, area_num, maxareas);

   max_priv_mail = z - 1;
   last_mail = 0;
   last_read_system = 0;

   if (z == 1) {
      free (area_num);
      free (area_msg);

      m_print(bbstxt[B_NO_MAIL_TODAY]);
      press_enter();

      return (0);
   }

   num_msg = last_msg = max_priv_mail;
   first_msg = 0;

   m_print(bbstxt[B_THIS_MAIL]);
   m_print(bbstxt[B_THIS_MAIL_UNDERLINE]);
   line = 3;

   for (i = 0; i < maxareas; i++) {
      if (!area_msg[i] || !read_system2 (area_num[i], 1, &tsys))
         continue;

      m_print (bbstxt[B_MAIL_LIST_FORMAT], tsys.msg_name, area_msg[i]);
      if ((line = more_question (line)) == 0)
         break;
   }

   free (area_num);
   free (area_msg);

   m_print (bbstxt[B_READ_MAIL_NOW]);
   i = yesno_question (DEF_YES);

   m_print (bbstxt[B_ONE_CR]);
   return (i);
}
Esempio n. 10
0
void * CBUShm::Open(const char *name, int shmsize)
{
    char buf[256];
    Close();
    sprintf(buf,"%s.shm",name);
    m_fileno = sh_open(buf,O_CREAT|O_RDWR,SH_DENYNO);
    if (m_fileno==-1)
    {
        sprintf(m_szmsg,"Cannot open SHM File for '%s' size=%d",name,shmsize);
        return(NULL);
    }
#ifdef WIN32
    if (shmsize>0)
    {
        m_shmid = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,shmsize,(LPCSTR)name);
        if (m_shmid==NULL)
        {
            // 共享内存创建错误:
            sprintf(m_szmsg,"Cannot open share memory for SHM(%s) size=%d -errno:%u!",name,shmsize,GetLastError());
            Close();
            return(NULL);
        }
    }
    else
    {
        m_shmid = OpenFileMapping(FILE_MAP_WRITE|FILE_MAP_READ,FALSE,(LPCSTR)name);
        if (m_shmid==NULL)
        {
            sprintf(m_szmsg,"Cannot open share memory for SHM(%s) - errno:%u!",name,GetLastError());
            Close();
            return(NULL);
        }
    }
    m_address = MapViewOfFile(m_shmid,FILE_MAP_WRITE|FILE_MAP_READ,0,0,0);
    if (m_address==NULL)
    {
        sprintf(m_szmsg,"MapViewOfFile fail [SHM(%s) size=%d] - errno:%u!",name,shmsize,GetLastError());
        Close();
        return(NULL);
    }
    BY_HANDLE_FILE_INFORMATION finfo;
    GetFileInformationByHandle(m_shmid,&finfo);
    m_size = finfo.nFileSizeLow;
    return(m_address);
#else
    bool bcreated = true;
    key_t shmkey = myftok(buf,BUSHM_KID);
    if (shmsize>0)
    {
        m_shmid = shmget(shmkey,shmsize,SHM_RIGHT|IPC_CREAT);
    }
    else
        m_shmid = -1;
    if (m_shmid==-1)
    {
        m_shmid = shmget(shmkey,0,BUSHM_FLAG);
        bcreated = false;
    }
    if (m_shmid==-1)
    {
        // 共享内存创建错误:
        sprintf(m_szmsg,"Cannot open share memory for key:0x%X-errno:%d!",shmkey,errno);
        Close();
        return(NULL);
    }
    m_address = shmat(m_shmid,0,0);
#ifdef SHM_FAILED //11:50 2006-7-4 for HP_UX64 
    if (m_address==SHM_FAILED)
#else
    if (m_address==(void *)-1L)
#endif
    {
        m_address = NULL;
        // 则表示挂接的地址出现问题,可能属于flag不正确的缘故
        sprintf(m_szmsg,"shmkey=%x:shmat(%d,0,0) 返回错误errno=%d!",shmkey,m_shmid,errno);
        Close();
        if (bcreated)
            Remove(name);
    }
    struct shmid_ds info;
    shmctl(m_shmid,IPC_STAT,&info);
    m_size = info.shm_segsz;
    return(m_address);
#endif
}
Esempio n. 11
0
/*
 * open the history file
 * if HISTNAME is not given and userid==0 then no history file.
 * if login_sh and HISTFILE is longer than HIST_MAX bytes then it is
 * cleaned up.
 * hist_open() returns 1, if history file is open
 */
int  sh_histinit(void *sh_context)
{
	Shell_t *shp = (Shell_t*)sh_context;
	register int fd;
	register History_t *hp;
	register char *histname;
	char *fname=0;
	int histmask, maxlines, hist_start=0;
	register char *cp;
	register off_t hsize = 0;

	if(shgd->hist_ptr=hist_ptr)
		return(1);
	if(!(histname = nv_getval(HISTFILE)))
	{
		int offset = staktell();
		if(cp=nv_getval(HOME))
			stakputs(cp);
		stakputs(hist_fname);
		stakputc(0);
		stakseek(offset);
		histname = stakptr(offset);
	}
#ifdef future
	if(hp=wasopen)
	{
		/* reuse history file if same name */
		wasopen = 0;
		shgd->hist_ptr = hist_ptr = hp;
		if(strcmp(histname,hp->histname)==0)
			return(1);
		else
			hist_free();
	}
#endif
retry:
	cp = path_relative(shp,histname);
	if(!histinit)
		histmode = S_IRUSR|S_IWUSR;
	if((fd=open(cp,O_BINARY|O_APPEND|O_RDWR|O_CREAT,histmode))>=0)
	{
		hsize=lseek(fd,(off_t)0,SEEK_END);
	}
	if((unsigned)fd <=2)
	{
		int n;
		if((n=fcntl(fd,F_DUPFD,10))>=0)
		{
			close(fd);
			fd=n;
		}
	}
	/* make sure that file has history file format */
	if(hsize && hist_check(fd))
	{
		close(fd);
		hsize = 0;
		if(unlink(cp)>=0)
			goto retry;
		fd = -1;
	}
	if(fd < 0)
	{
#if KSHELL
		/* don't allow root a history_file in /tmp */
		if(shgd->userid)
#endif	/* KSHELL */
		{
			if(!(fname = pathtmp(NIL(char*),0,0,NIL(int*))))
				return(0);
			fd = open(fname,O_BINARY|O_APPEND|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
		}
	}
	if(fd<0)
		return(0);
	/* set the file to close-on-exec */
	fcntl(fd,F_SETFD,FD_CLOEXEC);
	if(cp=nv_getval(HISTSIZE))
		maxlines = (unsigned)strtol(cp, (char**)0, 10);
	else
		maxlines = HIST_DFLT;
	for(histmask=16;histmask <= maxlines; histmask <<=1 );
	if(!(hp=new_of(History_t,(--histmask)*sizeof(off_t))))
	{
		close(fd);
		return(0);
	}
	shgd->hist_ptr = hist_ptr = hp;
	hp->histshell = (void*)shp;
	hp->histsize = maxlines;
	hp->histmask = histmask;
	hp->histfp= sfnew(NIL(Sfio_t*),hp->histbuff,HIST_BSIZE,fd,SF_READ|SF_WRITE|SF_APPENDWR|SF_SHARE);
	memset((char*)hp->histcmds,0,sizeof(off_t)*(hp->histmask+1));
	hp->histind = 1;
	hp->histcmds[1] = 2;
	hp->histcnt = 2;
	hp->histname = strdup(histname);
	hp->histdisc = hist_disc;
	if(hsize==0)
	{
		/* put special characters at front of file */
		sfwrite(hp->histfp,(char*)hist_stamp,2);
		sfsync(hp->histfp);
	}
	/* initialize history list */
	else
	{
		int first,last;
		off_t mark,size = (HIST_MAX/4)+maxlines*HIST_LINE;
		hp->histind = first = hist_nearend(hp,hp->histfp,hsize-size);
		histinit = 1;
		hist_eof(hp);	 /* this sets histind to last command */
		if((hist_start = (last=(int)hp->histind)-maxlines) <=0)
			hist_start = 1;
		mark = hp->histmarker;
		while(first > hist_start)
		{
			size += size;
			first = hist_nearend(hp,hp->histfp,hsize-size);
			hp->histind = first;
		}
		histinit = hist_start;
		hist_eof(hp);
		if(!histinit)
		{
			sfseek(hp->histfp,hp->histcnt=hsize,SEEK_SET);
			hp->histind = last;
			hp->histmarker = mark;
		}
		histinit = 0;
	}
	if(fname)
	{
		unlink(fname);
		free((void*)fname);
	}
	if(hist_clean(fd) && hist_start>1 && hsize > HIST_MAX)
	{
#ifdef DEBUG
		sfprintf(sfstderr,"%d: hist_trim hsize=%d\n",getpid(),hsize);
		sfsync(sfstderr);
#endif /* DEBUG */
		hp = hist_trim(hp,(int)hp->histind-maxlines);
	}
	sfdisc(hp->histfp,&hp->histdisc);
#if KSHELL
	(HISTCUR)->nvalue.lp = (&hp->histind);
#endif /* KSHELL */
	sh_timeradd(1000L*(HIST_RECENT-30), 1, hist_touch, (void*)hp->histname);
#if SHOPT_ACCTFILE
	if(sh_isstate(SH_INTERACTIVE))
		acctinit(hp);
#endif /* SHOPT_ACCTFILE */
#if SHOPT_AUDIT
	{
		char buff[SF_BUFSIZE];
		hp->auditfp = 0;
		if(sh_isstate(SH_INTERACTIVE) && (hp->auditmask=sh_checkaudit(hp,SHOPT_AUDITFILE, buff, sizeof(buff))))
		{
			if((fd=sh_open(buff,O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR))>=0 && fd < 10)
			{
				int n;
				if((n = sh_fcntl(fd,F_DUPFD, 10)) >= 0)
				{
					sh_close(fd);
					fd = n;
				}
			}
			if(fd>=0)
			{
				fcntl(fd,F_SETFD,FD_CLOEXEC);
				hp->tty = strdup(ttyname(2));
				hp->auditfp = sfnew((Sfio_t*)0,NULL,-1,fd,SF_WRITE);
			}
		}
	}
#endif
	return(1);
}
Esempio n. 12
0
void gate_msg(net_header_rec *nh, char *text, unsigned short nn, char *byname,unsigned int *list, unsigned short fnn)
{
  char newname[128], fn[100], qn[100], on[100];
  char *ti, nm[82], *ss;
  int f,i;
  unsigned short ntl;

  if (strlen(text)<80) {
    ti=text;
    text+=strlen(ti)+1;
    ntl=nh->length-strlen(ti)-1;
    ss=strchr(text,'\r');
    if (ss && (ss-text<80) && (ss-text<ntl)) {
      strncpy(nm,text,ss-text);
      nm[ss-text]=0;
      ss++;
      if (*ss=='\n')
        ss++;
      nh->length-=(ss-text);
      ntl-=(ss-text);
      text=ss;

      qn[0]=on[0]=0;

      if (fnn==65535) {

        strcpy(newname,nm);
        ss=strrchr(newname,'@');
        if (ss) {
          sprintf(ss+1,"%u",net_networks[nn].sysnum);
          ss=strrchr(nm,'@');
          if (ss) {
            ++ss;
            while ((*ss>='0') && (*ss<='9'))
              ++ss;
            strcat(newname,ss);
          }
          strcat(newname,"\r\n");
        }
      } else {
        if ((nm[0]=='`') && (nm[1]=='`')) {
          for (i=strlen(nm)-2; i>0; i--) {
            if ((nm[i]=='`') && (nm[i+1]=='`')) {
              break;
            }
          }
          if (i>0) {
            i+=2;
            strncpy(qn,nm,i);
            qn[i]=' ';
            qn[i+1]=0;
          }
        } else
          i=0;
        if (qn[0]==0) {
          ss=strrchr(nm,'#');
          if (ss) {
            if ((ss[1]>='0') && (ss[1]<='9')) {
              *ss=0;
              ss--;
              while ((ss>nm) && (*ss==' ')) {
                *ss=0;
                ss--;
              }
            }
          }
          if (nm[0]) {
            if (nh->fromuser)
              sprintf(qn,"``%s`` ",nm);
            else
              strcpy(on,nm);
          }
        }
        if ((on[0]==0) && (nh->fromuser==0)) {
          strcpy(on,nm+i);
        }
        if (on[0])
          sprintf(newname,"%s%s %s AT %u\r\n",qn, net_networks[fnn].name,
                  on, nh->fromsys);
        else
          sprintf(newname,"%s%s #%u AT %u\r\n",qn, net_networks[fnn].name,
                  nh->fromuser, nh->fromsys);

        nh->fromsys=net_networks[nn].sysnum;
        nh->fromuser=0;
      }


      nh->length += strlen(newname);
      if ((nh->main_type == main_type_email_name) ||
          (nh->main_type == main_type_new_post))
        nh->length += strlen(byname)+1;
      sprintf(fn,"%sP0-WT.NET",net_data);
      f=sh_open(fn,O_RDWR|O_BINARY|O_APPEND | O_CREAT, S_IREAD|S_IWRITE);
      if (f) {
        sh_lseek(f,0L,SEEK_END);
        if (!list)
          nh->list_len=0;
        if (nh->list_len)
          nh->tosys=0;
        sh_write(f,nh, sizeof(net_header_rec));
        if (nh->list_len)
          sh_write(f,list,2*(nh->list_len));
        if ((nh->main_type == main_type_email_name) ||
            (nh->main_type == main_type_new_post))
          sh_write(f,byname, strlen(byname)+1);
        sh_write(f,ti,strlen(ti)+1);
        sh_write(f,newname,strlen(newname));
        sh_write(f,text,ntl);
        sh_close(f);
      }
    }
  }
}
Esempio n. 13
0
void savefile(char *b, long l1, messagerec *m1, char *aux)
/* saves a message in memory to disk */
{
  int f,gatp,i5,i4,gati[128];
  messagerec m;
  char s[81],s1[81];
  int section;
  long l2;

  setcbrk(0);
  m=*m1;
  switch(m.storage_type) {
    case 0:
    case 1:
      lock_status();
      m.stored_as=status.qscanptr++;
      save_status();
      ltoa(m.stored_as,s1,16);
      strcpy(s,syscfg.msgsdir);
      if (m.storage_type==1) {
        strcat(s,aux);
        strcat(s,"\\");
      }
      strcat(s,s1);
      f=sh_open(s,O_RDWR | O_CREAT | O_BINARY,S_IREAD | S_IWRITE);
      sh_write(f, (void *)b,l1);
      sh_close(f);
      break;
    case 2:
      f=open_file(aux);
      if (f>0) {
        for (section=0; section<1024; section++) {
          set_gat_section(f,section);
          gatp=0;
          i5=(int) ((l1 + 511L)/512L);
          i4=1;
          while ((gatp<i5) && (i4<2048)) {
            if (gat[i4]==0)
              gati[gatp++]=i4;
            ++i4;
          }
          if (gatp>=i5) {
            l2=MSG_STARTING;
            gati[gatp]=-1;
            for (i4=0; i4<i5; i4++) {
              sh_lseek(f,l2 + 512L * (long)(gati[i4]),SEEK_SET);
              sh_write(f,(void *)(&b[i4*512]),512);
              gat[gati[i4]]=gati[i4+1];
            }
            save_gat(f);
            break;
          }
        }
        sh_close(f);
      }
      m.stored_as=((long) gati[0]) + ((long)gat_section)*2048L;
      break;
    case 255:
      f=sh_open(aux,O_RDWR | O_CREAT | O_BINARY,S_IREAD | S_IWRITE);
      sh_write(f, (void *)b,l1);
      sh_close(f);
      break;
    default:
      break;
  }
  farfree((void *)b);
  *m1=m;
}
Esempio n. 14
0
void edit_file (char *name, int len, int width)
{
   int fd, i, startrow, m;
   char insert, *p, savefile = 0, endrun = 0;

#ifdef __OS2__
   if ((txtptr = malloc (MAX_SIZE)) == NULL)
      return;
#else
   if ((txtptr = farmalloc (MAX_SIZE)) == NULL)
      return;
#endif

   memset (txtptr, 0, MAX_SIZE);

   for (i = 0; i < 26; i++) {
      oldrow[i] = NULL;
      screenrow[i] = NULL;
   }

   if ((fd = sh_open (name, SH_DENYRW, O_RDONLY|O_TEXT, S_IREAD|S_IWRITE)) != -1) {
      i = read (fd, txtptr, MAX_SIZE - 1);
      txtptr[i] = '\0';
      close (fd);

      unlink (name);

      ptr = txtptr;
      while (*ptr) {
         if (*ptr == 0x01)
            *ptr = '@';
         ptr++;
      }
   }

   clrscr_s ();
   ptr = txtptr;
   startrow = 0;
   width--;
   insert = 1;

   editrow[0] = '\0';
   display_screen (startrow, width, len);

   while (!endrun && CARRIER) {
      if ((i = readkey ()) == -1)
         continue;

      if (i == 0x1A || i == 0x2C1A) {
         savefile = 1;
         break;
      }

      switch (i) {
         // Home
         case 0x4700:
            if (ptr != screenrow[cy]) {
               ptr = screenrow[cy];
               display_screen (startrow, width, len);
            }
            break;

         // End
         case 0x4F00:
            if (screenrow[cy + 1] == NULL) {
               p = strchr (screenrow[cy], '\0');
               if (ptr != p) {
                  ptr = p;
                  display_screen (startrow, width, len);
               }
            }
            else if (ptr != screenrow[cy + 1] - 1) {
               ptr = screenrow[cy + 1] - 1;
               display_screen (startrow, width, len);
            }
            break;

         // Freccia su'
         case CTRLE:
         case 0x4800:
            if (cy <= 0) {
               if (startrow <= 0)
                  break;
               if (startrow >= 10)
                  startrow -= 10;
               else
                  startrow = 0;
            }
            else
               cy--;

            if (screenrow[cy + 1] == NULL)
               m = strlen (screenrow[cy]);
            else
               m = (int)(screenrow[cy + 1] - screenrow[cy]);
            if (m && screenrow[cy][m - 1] == '\n')
               m--;

            if (cx > m)
               cx = m;
            ptr = screenrow[cy] + cx;
            cy++;
            display_screen (startrow, width, len);
            break;

         // Freccia giu'
         case CTRLX:
         case 0x5000:
            if (screenrow[cy + 1] == NULL)
               break;

            if (cy >= (len - 1)) {
               startrow += 10;
               clrscr_s ();
            }
            cy++;

            if (screenrow[cy + 1] == NULL)
               m = strlen (screenrow[cy]);
            else
               m = (int)(screenrow[cy + 1] - screenrow[cy]);
            if (m && screenrow[cy][m - 1] == '\n')
               m--;

            if (cx > m)
               cx = m;
            ptr = screenrow[cy] + cx;
            cy--;
            display_screen (startrow, width, len);
            break;

         // Freccia sinistra
         case CTRLS:
         case 0x4B00:
            if (cx && ptr > txtptr) {
               ptr--;
               display_screen (startrow, width, len);
            }
            break;

         // Freccia destra
         case CTRLD:
         case 0x4D00:
            if (ptr + 1 < screenrow[cy + 1]) {
               ptr++;
               display_screen (startrow, width, len);
            }
            break;

         // CTRL-Y
         case 0x19:
         case 0x1519:
            if (screenrow[cy + 1] == NULL) {
               if (*ptr != '\0') {
                  *ptr = '\0';
                  display_screen (startrow, width, len);
               }
            }
            else {
               strcpy (screenrow[cy], screenrow[cy + 1]);
               for (i=cy;i<26;i++)
                screenrow[i]=NULL;
               display_screen (startrow, width, len);
            }
            break;

         // Delete
         case 0x5300:
         case 0x7f:
            if (ptr >= txtptr) {
               strcpy (ptr, &ptr[1]);
               for (i = 0; i < 26; i++)
                  screenrow[i] = NULL;
               display_screen (startrow, width, len);
            }
            break;

         // Backspace
         case 0x08:
         case 0x0E08:
            if (ptr > txtptr) {
               ptr--;
               strcpy (ptr, &ptr[1]);
               for (i = cy + 1; i < 26; i++) {
                  if (screenrow[i] == NULL)
                     break;
                  screenrow[i]--;
               }
               if (cy <= 0 && cx <= 0) {
                  if (startrow >= 10)
                     startrow -= 10;
                  else
                     startrow = 0;
                  clrscr_s ();
                  display_screen (startrow, width, len);
               }
               else
                  display_screen (startrow, width, len);
            }
            break;

         // Caratteri speciali (^K)
         case CTRLK:
            cpos (5, 2);
            m_print2 ("\026\001\020\234^K");

            while ((i = readkey ()) == -1) {
               if (!CARRIER)
                  break;
            }

            i &= 0xFF;
            i = toupper (i);

            if (i != 0x1B) {
               if (i < 32)
                  m_print2 ("^%c", i + 0x40);
               else
                  m_print2 ("%c", i);
            }

            switch (i) {
               case CTRLS:
               case 'S':
                  endrun = 1;
                  savefile = 1;
                  break;

               case CTRLQ:
               case 'Q':
                  endrun = 1;
                  savefile = 0;
                  break;

               case '?':
                  if (!read_editor_help ())
                     break;
                  clrscr_s ();
                  for (i = 0; i < 26; i++)
                     screenrow[i] = NULL;
                  break;
            }

            cpos (5, 2);
            m_print2 ("\026\001\020\234    \026\001\003");

            display_screen (startrow, width, len);
            break;

         // Redraw dello schermo
         case 0x0C:
            cls ();

            change_attr (BLUE|_LGREY);
            del_line ();
            m_print (" * %s\n", sys.msg_name);

            msg_attrib (&msg, last_msg + 1, 0, 0);

            change_attr (RED|_BLUE);
            del_line ();
            cpos (5, (usr.width ? usr.width : 80) - 18);
            m_print ("\026\001\020\234^Z=Save  ^K?=Help");

            change_attr (CYAN|_BLACK);
            m_print (bbstxt[B_ONE_CR]);

            for (i = 0; i < 26; i++)
               screenrow[i] = NULL;
            display_screen (startrow, width, len);
            break;

         // Caratteri speciali (^Q)
         case CTRLQ:
            cpos (5, 2);
            m_print2 ("\026\001\020\234^Q");

            while ((i = readkey ()) == -1) {
               if (!CARRIER)
                  break;
            }

            i &= 0xFF;
            i = toupper (i);

            if (i != 0x1B) {
               if (i < 32)
                  m_print2 ("^%c", i + 0x40);
               else
                  m_print2 ("%c", i);
            }

            switch (i) {
               case CTRLS:
               case 'S':
                  if (ptr != screenrow[cy])
                     ptr = screenrow[cy];
                  break;

               case CTRLD:
               case 'D':
                  if (ptr != screenrow[cy + 1] - 1)
                     ptr = screenrow[cy + 1] - 1;
                  break;
            }

            cpos (5, 2);
            m_print2 ("\026\001\020\234    \026\001\003");

            display_screen (startrow, width, len);
            break;

         // Enter
         case 0x0D:
         case 0x1C0D:
            i = '\n';
            // Fall through

         // Carattere normale
         default:
            if ((i &= 0xFF) < 32 && i != '\n')
               break;

            if (insert) {
               p = &ptr[strlen (ptr)];
               while (p >= ptr) {
                  *(p + 1) = *p;
                  p--;
               }

               *ptr++ = i;

               for (i = cy + 1; i < 26; i++) {
                  if (screenrow[i] == NULL)
                     break;
                  screenrow[i]++;
               }
            }
            else {
               if (*ptr == '\0') {
                  *ptr++ = i;
                  *ptr = '\0';
               }
               else
                  *ptr++ = i;
            }
            display_screen (startrow, width, len);
            if (cy >= (len)) {
               startrow += 10;
               clrscr_s ();
               display_screen (startrow, width, len);
            }
            break;
      }
   }

   cls ();

   if (savefile) {
      if ((fd = sh_open (name, SH_DENYRW, O_WRONLY|O_TEXT|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE)) != -1) {
         write (fd, txtptr, strlen (txtptr));
         close (fd);
      }
   }

#ifdef __OS2__
   free (txtptr);
#else
   farfree (txtptr);
#endif
}
Esempio n. 15
0
int test_unop(Shell_t *shp,register int op,register const char *arg)
{
	struct stat statb;
	int f;
	switch(op)
	{
	    case 'r':
		return(permission(arg, R_OK));
	    case 'w':
		return(permission(arg, W_OK));
	    case 'x':
		return(permission(arg, X_OK));
	    case 'V':
#if SHOPT_FS_3D
	    {
		register int offset = staktell();
		if(stat(arg,&statb)<0 || !S_ISREG(statb.st_mode))
			return(0);
		/* add trailing / */
		stakputs(arg);
		stakputc('/');
		stakputc(0);
		arg = (const char*)stakptr(offset);
		stakseek(offset);
		/* FALL THRU */
	    }
#else
		return(0);
#endif /* SHOPT_FS_3D */
	    case 'd':
		return(test_stat(arg,&statb)>=0 && S_ISDIR(statb.st_mode));
	    case 'c':
		return(test_stat(arg,&statb)>=0 && S_ISCHR(statb.st_mode));
	    case 'b':
		return(test_stat(arg,&statb)>=0 && S_ISBLK(statb.st_mode));
	    case 'f':
		return(test_stat(arg,&statb)>=0 && S_ISREG(statb.st_mode));
	    case 'u':
		return(test_mode(arg)&S_ISUID);
	    case 'g':
		return(test_mode(arg)&S_ISGID);
	    case 'k':
#ifdef S_ISVTX
		return(test_mode(arg)&S_ISVTX);
#else
		return(0);
#endif /* S_ISVTX */
#if SHOPT_TEST_L
	    case 'l':
#endif
	    case 'L':
	    case 'h': /* undocumented, and hopefully will disappear */
		if(*arg==0 || arg[strlen(arg)-1]=='/' || lstat(arg,&statb)<0)
			return(0);
		return(S_ISLNK(statb.st_mode));

	    case 'C':
#ifdef S_ISCTG
		return(test_stat(arg,&statb)>=0 && S_ISCTG(statb.st_mode));
#else
		return(0);
#endif	/* S_ISCTG */
	    case 'H':
#ifdef S_ISCDF
	    {
		register int offset = staktell();
		if(test_stat(arg,&statb)>=0 && S_ISCDF(statb.st_mode))
			return(1);
		stakputs(arg);
		stakputc('+');
		stakputc(0);
		arg = (const char*)stakptr(offset);
		stakseek(offset);
		return(test_stat(arg,&statb)>=0 && S_ISCDF(statb.st_mode));
	    }
#else
		return(0);
#endif	/* S_ISCDF */

	    case 'S':
		return(isasock(arg,&statb));
	    case 'N':
		return(test_stat(arg,&statb)>=0 && tmxgetmtime(&statb) > tmxgetatime(&statb));
	    case 'p':
		return(isapipe(arg,&statb));
	    case 'n':
		return(*arg != 0);
	    case 'z':
		return(*arg == 0);
	    case 's':
		sfsync(sfstdout);
	    case 'O':
	    case 'G':
		if(*arg==0 || test_stat(arg,&statb)<0)
			return(0);
		if(op=='s')
			return(statb.st_size>0);
		else if(op=='O')
			return(statb.st_uid==shp->gd->userid);
		return(statb.st_gid==shp->gd->groupid);
	    case 'a':
	    case 'e':
		if(memcmp(arg,"/dev/",5)==0 && sh_open(arg,O_NONBLOCK))
			return(1);
		return(permission(arg, F_OK));
	    case 'o':
		f=1;
		if(*arg=='?')
			return(sh_lookopt(arg+1,&f)>0);
		op = sh_lookopt(arg,&f);
		return(op && (f==(sh_isoption(op)!=0)));
	    case 't':
	    {
		char *last;
		op = strtol(arg,&last, 10);
		return(*last?0:tty_check(op));
	    }
	    case 'v':
	    case 'R':
	    {
		Namval_t *np;
		Namarr_t *ap;
		int isref;
		if(!(np = nv_open(arg,shp->var_tree,NV_VARNAME|NV_NOFAIL|NV_NOADD|NV_NOREF)))
			return(0);
		isref = nv_isref(np);
		if(op=='R')
			return(isref);
		if(isref)
		{
			if(np->nvalue.cp)
				np = nv_refnode(np);
			else
				return(0);
			
		}
		if(ap = nv_arrayptr(np))
			return(nv_arrayisset(np,ap));
		return(!nv_isnull(np) || nv_isattr(np,NV_INTEGER));
	    }
	    default:
	    {
		static char a[3] = "-?";
		a[1]= op;
		errormsg(SH_DICT,ERROR_exit(2),e_badop,a);
		/* NOTREACHED  */
		return(0);
	    }
	}
}