VirtualBox

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

最後變更 在這個檔案從767是 767,由 bird 提交於 18 年 前

$(APPEND) -n file line1 line2 line3

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 2.8 KB
 
1/* $Id: append.c 767 2007-01-19 02:27:11Z 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 "err.h"
30#include "kmkbuiltin.h"
31
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
43/**
44 * Appends text to a textfile, creating the textfile if necessary.
45 */
46int kmk_builtin_append(int argc, char **argv, char **envp)
47{
48 int i;
49 int fFirst;
50 FILE *pFile;
51 int fNewLine = 0;
52
53 g_progname = argv[0];
54
55 /*
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 /*
82 * Open the output file.
83 */
84 if (i >= argc)
85 {
86 errx(1, "missing filename!");
87 return usage();
88 }
89 pFile = fopen(argv[i], "a");
90 if (!pFile)
91 return err(1, "failed to open '%s'.", argv[i]);
92
93 /*
94 * Append the argument strings to the file
95 */
96 fFirst = 1;
97 for (i++; i < argc; i++)
98 {
99 const char *psz = argv[i];
100 size_t cch = strlen(psz);
101 if (!fFirst)
102 fputc(fNewLine ? '\n' : ' ', pFile);
103 else
104 fFirst = 0;
105 fwrite(psz, 1, cch, pFile);
106 }
107
108 /*
109 * Add the newline and close the file.
110 */
111 if ( fputc('\n', pFile) == EOF
112 || ferror(pFile))
113 {
114 fclose(pFile);
115 return errx(1, "error writing to '%s'!", argv[1]);
116 }
117 if (fclose(pFile))
118 return err(1, "failed to fclose '%s'!", argv[1]);
119 return 0;
120}
121
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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