Exemplo n.º 1
0
const char *get_sname(int socknumber, const char *proto, int numeric)
{
    static char buffer[64], init = 0;
    struct service *item;

    if (socknumber == 0)
	return ("*");
    if (numeric)
	goto do_ntohs;

    if (!init) {
	(void) read_services();
	init = 1;
    }
    buffer[0] = '\0';
    if (!strcmp(proto, "tcp"))
	item = searchlist(tcp_name, socknumber);
    else if (!strcmp(proto, "udp"))
	item = searchlist(udp_name, socknumber);
    else if (!strcmp(proto, "raw"))
	item = searchlist(raw_name, socknumber);
    else
	item = NULL;
    if (item) {
	strncpy(buffer, item->name, sizeof(buffer));
	buffer[sizeof(buffer) - 1] = '\0';
    }

    if (!buffer[0]) {
 do_ntohs:
	sprintf(buffer, "%d", ntohs(socknumber));
    }
    return (buffer);
}
Exemplo n.º 2
0
char *get_sname(int socknumber, char *proto, int numeric)
{
    static char buffer[64], init = 0;
    struct service *item;

    if (socknumber == 0)
	return ("*");
    if (numeric) {
	sprintf(buffer, "%d", ntohs(socknumber));
	return (buffer);
    }
    if (!init) {
	(void) read_services();
	init = 1;
    }
    buffer[0] = '\0';
    if (!strcmp(proto, "tcp")) {
	if ((item = searchlist(tcp_name, socknumber)) != NULL)
	    sprintf(buffer, "%s", item->name);
    } else if (!strcmp(proto, "udp")) {
	if ((item = searchlist(udp_name, socknumber)) != NULL)
	    sprintf(buffer, "%s", item->name);
    } else if (!strcmp(proto, "raw")) {
	if ((item = searchlist(raw_name, socknumber)) != NULL)
	    sprintf(buffer, "%s", item->name);

    }
    if (!buffer[0])
	sprintf(buffer, "%d", ntohs(socknumber));
    return (buffer);
}
Exemplo n.º 3
0
/* Testing correctness of tree 
 * Result : SUCCESSFUL SO FAR
 */
