VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/append.c@ 348

Last change on this file since 348 was 348, checked in by bird, 19 years ago

Added kmk_builtin_append.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
1/* $Id: append.c 348 2005-12-11 05:13:13Z bird $ */
2/** @file
3 *
4 * kMk Builtin command - append text to file.
5 *
6 * Copyright (c) 2005 knut st. osmundsen <[email protected]>
7 *
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <string.h>
28#include <stdio.h>
29#include <errno.h>
30#include "kmkbuiltin.h"
31
32/**
33 * Appends text to a textfile, creating the textfile if necessary.
34 */
35int kmk_builtin_append(int argc, char **argv, char **envp)
36{
37 int i;
38 FILE *pFile;
39
40 /*
41 * Open the output file.
42 */
43 if (argc <= 1)
44 {
45 fprintf(stderr, "append: missing filename!\n");
46 fprintf(stderr, "usage: append file [string ...]\n");
47 return 1;
48 }
49 pFile = fopen(argv[1], "a");
50 if (!pFile)
51 {
52 fprintf(stderr, "append: failed to open '%s': %s\n", argv[1], strerror(errno));
53 return 1;
54 }
55
56 /*
57 * Append the argument strings to the file
58 */
59 for (i = 2; i < argc; i++)
60 {
61 const char *psz = argv[i];
62 size_t cch = strlen(psz);
63 if (i > 2)
64 fputc(' ', pFile);
65 fwrite(psz, 1, cch, pFile);
66 }
67
68 /*
69 * Add the newline and close the file.
70 */
71 if ( fputc('\n', pFile) == EOF
72 || ferror(pFile))
73 {
74 fprintf(stderr, "append: error writing to '%s'!\n", argv[1]);
75 fclose(pFile);
76 return 1;
77 }
78 if (fclose(pFile))
79 {
80 fprintf(stderr, "append: failed to fclose '%s': %s\n", argv[1], strerror(errno));
81 return 1;
82 }
83 return 0;
84}
85
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette