Ignore:
Timestamp:
Jan 19, 2007, 3:27:11 AM (19 years ago)
Author:
bird
Message:

$(APPEND) -n file line1 line2 line3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmake/kmkbuiltin/append.c

    r370 r767  
    3030#include "kmkbuiltin.h"
    3131
     32
     33/**
     34 * Prints the usage and return 1.
     35 */
     36static int usage(void)
     37{
     38    fprintf(stderr, "usage: append [-n] file [string ...]\n");
     39    return 1;
     40}
     41
     42
    3243/**
    3344 * Appends text to a textfile, creating the textfile if necessary.
     
    3647{
    3748    int i;
     49    int fFirst;
    3850    FILE *pFile;
     51    int fNewLine = 0;
    3952
    4053    g_progname = argv[0];
    4154
    4255    /*
     56     * Parse options.
     57     */
     58    i = 1;
     59    while (i < argc
     60       &&  argv[i][0] == '-'
     61       &&  argv[i][1] != '\0' /* '-' is a file */
     62       &&  strchr("n", argv[i][1]) /* valid option char */
     63       )
     64    {
     65        char *psz = &argv[i][1];
     66        do
     67        {
     68            switch (*psz)
     69            {
     70                case 'n':
     71                    fNewLine = 1;
     72                    break;
     73                default:
     74                    errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]);
     75                    return usage();
     76            }
     77        } while (*++psz);
     78        i++;
     79    }
     80
     81    /*
    4382     * Open the output file.
    4483     */
    45     if (argc <= 1)
     84    if (i >= argc)
    4685    {
    4786        errx(1, "missing filename!");
    48         fprintf(stderr, "usage: append file [string ...]\n");
    49         return 1;
     87        return usage();
    5088    }
    51     pFile = fopen(argv[1], "a");
     89    pFile = fopen(argv[i], "a");
    5290    if (!pFile)
    53         return err(1, "failed to open '%s'.", argv[1]);
     91        return err(1, "failed to open '%s'.", argv[i]);
    5492
    5593    /*
    5694     * Append the argument strings to the file
    5795     */
    58     for (i = 2; i < argc; i++)
     96    fFirst = 1;
     97    for (i++; i < argc; i++)
    5998    {
    6099        const char *psz = argv[i];
    61100        size_t cch = strlen(psz);
    62         if (i > 2)
    63             fputc(' ', pFile);
     101        if (!fFirst)
     102            fputc(fNewLine ? '\n' : ' ', pFile);
     103        else
     104            fFirst = 0;
    64105        fwrite(psz, 1, cch, pFile);
    65106    }
Note: See TracChangeset for help on using the changeset viewer.