/* Copyright (c) 2003, Rene Luria aka herel <rene@luria.ch>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright notice,
 *     * this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *     * notice, this list of conditions and the following disclaimer in the
 *     * documentation and/or other materials provided with the distribution.
 *     * Neither the name of the author nor the names of its
 *     * contributors may be used to endorse or promote products derived from
 *     * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. */

/* affiche un nombre en argument avec des apostrophes pour séparer les milliers */

/* merci à Emmanuel Delahaye <emdelNOSPAM@noos.fr> pour les précisions quant au
   type long long en C99 et long en C90 */

/* merci à "Al 1" <ol1@free.xx> pour avoir identifié entre autres une erreur
   sur un free, et une variable mal utilisée */

#include <stdlib.h>
#include <stdio.h>

#if 0
/* C 99 */
typedef long long long_t ;
#define FMT "%lld"
#else
typedef long long_t ;
/* C 90 */
#define atoll(a) atol(a)
#define FMT "%ld"
#endif

#define SEP '\''

static size_t compte_chiffres (long_t nombre)
{
    size_t nb_chiffres = 1;
    for (; nombre / 10 > 0; nombre /= 10)
    {
        nb_chiffres++;
    }
    return nb_chiffres;
}

static char *virgule (long_t nombre)
{
    char *chaine;
    size_t nb_chiffres, longueur_chaine;
    long_t i;

    nb_chiffres = compte_chiffres (nombre);

    if (nb_chiffres < 4)
    {
        longueur_chaine = nb_chiffres;
    }
    else
    {
        longueur_chaine = nb_chiffres + (nb_chiffres - 1) / 3;
    }
    if ((chaine = malloc (longueur_chaine + 1)))
    {
        chaine[longueur_chaine] = '\0';
        longueur_chaine--;
        for (i = nb_chiffres; i > 0; i--)
        {
            sprintf (chaine + longueur_chaine, FMT"%s", nombre%10,
                    chaine + longueur_chaine+1);
            nombre /= 10;
            longueur_chaine--;
            if ((i > 1) && ((nb_chiffres - i + 1) % 3 == 0))
            {
                sprintf (chaine + longueur_chaine, "%c%s", SEP,
                        chaine + longueur_chaine +1);
                longueur_chaine--;
            }
        }
    }

    return chaine;
}

int main (int argc, char **argv) {
    long_t nombre;
    char *chaine;
    char chaine_default[] = "0";
    while (--argc > 0)
    {
        argv++;
        nombre = atoll (*argv);
        chaine = virgule (nombre);
        /* merci à "Al 1" <ol1@free.xx> pour cette correction
        if (chaine == NULL) {
            chaine = chaine_default;
        } */
        printf (FMT": %s\n", nombre, (chaine) ? chaine : chaine_default);
        free (chaine);
    }
    return EXIT_SUCCESS;
}