Exemple #1
0
/*
 ****************************************************************
 *	Testa se o arquivo é XPM				*
 ****************************************************************
 */
int
is_a_XPM_file (FILE *fp)
{
	char		line[80];

	if (fngets (line, sizeof (line), fp) == NOSTR)
		return (0);

	return (streq (line, "/* XPM */"));

}	/* end is_a_XPM_file */
Exemple #2
0
/*
 ****************************************************************
 *	Escreve mensagens para um outro usuário			*
 ****************************************************************
 */
void
main (int argc, const char *argv[])
{
	int		opt;
	FILE		*utmp_fp, *dst_fp;
	const char	*cp;
	const char	*my_user_nm = NOSTR, *my_tty_nm;
	const char	*my_complete_tty_nm;
	const char	*dst_user_nm, *dst_tty_nm;
	char		*dst_complete_tty_nm;
	int		dst_user_found = 0;
	const char	*dst_tty_given;
	UTMP		utmp;
	STAT		s;
	char		line[LINESZ];

	/*
	 *	Analisa as opções
	 */
	while ((opt = getopt (argc, argv, "vH")) != EOF)
	{
		switch (opt)
		{
		    case 'v':			/* Verbose */
			vflag++;
			break;

		    case 'H':			/* Help */
			help ();

		    default:			/* Erro */
			putc ('\n', stderr);
			help ();

		}	/* end switch */

	}	/* end while */

	/*
	 *	Pequena consistência
	 */
	argv += optind; 	argc -= optind;

	if (argc < 1 || argc > 2)
		help ();

	dst_user_nm = argv[0];  dst_tty_given = dst_tty_nm = argv[1];

	if (dst_tty_given  &&  (cp = strrchr (dst_tty_nm, '/')) != NOSTR)
		dst_tty_nm = cp + 1;

	/*
	 *	Obtém o nome do próprio terminal
	 */
	if ((my_tty_nm = ttyname (2 /* stderr */)) == NOSTR)
	{
		fprintf
		(	stderr,
			"%s: Não consegui obter o nome do terminal\n",
			pgname
		);
		exit (1);
	}

	my_complete_tty_nm = my_tty_nm;

	if ((cp = strrchr (my_tty_nm, '/')) != NOSTR)
		my_tty_nm = cp + 1;

	/*
	 *	Abre o arquivo do formato UTMP
	 */
	if ((utmp_fp = fopen (utmp_nm, "r")) == NOFILE)
	{
		fprintf
		(	stderr,
			"%s: Não consegui abrir \"%s\" (%s)\n",
			pgname, utmp_nm, strerror (errno)
		);
		exit (1);
	}

	/*
	 *	Procura o <usuário> no arquivo UTMP
	 */
	while (fread (&utmp, sizeof (UTMP), 1, utmp_fp) == 1)
	{
		if (utmp.ut_name[0] == '\0')
			continue;

		if (my_user_nm == NOSTR && streq ((char *)utmp.ut_line, my_tty_nm))
			my_user_nm = strdup ((char *)utmp.ut_name);

		if (!streq ((char *)utmp.ut_name, dst_user_nm))
			continue;

		if (dst_tty_given)
		{
			if (streq ((char *)utmp.ut_line, dst_tty_nm))
				dst_user_found++;
		}
		else
		{
			dst_user_found++;

			if (dst_tty_nm == NOSTR)
				dst_tty_nm = strdup ((char *)utmp.ut_line);
		}

	}	/* end for (EVER) */

	fclose (utmp_fp);

	/*
	 *	Verifica se foi encontrado o nome do próprio usuário
	 */
	if (my_user_nm == NOSTR)
	{
		fprintf
		(	stderr,
			"Não achei a minha própria entrada em \"%s\"\n",
			utmp_nm
		);
		exit (1);
	}

	/*
	 *	Verifica se foi encontrado o nome do <usuário>
	 */
	if (dst_user_found == 0)
	{
		fprintf (stderr, "O usuário \"%s\" não está em sessão", dst_user_nm);
		if (dst_tty_nm != NOSTR)
			fprintf (stderr, " no terminal \"%s\"", dst_tty_nm);
		fprintf (stderr, "\n");
		exit (1);
	}

	/*
	 *	Verifica se foi encontrado o nome do <usuário> mais de uma vez
	 */
	if (dst_user_found > 1)
	{
		fprintf
		(	stderr,
			"O usuário \"%s\" está em sessão em mais de um terminal!\n",
			dst_user_nm
		);
#if (0)	/*************************************/
		fprintf
		(	stderr,
			"Você está conectado com \"%s\"\n",
			dst_tty_nm
		);
#endif	/*************************************/
	}

	/*
	 *	Verifica se está escrevendo para o próprio terminal
	 */
	if (streq (dst_tty_nm, my_tty_nm))
	{
		fprintf (stderr, "Escrevendo para o próprio terminal?\n");
		exit (1);
	}

#undef	DEBUG
#ifdef	DEBUG
	printf ("my_user_nm  = \"%s\", my_tty_nm  = \"%s\"\n", my_user_nm, my_tty_nm);
	printf ("dst_user_nm = \"%s\", dst_tty_nm = \"%s\"\n", dst_user_nm, dst_tty_nm);
#endif	DEBUG

	/*
	 *     Verifica se o destinatário vai conseguir escrever no meu terminal
	 */
	if (stat (my_complete_tty_nm, &s) < 0)
	{
		fprintf
		(	stderr,
			"%s: Não consegui obter o estado do terminal \"%s\" (%s)\n",
			pgname, my_complete_tty_nm, strerror (errno)
		);
		exit (2);
	}

	if ((s.st_mode & S_IWOTH) == 0)
	{
		fprintf
		(	stderr,
			"*** CUIDADO: "
			"A recepção do seu terminal está desabilitada!\n"
		);
	}

	/*
	 *	Abre o terminal do destinatário
	 */
	dst_complete_tty_nm = malloc (strlen (dst_tty_nm) + 6);

	strcpy (dst_complete_tty_nm, "/dev/");
	strcat (dst_complete_tty_nm, dst_tty_nm);

	if ((dst_fp = fopen (dst_complete_tty_nm, "w")) == NOFILE)
	{
		fprintf
		(	stderr,
			"%s: Não consegui abrir \"%s\" (%s)\n",
			pgname, dst_tty_nm, strerror (errno)
		);
		exit (1);
	}

	/*
	 *     Verifica se o destinatário está com "mesg -y"
	 */
	if (fstat (fileno (dst_fp), &s) < 0)
	{
		fprintf
		(	stderr,
			"%s: Não consegui obter o estado do terminal \"%s\" (%s)\n",
			pgname, dst_complete_tty_nm, strerror (errno)
		);
		exit (2);
	}

	if ((s.st_mode & S_IWOTH) == 0)
	{
		fprintf
		(	stderr,
			"Não tenho permissão para escrever no terminal \"%s\"\n",
			dst_complete_tty_nm
		);
		exit (1);
	}

	/*
	 *	Envia o prólogo para o destinatário
	 */
	fprintf
	(	dst_fp,
		"\a\nMENSAGEM de \"%s\" (%s):\n\n",
		my_user_nm, my_tty_nm
	);

   /***	fflush (dst_fp); ***/

	if (ferror (dst_fp))
	{
		fprintf
		(	stderr,
			"%s: Não consegui enviar o prólogo para \"%s\" (%s)\n",
			pgname, dst_tty_nm, strerror (errno)
		);
		exit (1);
	}

	/*
	 *	Prepara o "setjmp" para o encerramento
	 */
	if (setjmp (end_env))
	{
		fprintf
		(	dst_fp,
			"\nFIM da mensagem de \"%s\" (%s)\n",
			my_user_nm, my_tty_nm
		);
	
		fclose (dst_fp);
		exit (0);
	}

	/*
	 *	Prepara os sinais
	 */
	signal (SIGHUP,  on_signal);
	signal (SIGINT,  on_signal);
	signal (SIGQUIT, on_signal);

	/*
	 *	Envia a mensagem de confirmação
	 */
	fprintf
	(	stderr,
		"\aConectado com o usuário \"%s\" no terminal \"%s\"\n",
		dst_user_nm, dst_tty_nm
	);

	/*
	 *	Envia as minhas diversas linhas
	 */
	for (EVER)
	{
		if (fngets (line, LINESZ, stdin) == NOSTR)
			break;

		/*
		 *	Verifica se é uma linha de comando
		 */
		if (line[0] == '!')
		{
			system (line + 1);
			fprintf (stderr, "!\n");
			continue;
		}

		/*
		 *     Verifica se o destinatário por acaso deu "mesg -n"
		 */
		if (fstat (fileno (dst_fp), &s) < 0)
		{
			fprintf
			(	stderr,
				"%s: Não consegui obter o estado do terminal \"%s\" (%s)\n",
				pgname, dst_complete_tty_nm, strerror (errno)
			);
			exit (2);
		}
	
		if ((s.st_mode & S_IWOTH) == 0)
		{
			fprintf
			(	stderr,
				"\aNão consigo mais escrever no terminal \"%s\"\n",
				dst_complete_tty_nm
			);
			write_end ();
		}

		if (fnputs (line, dst_fp) < 0 /* (EOF) */)
		{
			fprintf
			(	stderr,
				"%s: Não consegui enviar a mensagem para \"%s\" (%s)\n",
				pgname, dst_tty_nm, strerror (errno)
			);
			exit (1);
		}

	}	/* end for (EVER) */

	write_end ();

}	/* end write */
Exemple #3
0
/*
 ****************************************************************
 *	Informa sobre o uso de disco				*
 ****************************************************************
 */
