Passando parâmetros com getopt
Publicado por Andre Luiz da Silva 02/05/2004
[ Hits: 11.642 ]
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; }
Atenção a quem posta conteúdo de dicas, scripts e tal (6)
Como Configurar um Túnel SSH Reverso para Acessar Sua Máquina Local a Partir de uma Máquina Remota
Configuração para desligamento automatizado de Computadores em um Ambiente Comercial
O mínimo que você precisa saber sobre o terminal (parte 2)
O mínimo que você precisa saber sobre o terminal (parte 1)
Como iniciar uma máquina virtual do VirtualBox automaticamente no boot do LUbuntu 18 LTS
Mudar o gerenciador de login (GDM para SDDM e vice-versa) - parte 2
Como deixar as abas do Firefox mais fininhas
Mudar o gerenciador de login (GDM para SDDM)
"Tentando" fazer com que programas rodem no Wayland e no X11
Porblema com MergeList [RESOLVIDO] (10)
Melhor hospedagem em nuvem para projetos Laravel com baixo custo? [RES... (7)
Como baixar os drivers de áudio no linux mint? (7)
Como posso localizar o arquivo HTML da página inicial do GLPI dentro d... (2)
[Python] Automação de scan de vulnerabilidades
[Python] Script para analise de superficie de ataque
[Shell Script] Novo script para redimensionar, rotacionar, converter e espelhar arquivos de imagem
[Shell Script] Iniciador de DOOM (DSDA-DOOM, Doom Retro ou Woof!)
[Shell Script] Script para adicionar bordas às imagens de uma pasta