nl.c (1a0fda2b547365c9453523592a445dfe21266d4b) | nl.c (b7cf00e8d9bcf74fcfacde9a6d3a21eb709ae279) |
---|---|
1/*- 2 * Copyright (c) 1999 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Klaus Klein. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 59 unchanged lines hidden (view full) --- 68#define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */ 69 70#define FOOTER 0 71#define BODY 1 72#define HEADER 2 73#define NP_LAST HEADER 74 75static struct numbering_property numbering_properties[NP_LAST + 1] = { | 1/*- 2 * Copyright (c) 1999 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Klaus Klein. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 59 unchanged lines hidden (view full) --- 68#define FORMAT_RZ "%0*d" /* right justified, leading zeros kept */ 69 70#define FOOTER 0 71#define BODY 1 72#define HEADER 2 73#define NP_LAST HEADER 74 75static struct numbering_property numbering_properties[NP_LAST + 1] = { |
76 { "footer", number_none }, 77 { "body", number_nonempty }, 78 { "header", number_none } | 76 { .name = "footer", .type = number_none }, 77 { .name = "body", .type = number_nonempty }, 78 { .name = "header", .type = number_none } |
79}; 80 81#define max(a, b) ((a) > (b) ? (a) : (b)) 82 83/* 84 * Maximum number of characters required for a decimal representation of a 85 * (signed) int; courtesy of tzcode. 86 */ --- 36 unchanged lines hidden (view full) --- 123static int startnum = 1; 124 125/* number of characters to be used for the line number */ 126/* should be unsigned but required signed by `*' precision conversion */ 127static int width = 6; 128 129 130int | 79}; 80 81#define max(a, b) ((a) > (b) ? (a) : (b)) 82 83/* 84 * Maximum number of characters required for a decimal representation of a 85 * (signed) int; courtesy of tzcode. 86 */ --- 36 unchanged lines hidden (view full) --- 123static int startnum = 1; 124 125/* number of characters to be used for the line number */ 126/* should be unsigned but required signed by `*' precision conversion */ 127static int width = 6; 128 129 130int |
131main(argc, argv) 132 int argc; 133 char *argv[]; | 131main(int argc, char *argv[]) |
134{ 135 int c; 136 long val; 137 unsigned long uval; 138 char *ep; 139 size_t intbuffersize, clen; 140 char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' }; 141 size_t delim1len = 1, delim2len = 1; --- 111 unchanged lines hidden (view full) --- 253 } 254 255 /* Generate the delimiter sequence */ 256 memcpy(delim, delim1, delim1len); 257 memcpy(delim + delim1len, delim2, delim2len); 258 delimlen = delim1len + delim2len; 259 260 /* Allocate a buffer suitable for preformatting line number. */ | 132{ 133 int c; 134 long val; 135 unsigned long uval; 136 char *ep; 137 size_t intbuffersize, clen; 138 char delim1[MB_LEN_MAX] = { '\\' }, delim2[MB_LEN_MAX] = { ':' }; 139 size_t delim1len = 1, delim2len = 1; --- 111 unchanged lines hidden (view full) --- 251 } 252 253 /* Generate the delimiter sequence */ 254 memcpy(delim, delim1, delim1len); 255 memcpy(delim + delim1len, delim2, delim2len); 256 delimlen = delim1len + delim2len; 257 258 /* Allocate a buffer suitable for preformatting line number. */ |
261 intbuffersize = max(INT_STRLEN_MAXIMUM, width) + 1; /* NUL */ | 259 intbuffersize = max((int)INT_STRLEN_MAXIMUM, width) + 1; /* NUL */ |
262 if ((intbuffer = malloc(intbuffersize)) == NULL) 263 err(EXIT_FAILURE, "cannot allocate preformatting buffer"); 264 265 /* Do the work. */ 266 filter(); 267 268 exit(EXIT_SUCCESS); 269 /* NOTREACHED */ 270} 271 272static void | 260 if ((intbuffer = malloc(intbuffersize)) == NULL) 261 err(EXIT_FAILURE, "cannot allocate preformatting buffer"); 262 263 /* Do the work. */ 264 filter(); 265 266 exit(EXIT_SUCCESS); 267 /* NOTREACHED */ 268} 269 270static void |
273filter() | 271filter(void) |
274{ 275 char *buffer; 276 size_t buffersize; 277 ssize_t linelen; 278 int line; /* logical line number */ 279 int section; /* logical page section */ 280 unsigned int adjblank; /* adjacent blank lines */ 281 int consumed; /* intbuffer measurement */ --- 72 unchanged lines hidden (view full) --- 354 free(buffer); 355} 356 357/* 358 * Various support functions. 359 */ 360 361static void | 272{ 273 char *buffer; 274 size_t buffersize; 275 ssize_t linelen; 276 int line; /* logical line number */ 277 int section; /* logical page section */ 278 unsigned int adjblank; /* adjacent blank lines */ 279 int consumed; /* intbuffer measurement */ --- 72 unchanged lines hidden (view full) --- 352 free(buffer); 353} 354 355/* 356 * Various support functions. 357 */ 358 359static void |
362parse_numbering(argstr, section) 363 const char *argstr; 364 int section; | 360parse_numbering(const char *argstr, int section) |
365{ 366 int error; 367 char errorbuf[NL_TEXTMAX]; 368 369 switch (argstr[0]) { 370 case 'a': 371 numbering_properties[section].type = number_all; 372 break; --- 25 unchanged lines hidden (view full) --- 398 default: 399 errx(EXIT_FAILURE, 400 "illegal %s line numbering type -- %s", 401 numbering_properties[section].name, argstr); 402 } 403} 404 405static void | 361{ 362 int error; 363 char errorbuf[NL_TEXTMAX]; 364 365 switch (argstr[0]) { 366 case 'a': 367 numbering_properties[section].type = number_all; 368 break; --- 25 unchanged lines hidden (view full) --- 394 default: 395 errx(EXIT_FAILURE, 396 "illegal %s line numbering type -- %s", 397 numbering_properties[section].name, argstr); 398 } 399} 400 401static void |
406usage() | 402usage(void) |
407{ 408 409 (void)fprintf(stderr, 410"usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n" 411" [-n format] [-s sep] [-v startnum] [-w width] [file]\n"); 412 exit(EXIT_FAILURE); 413} | 403{ 404 405 (void)fprintf(stderr, 406"usage: nl [-p] [-b type] [-d delim] [-f type] [-h type] [-i incr] [-l num]\n" 407" [-n format] [-s sep] [-v startnum] [-w width] [file]\n"); 408 exit(EXIT_FAILURE); 409} |