int
main (int argc, const char *argv[])
{
	int		opt;

	/*
	 *	Analisa as opções.
	 */
	while ((opt = getopt (argc, argv, "damb:.sp:P:vgNH")) != EOF)
	{
		switch (opt)
		{
		    case 'd':			/* Somente total */
			dflag++;
			break;

		    case 'a':			/* Para cada arquivo */
			aflag++;
			break;

		    case 'm':			/* Em MB */
			mflag++;
			break;

		    case 'b':			/* Tamanho do bloco */
			blkmask = getintmask (optarg);
			blkmask_given++;
			break;

		    case '.':			/* Não ignora ".old" ... */
			dotflag++;
			break;

		    case 's':			/* Padrões = 	*.[cshryv] e */
			put_s_option ();
			break;

		    case 'p':		/* Padrões de inclusão */
			put_p_option ((char *)optarg, inc_pat, &inc_pati);
			break;

		    case 'P':		/* Padrões de exclusão */
			put_p_option ((char *)optarg, exc_pat, &exc_pati);
			break;

		    case 'v':			/* Verbose */
			vflag++;
			break;

		    case 'g':			/* Debug */
			gflag++;
			break;

		    case 'N':			/* Nomes do <stdin> */
			Nflag++;
			break;

		    case 'H':			/* Help */
			help ();

		    default:			/* Erro */
			fputc ('\n', stderr);
			help ();

		}	/* end switch */

	}	/* end while */

	argv += optind;

	printf (du_title, mflag ? "MB" : "KB");

	/*
	 *	Analisa as <árvores>
	 */
	if (*argv == NOSTR) 		/* Não foram dadas <árvores> */
	{
		if (Nflag)
		{
			char		*area = alloca (512);

			while (fngets (area, 512, stdin) != NOSTR)
				proc_file (area);
		}
		else
		{
			proc_file (".");
		}
	}
	else				/* Foram dadas <árvores> */
	{
		if (Nflag)
		{
			char		*area = alloca (512);

			error ("Os argumentos supérfluos serão ignorados");

			while (fngets (area, 512, stdin) != NOSTR)
				proc_file (area);
		}
		else
		{
			for (/* vazio */; *argv; argv++)
				proc_file (*argv);
		}
	}

	/*
	 *	Se foram dados mais de uma <árvore>, dá o total geral
	 */
	if (files > 1)
		printf (du_fmt, edit_sz_value (TOTAL), "TOTAL");

	return (ret);

}	/* end du */
Exemple #4
0
obj_t php_fgets(FILE *stream, int limit) {
   /* return a bigloo string no bigger than limit, as read by fgets */  
   /* return scheme '() on EOF */
   obj_t string;
   static char *buffer = NULL;
   int actually_read = 0;
  
   /* initialize internal buffer if we haven't yet 
      XXX: this is a bona fide one time BUFFERSIZE memory leak.  
      who cares? */
   if (buffer == NULL) {
      if ((buffer = malloc( BUFFERSIZE )) == NULL) {
	 /* out of memory */
	 return BNIL;
      }
   }
   if (limit <= BUFFERSIZE) { 
      /* in this case we can use the statically allocated buffer that
	 way we can avoid allocating a limit size string for potentially
	 small line */
      if (fngets( buffer, limit, stream ) == -1) {
	 return BNIL;
      }

      actually_read = strlen( buffer );
      string = string_to_bstring_len(buffer, actually_read);
      
   } else {
      /* don't use the static buffer, limit is too big.  We use realloc
	 because, at least on my linux/glibc2 system, it behaves really
	 nicely.  GC_REALLOC_ATOMIC behaves quadratically. */
      char *bigbuf = NULL;
      int len;

      do {
	 if ((bigbuf = realloc( bigbuf, actually_read + BUFFERSIZE )) == NULL) {
	    /* out of memory */
	    return BNIL;
	 }
	 if (fngets( bigbuf + actually_read, 
		    MIN(BUFFERSIZE, limit), stream ) == -1) {
	    /* fgets says there's nothing left to read */
	    if (actually_read > 0) {
	       /* we have read something in previous iterations */
	       break;
	    } else {
	       /* we've read nothing in this iteration, the stream is at EOF */
	       free(bigbuf);
	       return BNIL;
	    }
	 }
	 /* this is how much we read this time around */
	 len = strlen( bigbuf + actually_read );
	 /* this is ho much we've read total */
	 actually_read += len;
	 /* this is how much we've got left to read before hitting the limit */
	 limit -= len;            
      } while (!((len < (BUFFERSIZE - 1)) || /* didn't fill the buffer,
						so must be finished */
		 /* filled the buffer exactly, so must be finished */
		 *(bigbuf + actually_read - 1) == '\n')
	       /* bumped up against the user's limit */
	       && limit >= 0); 
      
      string  = string_to_bstring_len(bigbuf, actually_read);
      if (bigbuf) free(bigbuf);
   }
  
   return string;
}
/*
 ****************************************************************
 *	Programa principal					*
 ****************************************************************
 */
