Passando parâmetros com getopt
Publicado por Andre Luiz da Silva 02/05/2004
[ Hits: 11.947 ]
Homepage: http://localhost
Esse é um simples código demonstrando como manipular parâmetros passados para um programa usando a função "getopt".
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
void help()
{
printf ("Olah.... eu sou o a funcao help!");
}
void verbose ()
{
printf ("Olah... eu sou a verbose!! \n");
}
int print (char *file, char *string)
{
FILE *fd;
if ((fd = fopen (file, "w")) == NULL)
{
printf ("fopen(): %s \n", strerror(errno));
exit (1);
}
fprintf (fd, "%s", string);
fclose (fd);
return 0;
}
int opterr = 0;
int main(int argc, char **argv)
{
char *file = NULL, *phase = NULL;
if (argc == 1)
{
printf ("Nao tem argumento! Falowz... \n");
exit (0);
}
while (1)
{
int opt;
opt = getopt (argc, argv, "hvp:f:");
switch (opt)
{
case 'h':
help();
break;
case 'v':
verbose();
break;
case 'p':
phase = optarg;
break;
case 'f':
file = optarg;
break;
}
if (opt == -1)
break;
if (opt == '?')
{
printf ("%c: This option don't exists!\n", optopt);
break;
}
}
if (phase != NULL && file != NULL)
{
print (file, phase);
exit(0);
}
return 0;
}
Programa para cálculo vetorial
SIMULADOR DE DADOS DE RPG - Baseado em Vampiro A Máscara - Alea Jacta Est ver2.0.1
Vários pacotes de ping disparados contra o host
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Linux em 2025: Segurança prática para o usuário
Desktop Linux em alta: novos apps, distros e privacidade marcam o sábado
IA chega ao desktop e impulsiona produtividade no mundo Linux
Novos apps de produtividade, avanços em IA e distros em ebulição agitam o universo Linux
Como instalar o repositório do DBeaver no Ubuntu
Como instalar o Plex Media Server no Ubuntu
Digitando underscore com "shift" + "barra de espaços"
Como ativar a lixeira e recuperar aquivos deletados em um servidor Linux
Como mudar o nome de dispositivos Bluetooth via linha de comando
How can I remove duplicate MSG files from my folders effectively? (0)
O programa assinador digital (0)
dpkg: erro: gatilho de arquivo duplicado chamado pelo arquivo de nome (6)









