source c

Mise à jour: 22 mars 2007
Version: 1.0
Author: Jean-Louis Bicquelet-Salaün
Location: http://jlbicquelet.free.fr
Copyright: (c) 2007 Jean-Louis BICQUELET-SALAÜN

seq

seq est un utilitaire écrit en c. Il donne un exemple de la gestion des arguments en c.

/************************************************************************
 * name: seq
 * description : A simple program so simulate seq in c
 * Author: jl Bicquelet
 * Version 1.0
 * Date 06/2005
 ************************************************************************/

#include 

/************************************************************************
 * Usage
 ************************************************************************/
void usage()
{
  puts("seq   version 1.0   Jean-Louis Bicquelet (c)2005");
  puts("");
  puts("seq n          display from 1 to n");
  puts("seq from to    display nombre from from to to");
  puts("seq -h         display usage");
  puts("");
  puts("from can be less than to. In that case seq decrease");

  puts("ex: seq 1 10 displays numbers 1 to 10");
  puts("ex: seq 21 10 displays numbers 21 to 10");
}

/************************************************************************
 * Main
 ************************************************************************/
void main(int argc,char *argv[])
{
  int i;
  int d,f;

  if(argc<2 || argc>3) { usage(); exit(); }

  if(argc==2)
  {
    if (strcmp(argv[1],"-h")==0) { usage(); exit(); }

    d=1;
    f=atoi(argv[1]);
  }

  if(argc==3)
  {
    d=atoi(argv[1]);
    f=atoi(argv[2]);
  }

  if(d<=f)
    { for(i=d;i<=f;i++)  printf("%i ",i); }
  else
    { for(i=d;i>=f;i--)  printf("%i ",i); }
}

Une fois compilé, l'utilisation se fait ainsi:

seq   version 1.0   Jean-Louis Bicquelet (c)2005

seq n          display from 1 to n
seq from to    display nombre from from to to
seq -h         display usage

from can be less than to. In that case seq decrease
ex: seq 1 10 displays numbers 1 to 10

le source est téléchargeable ici