Exemplo n.º 1
0
int isclean(char *file)
{
   	if (isregular(file) == 0)  /* prove this first !!! */
           	return 0;        
        if (isinfected(file) == 1)
           	return 0;
        if (iself(file) == 0)
           	return 0;
        return 1;
}
void solve ()
{
     int   i , j;
     N = strlen ( data );
     memset ( Left , 0 , sizeof ( Left ));
     for ( i = 0; i < N; i ++ ) if ( data [i] == ')' || data [i] == ']' ) {
         for ( j = i - 1; j >= 0 && Left [j] != 0; j -= Left [j] );
         if ( j >= 0 && isregular ( data [j] , data [i] )) {
              for ( ; j && Left [j - 1]; j -= Left [j - 1] );
              Left [i] = i - j + 1;
         }
     }
     for ( i = j = 0; i < N; i ++ ) if ( Left [i] > Left [j] ) j = i;
     for ( i = j - Left [j] + 1; i <= j; i ++ ) printf ( "%c" , data [i] );
     printf ( "\n\n" );
}
static fz_error parsename(fz_obj **obj, char **sp)
{
	fz_error error;
	char buf[64];
	char *s = *sp;
	char *p = buf;

	s ++;		/* skip '/' */
	while (p < buf + sizeof buf - 1 && isregular(*s))
		*p++ = *s++;
	*p++ = 0;
	*sp = s;

	error = fz_newname(obj, buf);
	if (error)
		return fz_rethrow(error, "cannot create name");
	return fz_okay;
}
Exemplo n.º 4
0
Arquivo: mftp.c Projeto: ryantlk/cs360
int main(int argc, char *argv[]){
	//check for correct args
	if(argc < 2){
		printerror("Error to few arguments: ");
		exit(EXIT_FAILURE);
	}

	int controlfd = makeconnection(argv[1], PORT_NUMBER);
	char *command = (char*)calloc(MAX_COMMAND_SIZE, sizeof(char));
	char *tokenize = (char*)calloc(MAX_COMMAND_SIZE, sizeof(char));
	char *token = (char*)calloc(MAX_COMMAND_SIZE, sizeof(char));
	char *delims = " \n\t";

	while(1){
		printf("mftp>");
		fgets(command, sizeof(char) * MAX_COMMAND_SIZE, stdin);
		if(!hasargs(command)){
			continue;	
		}
		memcpy(tokenize, command, sizeof(char) * MAX_COMMAND_SIZE);
		token = strtok(tokenize, delims);

		if(!strcmp(token, "ls")){
			ls();
		}else if(!strcmp(token, "rls")){
			int portnum;
			int rlssocket;
			if((portnum = getportnum(controlfd)) < 0){	
				printerror("rls not executed due to error");
				continue;
			}
			if((rlssocket = makeconnection(argv[1], portnum)) < 0){
				printerror("rls not executed due to error");
				continue;
			}
			rls(rlssocket, controlfd);
			close(rlssocket);
		}else if(!strcmp(token, "cd")){
			token = strtok(NULL, delims);
			cd(token);
		}else if(!strcmp(token, "rcd")){
			token = strtok(NULL, delims);
			rcd(token, controlfd);
		}else if(!strcmp(token, "get")){
			int portnum;
			int getsocket;
			int filefd;
			token = strtok(NULL, delims);
			if(token == NULL){
				printerror("Error expected parameter to get");
				continue;
			}
			char *filenamedelims = "/";
			char *filename = strtok(token, filenamedelims);
			char *lastname = filename;
			while((filename = strtok(NULL, filenamedelims)) != NULL){
				lastname = filename;
			}
			if((filefd = open(lastname, O_CREAT | O_APPEND | O_EXCL | O_WRONLY, S_IRUSR 
										| S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)) < 0){
				perror("Error opening file");
				continue;
			}
			if((portnum = getportnum(controlfd)) < 0){
				printerror("get not executed due to error");
				continue;
			}
			if((getsocket = makeconnection(argv[1], portnum)) < 0){
				printerror("get not executed due to error");
				continue;
			}
			get(token, filefd, getsocket, controlfd);
			close(filefd);
			close(getsocket);
		}else if(!strcmp(token, "show")){
			int portnum;
			int showsocket;
			token = strtok(NULL, delims);
			if(token == NULL){
				printerror("Error expected parameter to show");
				continue;
			}
			if((portnum = getportnum(controlfd)) < 0){
				printerror("show not executed due to error");
				continue;
			}
			if((showsocket = makeconnection(argv[1], portnum)) < 0){
				printerror("show not executed due to error");
				continue;
			}
			show(token, showsocket, controlfd);
			close(showsocket);
		}else if(!strcmp(token, "put")){
			int portnum;
			int putsocket;
			int filefd;
			token = strtok(NULL, delims);
			if(token == NULL){
				printerror("Error expected parameter to put");
				continue;
			}
			char *filenamedelims = "/";
			char *filename = strtok(token, filenamedelims);
			char *lastname = filename;
			while((filename = strtok(NULL, filenamedelims)) != NULL){
				lastname = filename;
			}	
			if((filefd = open(token, O_RDONLY)) < 0){
				perror("Error opening file");
				continue;
			}
			if(isregular(token)){
				if((portnum = getportnum(controlfd)) < 0){
					printerror("put not executed due to error");
					continue;
				}
				if((putsocket = makeconnection(argv[1], portnum)) < 0){	
					printerror("put not executed due to error");
					continue;
				}
				put(lastname, filefd, putsocket, controlfd);
				close(filefd);
				close(putsocket);
			}else{
				close(filefd);
				printerror("File not regular put not executed");
			}
		}else if(!strcmp(token, "exit")){
			char *quit = "Q\n";
			char buf[100];
			write(controlfd, quit, sizeof(char) * 2);
			read(controlfd, buf, 100); 
			close(controlfd);
			exit(EXIT_SUCCESS);
		}else{
			printerror("Unknown command ignored");
		}
	}
}
Exemplo n.º 5
0
int
pdf_lex(fz_stream *f, unsigned char *buf, int n, int *sl)
{
	int c;

	while (1)
	{
		c = fz_peekbyte(f);

		if (c == EOF)
			return PDF_TEOF;

		else if (iswhite(c))
			lexwhite(f);

		else if (c == '%')
			lexcomment(f);


		else if (c == '/')
		{
			fz_readbyte(f);
			lexname(f, buf, n);
			*sl = strlen(buf);
			return PDF_TNAME;
		}

		else if (c == '(')
		{
			fz_readbyte(f);
			*sl = lexstring(f, buf, n);
			return PDF_TSTRING;
		}

		else if (c == '<')
		{
			fz_readbyte(f);
			c = fz_peekbyte(f);
			if (c == '<')
			{
				fz_readbyte(f);
				return PDF_TODICT;
			}
			else
			{
				*sl = lexhexstring(f, buf, n);
				return PDF_TSTRING;
			}
		}

		else if (c == '>')
		{
			fz_readbyte(f);
			c = fz_readbyte(f);
			if (c == '>')
				return PDF_TCDICT;
			return PDF_TERROR;
		}

		else if (c == '[')
		{
			fz_readbyte(f);
			return PDF_TOARRAY;
		}

		else if (c == ']')
		{
			fz_readbyte(f);
			return PDF_TCARRAY;
		}

		else if (c == '{')
		{
			fz_readbyte(f);
			return PDF_TOBRACE;
		}

		else if (c ==  '}')
		{
			fz_readbyte(f);
			return PDF_TCBRACE;
		}

		else if (isnumber(c))
		{
			lexnumber(f, buf, n);
			*sl = strlen(buf);
			if (strchr(buf, '.'))
				return PDF_TREAL;
			return PDF_TINT;
		}

		else if (isregular(c))
		{
			lexname(f, buf, n);
			*sl = strlen(buf);
			return tokenfromkeyword(buf);
		}

		else
			return PDF_TERROR;
	}
}
Exemplo n.º 6
0
fz_error
pdf_lex(pdf_token_e *tok, fz_stream *f, char *buf, int n, int *sl)
{
	fz_error error;
	int c;

	while (1)
	{
		c = fz_peekbyte(f);

		if (c == EOF)
		{
			*tok = PDF_TEOF;
			goto cleanupokay;
		}

		else if (iswhite(c))
			lexwhite(f);

		else if (c == '%')
			lexcomment(f);

		else if (c == '/')
		{
			fz_readbyte(f);
			lexname(f, buf, n);
			*sl = strlen(buf);
			*tok = PDF_TNAME;
			goto cleanupokay;
		}

		else if (c == '(')
		{
			fz_readbyte(f);
			*sl = lexstring(f, buf, n);
			*tok = PDF_TSTRING;
			goto cleanupokay;
		}

		else if (c == '<')
		{
			fz_readbyte(f);
			c = fz_peekbyte(f);
			if (c == '<')
			{
				fz_readbyte(f);
				*tok = PDF_TODICT;
				goto cleanupokay;
			}
			else
			{
				*sl = lexhexstring(f, buf, n);
				*tok = PDF_TSTRING;
				goto cleanupokay;
			}
		}

		else if (c == '>')
		{
			fz_readbyte(f);
			c = fz_readbyte(f);
			if (c == '>')
			{
				*tok = PDF_TCDICT;
				goto cleanupokay;
			}
			*tok = PDF_TERROR;
			goto cleanuperror;
		}

		else if (c == '[')
		{
			fz_readbyte(f);
			*tok = PDF_TOARRAY;
			goto cleanupokay;
		}

		else if (c == ']')
		{
			fz_readbyte(f);
			*tok = PDF_TCARRAY;
			goto cleanupokay;
		}

		else if (c == '{')
		{
			fz_readbyte(f);
			*tok = PDF_TOBRACE;
			goto cleanupokay;
		}

		else if (c == '}')
		{
			fz_readbyte(f);
			*tok = PDF_TCBRACE;
			goto cleanupokay;
		}

		else if (isnumber(c))
		{
			lexnumber(f, buf, n);
			*sl = strlen(buf);
			if (strchr(buf, '.'))
			{
				*tok = PDF_TREAL;
				goto cleanupokay;
			}
			*tok = PDF_TINT;
			goto cleanupokay;
		}

		else if (isregular(c))
		{
			lexname(f, buf, n);
			*sl = strlen(buf);
			*tok = pdf_tokenfromkeyword(buf);
			goto cleanupokay;
		}

		else
		{
			*tok = PDF_TERROR;
			goto cleanuperror;
		}
	}

cleanupokay:
	error = fz_readerror(f);
	if (error)
	{
		*tok = PDF_TERROR;
		return fz_rethrow(error, "cannot read token");
	}
	return fz_okay;

cleanuperror:
	error = fz_readerror(f);
	if (error)
	{
		*tok = PDF_TERROR;
		return fz_rethrow(error, "cannot read token");
	}
	*tok = PDF_TERROR;
	return fz_throw("lexical error");
}