void
main (int argc, const char *argv[])
{
	int		opt;
	char		dir_name[PATHSZ];

	/*
	 *	Analisa as opções de execução.
	 */
	while ((opt = getopt (argc, argv, pgoptions)) != EOF)
	{
		switch (opt)
		{
		    case 'I':
			if (last_search_dir >= &search_dir[INCSZ - 3])
				error ("$Excesso de opções '-I'");

			*++last_search_dir = (char *)optarg;
			break;

		    case 'u':
			uflag++;
			break;

		    case 'M':
			exit (0);

		    case 'H':
			help (0);

		    case 'N':
			Nflag++;
			break;

		    case 'v':
			vflag++;
			break;

		    default:
			putc ('\n', stderr);
			help (2);

		}	/* end switch (opt) */

	}	/* while ((opt = getopt (...))) */

	argv += optind;
	argc -= optind;

	if (Nflag)
	{
		if (*argv != NOSTR)
			error ("$Com a opção '-N' não podem ser dados argumentos");

		while (fngets (dir_name, sizeof (dir_name) - 1, stdin) != NOSTR)
		{
			if (ftw (dir_name, include) != 0)
				error ("$\"ftw (\"%s\")\" retornou -1", dir_name);
		}
	}
	else if (*argv == NOSTR)
	{
		if (ftw (".", include) != 0)
			error ("$\"ftw (\".\")\" retornou -1");
	}
	else
	{
		while (*argv != NOSTR)
		{
			if (ftw (*argv, include) != 0)
				error ("$\"ftw (\"%s\")\" retornou -1", *argv);
			argv++;
		}
	}

	exit (0);

}	/* end main */