Changeset 767 for trunk/src/gmake/kmkbuiltin/append.c
- Timestamp:
- Jan 19, 2007, 3:27:11 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmake/kmkbuiltin/append.c
r370 r767 30 30 #include "kmkbuiltin.h" 31 31 32 33 /** 34 * Prints the usage and return 1. 35 */ 36 static int usage(void) 37 { 38 fprintf(stderr, "usage: append [-n] file [string ...]\n"); 39 return 1; 40 } 41 42 32 43 /** 33 44 * Appends text to a textfile, creating the textfile if necessary. … … 36 47 { 37 48 int i; 49 int fFirst; 38 50 FILE *pFile; 51 int fNewLine = 0; 39 52 40 53 g_progname = argv[0]; 41 54 42 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 /* 43 82 * Open the output file. 44 83 */ 45 if ( argc <= 1)84 if (i >= argc) 46 85 { 47 86 errx(1, "missing filename!"); 48 fprintf(stderr, "usage: append file [string ...]\n"); 49 return 1; 87 return usage(); 50 88 } 51 pFile = fopen(argv[ 1], "a");89 pFile = fopen(argv[i], "a"); 52 90 if (!pFile) 53 return err(1, "failed to open '%s'.", argv[ 1]);91 return err(1, "failed to open '%s'.", argv[i]); 54 92 55 93 /* 56 94 * Append the argument strings to the file 57 95 */ 58 for (i = 2; i < argc; i++) 96 fFirst = 1; 97 for (i++; i < argc; i++) 59 98 { 60 99 const char *psz = argv[i]; 61 100 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; 64 105 fwrite(psz, 1, cch, pFile); 65 106 }
Note:
See TracChangeset
for help on using the changeset viewer.