int main()
{
	addDOMNode(NULL, PARENT, "html");
	dom_node *n = addDOMNode(root, PARENT, "body");
	dom_node *k = addDOMNode(n, PARENT, "head");
	addDOMNode(n, SIBLING, "noooo");
	searchlist("test");
	printf("%p has %s\n",searchlist("head"), searchlist("head")->id );
	delgloballist(list);
}
Exemplo n.º 4
0
struct Elem *searchlist(struct Elem *list, int k) {
	for (; list; list = list->tail) {
		if ((list->tag == 2) && (searchlist(list->value.list, k)))
			return searchlist(list->value.list, k);
		if (list->tag || (list->value.i != k))
			continue;
		return list;
	}
	return 0;
}
Exemplo n.º 5
0
int fgettra(FILE *fp, segy *tp, int itr)
{
	int nread;
	if(lastfp != fp)  searchlist(fp);  /* search for match */
		
	
	if(infoptr == (struct insegyinfo *) NULL) {
		/* get first trace */
		if(0 >= fgettr(fp, tp)) return(0); /* error return */

		switch(infoptr->ftype) {
		case TTY:
			warn("stdin not redirected");
		break;
		case DISK:	/* correct */
		break;
		default:
			err("%s: input must be disk file",__FILE__);
		}
	


		efseeko(fp,(off_t) 0LL,SEEK_END);
		if( in_line_hdr ){
			infoptr->ntr = (off_t)((eftello(fp)-3600)/infoptr->nsegy);
			efseeko(fp, (off_t) 3600+infoptr->nsegy,SEEK_SET);
		}else{
			infoptr->ntr = (off_t)(eftello(fp)/infoptr->nsegy);
			efseeko(fp, (off_t) infoptr->nsegy,SEEK_SET);
		}
	} /* end first entry initialization */
	
	/* Check on requested trace number */
	if(itr >= infoptr->ntr)
		err("%s: trying to read off end of file",__FILE__);
	
	/* Position file pointer at start of requested trace */
	if( in_line_hdr ){
		efseeko(fp, (off_t) 3600+itr*infoptr->nsegy,SEEK_SET);
	}else{
		efseeko(fp, (off_t) itr*infoptr->nsegy,SEEK_SET);
	}
	
	nread=fgettr(fp, tp); /* let fgettr do the work */
	if(nread != infoptr->nsegy)
		err("%s: read %d bytes in trace of %d bytes",
		    __FILE__,nread,infoptr->nsegy);
	
	if(tp->ns != infoptr->nsfirst)
		warn("%s: header ns field = %d differs from first trace = %d",
		     __FILE__,tp->ns,infoptr->nsfirst);
	
	return(infoptr->ntr);
}
Exemplo n.º 6
0
/*
Inputs:  A pointer to a priority table and a user.
Outputs: Zero if user found, else 1, and priority table is modified.
Effects: All occurences of <user> in the priority table will be removed.
	 (There should only be one at most.)
*/
int del_user ( struct user_priority * ppri_tbl, char * user )
{
    int		limit;

    for (limit = PRI_MIN; limit <= PRI_MAX; limit++)
	if (searchlist(user, ppri_tbl->users[limit - PRI_MIN]))
	{
	    dellist (&(ppri_tbl->users[limit - PRI_MIN]), user);
	    return (0);
	}
    return (1);
}
head searchlist(head listhead,int searchitem)
{
     if(listhead == NULL)                   //No further nodes exist
     {
                 printf("Item not found\n");
                 return 0;
     }
     else if(listhead->item == searchitem)  //Item found at current node
     {
     printf("Item found at index location %d\n", listhead->index);
     return listhead;
     }
     else                                   //Item not found in current node, recursively search next nodes
     searchlist(listhead->next, searchitem);   
}
Exemplo n.º 8
0
void fputtr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length)
{
	/* search linked list for possible alternative */
	if(fp != lastfp)  searchlist(fp);

	if (infoptr == ((struct outsegyinfo *) NULL)) {
		/* initialize new segy output stream */

		/* allocate new segy output information table */
		*oldinfoptr = (struct outsegyinfo *)
			malloc(sizeof(struct outsegyinfo));
		infoptr = *oldinfoptr;
		infoptr->nextinfo = (struct outsegyinfo *) NULL;
		/* save FILE * ptr */
		infoptr->outfp = fp;
		infoptr->itr = 0;

		switch (infoptr->ftype = filestat(fileno(fp))) {
		case DIRECTORY:
			suerr("%s: segy output can't be a directory", __FILE__);
		case TTY:
			suerr("%s: segy output can't be tty", __FILE__);
		default:
			/* the rest are ok */
		break;
		}

		/* Sanity check the segy header */
		infoptr->nsfirst = tp->ns;
		if (infoptr->nsfirst > SU_NFLTS)
			suerr("%s: unable to handle %d > %d samples per trace",
			    __FILE__, infoptr->nsfirst, SU_NFLTS);
		switch(tp->trid) {
		case CHARPACK:
			infoptr->bytesper = sizeof(char); break;
		case SHORTPACK:
			infoptr->bytesper = 2*sizeof(char); break;
		default:
			infoptr->bytesper = sizeof(float); break;
		}

/*--------------------------------------------------------------------*\
   Write out a line header if it has been set as the default or has 
   requested on the caommandline.  Commandline takes precedence over
   the default in all cases.

   Reginald H. Beardsley                            [email protected]
\*--------------------------------------------------------------------*/
              
/* commented out 15.05.09 WWS 

                getparint( "lheader" ,&out_line_hdr );

                if( out_line_hdr ){

                   if( in_line_hdr ){
                     (void) efwrite(&(su_text_hdr[0]), 1 ,3200 
                                    ,infoptr->outfp);

                   }else{
                     memset( su_text_hdr ,0 ,sizeof(su_text_hdr) );
                     sprintf( hdr_str ,"%-80s" 
                            ,"C 1 CLIENT CWP/SU default text header " );
                     strncat( su_text_hdr ,hdr_str ,80 );
                     for( i=1; i<40; i++ ){
                        sprintf( hdr_str ,"%-80s" ,"C" );
                        strncat( su_text_hdr ,hdr_str ,80 );
                     }
                     (void) efwrite(&(su_text_hdr[0]), 1 ,3200 
                                    ,infoptr->outfp);


                   }

                   memset( &su_binary_hdr ,0 ,sizeof(su_binary_hdr) );
                   su_binary_hdr.format = 1;
                   su_binary_hdr.hns = tp->ns;
                   su_binary_hdr.hdt = tp->dt;
 
 
                  (void) efwrite(&su_binary_hdr, 1
                              ,sizeof(su_binary_hdr), infoptr->outfp);
                }
*/
		
	}

	if (tp->ns != infoptr->nsfirst && fixed_length)
		suerr("%s: on trace #%ld, number of samples in header (%d) "
		    "differs from number for first trace (%d)", 
		    __FILE__, (infoptr->itr)+1, tp->ns, infoptr->nsfirst);
	

	(void) efwrite(tp, 1,HDRBYTES, infoptr->outfp);
	datawrite(tp, infoptr, fixed_length);
	
	++infoptr->itr;
	lastfp = infoptr->outfp;
}
Exemplo n.º 9
0
void fputtr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length)
{
        unsigned int databytes;         /* bytes from nsfirst           */
        int nwritten;                   /* bytes seen by fwrite calls   */
	
	/* search linked list for possible alternative */
	if(fp != lastfp)  searchlist(fp);
	
	if (infoptr == ((struct outsegyinfo *) NULL)) {
		/* initialize new segy output stream */
		
		/* allocate new segy output information table */
		*oldinfoptr = (struct outsegyinfo *)
			malloc(sizeof(struct outsegyinfo));
		infoptr = *oldinfoptr;
		infoptr->nextinfo = (struct outsegyinfo *) NULL;
		/* save FILE * ptr */
		infoptr->outfp = fp;
		infoptr->itr = 0;
		/* allocate XDR struct and associate FILE * ptr */
		infoptr->segy_xdr = (XDR *) malloc(sizeof(XDR));
		
		switch (infoptr->ftype = filestat(fileno(fp))) {
		case DIRECTORY:
			suerr("%s: segy output can't be a directory", __FILE__);
		case TTY:
			suerr("%s: segy output can't be tty", __FILE__);
		break;
		default:  /* the rest are ok */
		break;
		}
		xdrstdio_create(infoptr->segy_xdr,fp,XDR_ENCODE);
		
		/* Sanity check the segy header */
		infoptr->nsfirst = tp->ns;
		if (infoptr->nsfirst > SU_NFLTS)
			suerr("%s: unable to handle %d > %d samples per trace",
			    __FILE__, infoptr->nsfirst, SU_NFLTS);

		switch(tp->trid) {
		case CHARPACK:
		   infoptr->bytesper = sizeof(char); break;
		case SHORTPACK:
		   infoptr->bytesper = 2*sizeof(char); break;
		default:
		   infoptr->bytesper = BYTES_PER_XDR_UNIT; break;
		}

	}

	databytes = infoptr->bytesper * (fixed_length?infoptr->nsfirst:tp->ns);
	if(FALSE == xdrhdrsub(infoptr->segy_xdr,tp)) 
		suerr("%s: unable to write header on trace #%ld",
		    __FILE__, (infoptr->itr)+1);
	
        nwritten = datawrite(infoptr, tp, fixed_length);

        if (nwritten != databytes)
                suerr("%s: on trace #%ld, tried to write %d bytes, "
                    "wrote %d bytes",
                    __FILE__, (infoptr->itr)+1, databytes, nwritten);
	
	++infoptr->itr;
	lastfp = infoptr->outfp;
}
Exemplo n.º 10
0
int main()
{
    
    f1.open("/Users/robinmalhotra2/Desktop/csl201check.txt",std::ios::in|std::ios::out);
    
    f1.getline(s, 80, ' ');
        while (f1.good())
    {

        
        
        
       
        std::cout<<"addasd";
        if ((strlen(s)==1) && (s[0])<'9' && (s[0])>'0')
            
        {
            switch ((s[0]))
            {
                    
                case '1':
                    createandprintlist();
                    
                
                    break;
                    
                case '2':
                    sortlist();
                    break;
                    
                case '3':
                    nextinsert();
                    break;
                    
                case '4':
                    searchlist();
                    break;
                    
                case '5':
                    deletenext();
                    break;
                    
                case '6':
                    deduplicate();
                    break;
                    
                case '7':
                    mergesortthing();
                    break;
                    
                case '8':
                    credits();
                    break;
                    
                    
                    
                default:
                    break;
            }
        }
        
        
    }
    f1.close();
    
    
    
}
Exemplo n.º 11
0
static
int fgettr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length) {
   int nread;  /* bytes seen by fread calls  */
   unsigned char buf[240];  /* buffer for test for line header */

   /* search linked list for possible alternative */
   if(fp != lastfp)  searchlist(fp);

   if (infoptr == ((struct insegyinfo *) NULL)) {
	/* initialize new segy input stream */
	unsigned int databytes; /* bytes from nsfirst   */

	/* allocate new segy input information table */
	*oldinfoptr = (struct insegyinfo *)
	 malloc(sizeof(struct insegyinfo));
	infoptr = *oldinfoptr;
	infoptr->nextinfo = (struct insegyinfo *) NULL;
	infoptr->infp = fp;  /* save FILE * ptr */
	infoptr->itr = 0;
	infoptr->ntr = -1;
	
	switch (infoptr->ftype = filestat(fileno(fp))) {
	 case DIRECTORY:
	    err("%s: segy input can't be a directory", __FILE__);

	 case TTY:
	    err("%s: segy input can't be tty", __FILE__);

	 default:
	    /* all others are ok */
	    break;
	}

/*--------------------------------------------------------------------*\
   Check for the presence of a line header and set a flag if one is
   found. The decision of what to do will be delayed until the call
   to fputtr(). This allows us to accept data w/ or w/o a line
   header.

   Reginald H. Beardsley [email protected]
\*--------------------------------------------------------------------*/

	/* Attempt to get a text header */

	nread=efread(buf ,1 ,HDRBYTES ,infoptr->infp);

	switch( nread ){

	 case 0:   
	    return 0; /* no traces; trap in mains */

	 default:  

	    if (nread < HDRBYTES ){
		return 0; 

	    }else if( isascii_txt( buf ,HDRBYTES  )
		  || isebcdic_txt( buf ,HDRBYTES  ) ){
		in_line_hdr = 1;
		memcpy( su_text_hdr ,buf ,HDRBYTES );
		nread += efread(&(su_text_hdr[HDRBYTES]) ,1 
			,3200-HDRBYTES ,infoptr->infp);

	    }else{
		in_line_hdr=0;
		memcpy( tp ,buf ,HDRBYTES );

	    }
	}		

	
	if( in_line_hdr ){

	 /* Get the binary header */
	 nread = efread(&su_binary_hdr, 1, sizeof(bhed), infoptr->infp);
	 switch( nread ){
	    case 0:   
		return 0; /* no traces; trap in mains */

		default:  
		 if (nread != sizeof(su_binary_hdr)){
		    err("%s:%d bad binary header" , __FILE__ ,__LINE__ );
		 }
	 }	 

	 /* Get the first trace header */
	 nread = efread(tp, 1, HDRBYTES, infoptr->infp);
	 switch( nread ){
	    case 0:   
		return 0; /* no traces; trap in mains */

	    default:  
		if (nread != HDRBYTES){ 
		  err("%s: bad first header", __FILE__);
		}
	 }	 


	}

	/* Have the header, now for the data */
	infoptr->nsfirst = tp->ns;
	if (infoptr->nsfirst > SU_NFLTS){
	 err("%s: unable to handle %d > %d samples per trace",
		__FILE__, infoptr->nsfirst, SU_NFLTS);
	}

	switch (tp->trid) {
	 case CHARPACK:
	    infoptr->bytesper = sizeof(char); break;
	 case SHORTPACK:
	    infoptr->bytesper = 2*sizeof(char); break;
	 default:
	    infoptr->bytesper = sizeof(float); break;
	}

	databytes = infoptr->bytesper * tp->ns;

	infoptr->nsegy = HDRBYTES + databytes;


	/* Inconvenient to bump nread here; do it in the switch */
	nread = dataread(tp, infoptr, fixed_length);
	
	switch (nread) {
	 case 0:   
	    err("%s: no data on first trace", __FILE__);

	 default:  
	    if (nread != databytes){
		err("%s: first trace: " "read only %d bytes of %u",
		  __FILE__, nread, databytes);

	    }else{
		nread += HDRBYTES;
	    }
	}
	
	
	if (infoptr->ftype == DISK) { /* compute ntr */
	 efseeko(fp, (off_t) 0LL,SEEK_END);

	 if( in_line_hdr ){
	    infoptr->ntr = (eftello(fp)-3600)/infoptr->nsegy;
	    efseeko(fp, (off_t) 3600+infoptr->nsegy,SEEK_SET);

	 }else{
	    infoptr->ntr = eftello(fp)/infoptr->nsegy;
	    efseeko(fp, (off_t) infoptr->nsegy ,SEEK_SET);

	 }
	 
	}

   }else{

	/* not first trace */

/*--------------------------------------------------------------------*\
   A number of programs seek on the input file using either fseek(3c)
   or rewind(3c) and then expect to read trace data.  As a consequence
   we need to check and offset the filepointer if needed.
\*--------------------------------------------------------------------*/

	if( in_line_hdr && ftello( infoptr->infp ) == 0 ){

	 fseeko( infoptr->infp ,(off_t)3600L ,SEEK_SET );

	}

	nread = (int) efread(tp, 1, HDRBYTES, infoptr->infp);

	switch( nread ){

	 case 0: 
	    lastfp = infoptr->infp;
	    return 0; /* finished */

	 default:  
	    if (nread != HDRBYTES){
		err("%s: on trace #%ld read %d bytes expected %d bytes",
		  __FILE__,(infoptr->itr)+1,nread,HDRBYTES);
	    }
	}

	nread += dataread(tp, infoptr, fixed_length);

	if (fixed_length && (tp->ns != infoptr->nsfirst)){
	 err("%s: on trace #%ld number of samples in header (%d) differs from number for first trace (%d)"
		,__FILE__, (infoptr->itr)+1, tp->ns,

		infoptr->nsfirst);
	}
   }

   ++(infoptr->itr);
   lastfp = infoptr->infp;
   return (nread);
}
Exemplo n.º 12
0
static
int fgettr_internal(FILE *fp, segy *tp, cwp_Bool fixed_length)
{
	int nread;		/* bytes seen by fread calls	*/
	off_t curoff;

	if(fp != lastfp)  /* search linked list for possible alternative */
			    searchlist(fp);

	if (infoptr == ((struct insegyinfo *) NULL)) {
	/* initialize new segy input stream */
		unsigned int databytes;	/* bytes from nsfirst		*/

		/* allocate new segy input information table */
		*oldinfoptr = (struct insegyinfo *)
			malloc(sizeof(struct insegyinfo));
		infoptr = *oldinfoptr;
		infoptr->nextinfo = (struct insegyinfo *) NULL;
		/* save FILE * ptr */
		infoptr->infp = fp;
		infoptr->itr = 0;
		infoptr->ntr = -1;
		/* allocate XDR struct and associate FILE * ptr */
		infoptr->segy_xdr = (XDR *) malloc(sizeof(XDR));

		switch (infoptr->ftype = filestat(fileno(fp))) {
		case DIRECTORY:
			err("%s: segy input can't be a directory", __FILE__);
		case TTY:
			err("%s: segy input can't be tty", __FILE__);
		break;
		default: /* the rest are ok */
		break;
		}
		/* xdrstdio_create(infoptr->segy_xdr,fp,XDR_DECODE); */
		infoptr->buf = ealloc1(sizeof(segy),sizeof(char));
		xdrmem_create(infoptr->segy_xdr, infoptr->buf, sizeof(segy), XDR_DECODE);
		infoptr->bufstart = xdr_getpos(infoptr->segy_xdr);

		/* retrieve segy trace header */
		nread = efread(infoptr->buf ,1 ,HDRBYTES ,infoptr->infp);

		if(nread != HDRBYTES || FALSE == xdrhdrsub(infoptr->segy_xdr,tp))
			err("%s: bad first header", __FILE__);
		
		/* Have the header, now for the data */
		infoptr->nsfirst = tp->ns;
		if (infoptr->nsfirst > SU_NFLTS)
			err("%s: unable to handle %d > %d samples per trace",
				    __FILE__, infoptr->nsfirst, SU_NFLTS);

		switch(tp->trid) {
		case CHARPACK:
		   infoptr->bytesper = sizeof(char); break;
		case SHORTPACK:
		   infoptr->bytesper = 2*sizeof(char); break;
		default:
		   infoptr->bytesper = BYTES_PER_XDR_UNIT; break;
		}

		databytes = infoptr->bytesper * tp->ns;

		infoptr->nsegy = databytes + HDRBYTES;

		nread = dataread(infoptr, tp, fixed_length);


		switch (nread) {
		case 0:   err("%s: no data on first trace", __FILE__);
		default:  if (nread != databytes)
				 err("%s: first trace: tried to read %d bytes "
				     "read %d bytes",
					__FILE__, databytes, nread);
			else nread += HDRBYTES;
		}

		if(infoptr->ftype == DISK) { /* compute ntr */
		    curoff = eftello(fp); 
		    efseeko(fp,(off_t) 0,SEEK_END);
		    infoptr->ntr = eftello(fp)/infoptr->nsegy;
		    efseeko(fp, curoff, SEEK_SET); /* restore location */
		    }


	} else { /* Not first entry */

		  xdr_setpos(infoptr->segy_xdr, infoptr->bufstart);
		  nread = efread(infoptr->buf ,1 ,HDRBYTES ,infoptr->infp);
		  if ( nread != HDRBYTES || FALSE == xdrhdrsub(infoptr->segy_xdr,tp)) nread=0;
		  if(nread == HDRBYTES)
			nread += dataread(infoptr, tp, fixed_length);
		  if (nread <= 0) {
			   lastfp = infoptr->infp;
			   return 0;
		  }



		if (fixed_length && (tp->ns != infoptr->nsfirst))
			err("%s: on trace #%ld, "
			    "number of samples in header (%d) "
			    "differs from number for first trace (%d)", 
			     __FILE__, infoptr->itr, tp->ns, infoptr->nsfirst);
	}

	++(infoptr->itr);
	lastfp = infoptr->infp;
	return (nread);
}
Exemplo n.º 13
0
int
pickfilter(RSTATUS *prs, CANDIDATE *pc, FSTATUS *pfs)
{
	register char **	pp;
	register char **	pl;

	register PSTATUS *	pps		= pc->pps;

	char *			pipes[2]	= { 0 , 0 };
	char *			cp;
	char *			output_type;

	char **			modes		= 0;
	char **			parms		= 0;
	char **			valid_printer_types;
	char **			p_cpi		= 0;
	char **			p_lpi		= 0;
	char **			p_pwid		= 0;
	char **			p_plen		= 0;

	FILTERTYPE		ret		= fl_none;

	int			got_cpi		= 0;
	int			got_lpi		= 0;
	int			got_plen	= 0;
	int			got_pwid	= 0;
	int			must_have_filter= 0;

	unsigned long		chk;


	/* fix for bugid 1097387	*/
	output_type = (char *) NULL;

	/*
	 * The bulk of the code below is building a parameter list "parms"
	 * to send to "insfilter()".
	 */

	if (prs->request->modes) {
		cp = Strdup(prs->request->modes);
		transform_WS_to_SEP(cp);
		modes = getlist(cp, "", LP_SEP);
		Free (cp);
	}

	pp = parms = (char **)Malloc(
		2 * (NPARM_SPEC + lenlist(modes) + 1) * sizeof(char *)
	);

	/*
	 * Add to the parameter list the appropriate cpi/lpi/etc.
	 * characteristics (aka ``stuff'') that will be used for
	 * this job. The printer defaults are questionable.
	 * Take this opportunity to also save the ``stuff'' in
	 * the request structure.
	 */

	unload_str (&(prs->cpi));
	unload_str (&(prs->lpi));
	unload_str (&(prs->plen));
	unload_str (&(prs->pwid));

	/*
	 * If a form is involved, pick up its page size and print
	 * spacing requirements.
	 */
	if (pfs) {
		if (pfs->cpi) {
			*pp++ = PARM_CPI;
			*pp++ = prs->cpi = pfs->cpi;
			got_cpi = 1;
		}
		if (pfs->lpi) {
			*pp++ = PARM_LPI;
			*pp++ = prs->lpi = pfs->lpi;
			got_lpi = 1;
		}
		if (pfs->plen) {
			*pp++ = PARM_LENGTH;
			*pp++ = prs->plen = pfs->plen;
			got_plen = 1;
		}
		if (pfs->pwid) {
			*pp++ = PARM_WIDTH;
			*pp++ = prs->pwid = pfs->pwid;
			got_pwid = 1;
		}

	/*
	 * If no form is involved, pick up whatever page size and print
	 * spacing requirements were given by the user.
	 */
	} else {
		if (o_cpi) {
			*pp++ = PARM_CPI;
			*pp++ = prs->cpi = o_cpi;
			got_cpi = 1;
		}
		if (o_lpi) {
			*pp++ = PARM_LPI;
			*pp++ = prs->lpi = o_lpi;
			got_lpi = 1;
		}
		if (o_length) {
			*pp++ = PARM_LENGTH;
			*pp++ = prs->plen = o_length;
			got_plen = 1;
		}
		if (o_width) {
			*pp++ = PARM_WIDTH;
			*pp++ = prs->pwid = o_width;
			got_pwid = 1;
		}
	}

	/*
	 * Pick up whatever page size and print spacing requirements
	 * haven't been specified yet from the printer defaults.
	 *
	 * Note: The following cpi/lpi/etc are guaranteed to work
	 * for at least one type of the printer at hand, but not
	 * necessarily all types. Once we pick a type that works
	 * we'll verify that the cpi/lpi/etc stuff works, too.
	 * The code that does that assumes that we do the following last,
	 * after picking up the form and/or user stuff. If this changes,
	 * then the later code will have to be changed, too.
	 */
	if (!got_cpi && pps->cpi) {
		*pp++ = PARM_CPI;
		*(p_cpi = pp++) = prs->cpi = pps->cpi;
	}
	if (!got_lpi && pps->lpi) {
		*pp++ = PARM_LPI;
		*(p_lpi = pp++) = prs->lpi = pps->lpi;
	}
	if (!got_plen && pps->plen) {
		*pp++ = PARM_LENGTH;
		*(p_plen = pp++) = prs->plen = pps->plen;
	}
	if (!got_pwid && pps->pwid) {
		*pp++ = PARM_WIDTH;
		*(p_pwid = pp++) = prs->pwid = pps->pwid;
	}

	/*
	 * Pick up the number of pages, character set (the form's
	 * or the user's), the form name, the number of copies,
	 * and the modes.
	 */

	if (prs->request->pages) {
		*pp++ = PARM_PAGES;
		*pp++ = prs->request->pages;
		must_have_filter = 1;
	}

	if (prs->request->charset) {
		*pp++ = PARM_CHARSET;
		*pp++ = prs->request->charset;

	} else if (pfs && pfs->form->chset) {
		*pp++ = PARM_CHARSET;
		*pp++ = pfs->form->chset;
	}

	if (prs->request->form) {
		*pp++ = PARM_FORM;
		*pp++ = prs->request->form;
	}

	if (prs->request->copies > 1) {
		*pp++ = PARM_COPIES;
		sprintf ((*pp++ = BIGGEST_NUMBER_S), "%d", prs->request->copies);
	}

	if (modes) {
		for (pl = modes; *pl; pl++) {
			*pp++ = PARM_MODES;
			*pp++ = *pl;
		}
		must_have_filter = 1;
	}

	*pp = 0;	/* null terminated list! */


	/*
	 * If the printer type(s) are not ``unknown'', then include
	 * them as possible ``output'' type(s) to match
	 * with the user's input type (directly, or through a filter).
	 */
	if (!STREQU(*(pps->printer->printer_types), NAME_UNKNOWN))
		valid_printer_types = pc->printer_types;
	else {
		valid_printer_types = 0;
		must_have_filter = 0;
	}

	pc->fast = 0;
	pc->slow = 0;
	pc->output_type = 0;
	pc->flags = 0;
	ret = fl_none;

	/*
	 * If we don't really need a filter and the types match,
	 * then that's good enough. Some ``broadly defined''
	 * filters might match our needs, but if the printer
	 * can do what we need, then why pull in a filter?



	 * Besides, Section 3.40 in the requirements imply
	 * that we don't use a filter if the printer can handle
	 * the file.
	 */
	if (!must_have_filter ) {

		if (
			valid_printer_types
		     && searchlist_with_terminfo(
				prs->request->input_type,
				valid_printer_types
			)
		) {
			ret = fl_both;	/* not really, but ok */
			pc->printer_type = Strdup(prs->request->input_type);

		} else if (
			pps->printer->input_types
		     && searchlist_with_terminfo(
				prs->request->input_type,
				pps->printer->input_types
			)
		) {
			ret = fl_both;	/* not really, but ok */

			/*
			 * (1) There is one printer type, might even
			 *     be ``unknown'';
			 * (2) There are several printer types, but that
			 *     means only one input type, ``simple'',
			 *     which any of the printer types can handle.
			 */
			pc->printer_type = Strdup(*(pc->printer_types));

		}
	}

	/*
	 * Don't try using a filter if the user doesn't want
	 * a filter to be used! He or she would rather see an
	 * error message than (heaven forbid!) a filter being
	 * used.
	 */
	if (ret == fl_none && !(prs->request->actions & ACT_RAW)) {

		/*
		 * For each printer type, and each input type the printer
		 * accepts, see if we have a filter that matches the
		 * request to the printer. Each time we try, save the
		 * output type we use in case of success; we just might
		 * need that value later....
		 */

		for (
			pl = valid_printer_types;
			pl && *pl && ret == fl_none;
			pl++
		)
			ret = insfilter(
				pipes,
				prs->request->input_type,
				(output_type = *pl),
				*pl,
				pps->printer->name,
				parms,
				&(pc->flags)
			);
		if (ret != fl_none)
			pc->printer_type = Strdup(*pl);

		for (
			pl = pps->printer->input_types;
			pl && *pl && ret == fl_none;
			pl++
		)
			/*
			 * Don't waste time with check we've already made.
			 */
			if ((must_have_filter == 1) ||
				!valid_printer_types
			     || !searchlist(*pl, valid_printer_types)
			)
				/*
				 * Either we have one (or less) printer
				 * types and many input types, or we have
				 * one input type, ``simple''; regardless,
				 * using the first printer type is OK.
				 */
				ret = insfilter(
					pipes,
					prs->request->input_type,
					(output_type = *pl),
					*(pc->printer_types),
					pps->printer->name,
					parms,
					&(pc->flags)
				);
		if (ret != fl_none)
			pc->printer_type = Strdup(*(pc->printer_types));

	}

	/*
	 * If we were successful, check that the printer type
	 * we picked can handle the PRINTER'S cpi/lpi/etc. defaults.
	 * (We know that ALL the printer's types can handle the stuff
	 * the user gave or the stuff in the form.)
	 * Each printer's default that doesn't pass muster gets dropped.
	 * This may mean re-instantiating the filter(s) (if any).
	 */
	if (ret != fl_none && (p_cpi || p_lpi || p_pwid || p_plen)) {

#define	NZ(X)	((X)? *(X) : (char *)0)
		chk = chkprinter(
			pc->printer_type,
			NZ(p_cpi),
			NZ(p_lpi),
			NZ(p_plen),
			NZ(p_pwid),
			(char *)0
		);

		if (chk) {
			register char **	_pp;

			char *			hole	= "";


			/*
			 * Remove the offending printer defaults from the
			 * request list of cpi/lpi/etc. stuff, and punch
			 * (non-null!) holes in the parameter list.
			 */
#define DROP(P,R)	if (P) {P[-1] = P[0] = hole; R = 0;} else/*EMPTY*/
			if (chk & PCK_CPI)	DROP (p_cpi, prs->cpi);
			if (chk & PCK_LPI)	DROP (p_lpi, prs->lpi);
			if (chk & PCK_WIDTH)	DROP (p_pwid, prs->pwid);
			if (chk & PCK_LENGTH)	DROP (p_plen, prs->plen);

			/*
			 * If there are filters, we have to re-instantiate
			 * them. (Can't check "ret" here, because it may
			 * be misleading.)
			 */
			if (pipes[0] || pipes[1]) {

				/*
				 * First, close up the gaps we punched in
				 * the parameter list.
				 */
				for (pp = _pp = parms; *pp; pp++)
					if (*pp != hole)
						*_pp++ = *pp;
				*_pp = 0;

				/*
				 * Re-instantiate the filter(s). This
				 * CAN'T fail, because it is not mandatory
				 * that filters handle cpi/lpi/etc. stuff.
				 */
				ret = insfilter(
					pipes,
					prs->request->input_type,
					output_type,
					pc->printer_type,
					pps->printer->name,
					parms,
					&(pc->flags)
				);
			}
		}
	}

	/*
	 * Save the filters, if any. Note: although "ret" can be
	 * misleading, i.e. set to "fl_both" when there really aren't
	 * any filters, the declaration of "pipes" ensured they'd be
	 * zero if not set.
	 */
	if (ret == fl_both || ret == fl_slow)
		pc->slow = pipes[0];
	if (ret == fl_both || ret == fl_fast)
		pc->fast = pipes[1];

	if (ret != fl_none)
		pc->output_type = Strdup (output_type);

	/*
	 * Wait until now to allocate storage for the cpi/etc.
	 * stuff, to make life easier above.
	 */
	if (prs->cpi)	prs->cpi = Strdup(prs->cpi);
	if (prs->lpi)	prs->lpi = Strdup(prs->lpi);
	if (prs->plen)	prs->plen = Strdup(prs->plen);
	if (prs->pwid)	prs->pwid = Strdup(prs->pwid);


	if (parms)
		Free ((char *)parms);
	if (modes)
		freelist (modes);

	return ((ret != fl_none));
}
Exemplo n.º 14
0
int main(int argc, char* argv[])
{
    int sockfd, clientfd,status,yes=1,fdmax,p,fd,j;
    unsigned int port;
    struct addrinfo hints;
    struct addrinfo *servinfo;
    struct sockaddr_in* clientinfo;  // will point to the results
    fd_set master,read_fd;
    
    memset(&hints, 0, sizeof hints); // make sure the struct is empty
    FD_ZERO(&master);
    FD_ZERO(&read_fd);
    
    if (argc != 4) {
    printf("usage: ./server ip port clientcount\n");
    return 1;
    }
    
    port =atoi(argv[2]);
    printf("IP Entered,%s\n",argv[1]);                                
    servinfo= (struct addrinfo*)calloc(1,sizeof(struct addrinfo));
    InitAddress (argv[1], port, servinfo);
    
    MAX_COUNT = atoi(argv[3]);                               //Max number of clients
    
    // create userlist
    userlist = initlist();
     
    // make a socket:
    if ((sockfd = socket(servinfo->ai_family, servinfo->ai_socktype,servinfo->ai_protocol)) == -1) {
        perror("server: socket");
	exit(0);
    }
    // local addresses are reused
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1) {
        perror("setsockopt");
        exit(1);
    }
    // binding to a port
    if (bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
        close(sockfd);
        perror("server: bind");
    }
    //listen
    if(listen(sockfd,MAX_COUNT)==-1){          
      close(sockfd);
      perror("server:listen");
    }

    FD_SET(sockfd,&master);
    fdmax = sockfd;
   
    while(1){   
      
      read_fd = master;
      if( select(fdmax+1,&read_fd,NULL,NULL,NULL) == -1){
	  perror("server:select");
	  exit(0);
      }
      for(p=0;p<=fdmax;p++)
      {
	
	if(FD_ISSET(p,&read_fd)){
	  if(p==sockfd){ // New connection
	      int clientfd = handle_new_cnt(p,master,&fdmax);  
	      if(clientfd)
	         FD_SET(clientfd,&master);
	   } else {// received some msg from old connection
	      if(!handle_old_cnt(p,master,fdmax,sockfd))
	      {
		char usrname[17];
		if(searchlist(p,userlist,usrname)) userlist=deletelist(p,userlist);;      //closing the user incase he has quit
		printf("\nNumber of valid users\t:%d\n",list_size(userlist));
		close(p);
		FD_CLR(p,&master);
	      }
	       
	  }
	 } //data handling done
	}//data for particular p over
      }// infinite loop 
    
    
    close(sockfd);
    //printsockaddr((struct sockaddr_in*)servinfo->ai_addr);
    freeaddrinfo(servinfo); // free the linked-list

}