Annotation of coherent/a/usr/man/ALL/strncpy, revision 1.1.1.1

1.1       root        1: 
                      2: 
                      3: strncpy()                String Function                strncpy()
                      4: 
                      5: 
                      6: 
                      7: 
                      8: Copy one string into another
                      9: 
                     10: #include <string.h>
                     11: cchhaarr *ssttrrnnccppyy(_s_t_r_i_n_g_1, _s_t_r_i_n_g_2, _n)
                     12: cchhaarr *_s_t_r_i_n_g_1, *_s_t_r_i_n_g_2; uunnssiiggnneedd _n;
                     13: 
                     14: strncpy copies up to n bytes of string2 into string1, and returns
                     15: string1.  Copying  ends when n  bytes have been copied  or a null
                     16: character  has  been  encountered,  whichever  comes  first.   If
                     17: string2 is less than n characters in length, string2 is padded to
                     18: length n with one or more null bytes.
                     19: 
                     20: ***** Example *****
                     21: 
                     22: This example,  called sswwaapp.cc, reads a file  of names, and changes
                     23: them from the format
                     24: 
                     25: 
                     26:         first_name  [middle_initial] last_name
                     27: 
                     28: 
                     29: to the format
                     30: 
                     31: 
                     32:         last_name, first_name [middle_initial]
                     33: 
                     34: 
                     35: It demonstrates ssttrrnnccppyy, ssttrrnnccaatt, ssttrrnnccmmpp, and iinnddeexx.
                     36: 
                     37: 
                     38: #include <stdio.h>
                     39: #define NNAMES 512
                     40: #define MAXLEN 60
                     41: 
                     42: 
                     43: 
                     44: char *array[NNAMES];
                     45: char gname[MAXLEN], lname[MAXLEN];
                     46: extern int strncmp(), strcomp();
                     47: extern char *strcpy(), *strncpy(), *strncat(), *index();
                     48: 
                     49: 
                     50: 
                     51: main(argc, argv)
                     52: int argc; char *argv[];
                     53: {
                     54:         FILE *fp;
                     55:         register int count, num;
                     56:         register char  *name, string[60], *cptr, *eptr;
                     57:         unsigned glength, length;
                     58: 
                     59: 
                     60: 
                     61: 
                     62: 
                     63: 
                     64: COHERENT Lexicon                                           Page 1
                     65: 
                     66: 
                     67: 
                     68: 
                     69: strncpy()                String Function                strncpy()
                     70: 
                     71: 
                     72: 
                     73:         if (--argc != 1) {
                     74:                 fprintf (stderr, "Usage: swap filename\n");
                     75:                 exit(1);
                     76:         }
                     77: 
                     78: 
                     79: 
                     80:         if ((fp = fopen(argv[1], "r")) == NULL)
                     81:                 printf("Cannot open %s\n", argv[1]);
                     82:         count = 0;
                     83: 
                     84: 
                     85: 
                     86:         while (fgets(string, 60, fp) != NULL) {
                     87:                 if ((cptr = index(string, '.')) != NULL) {
                     88:                         cptr++;
                     89:                         cptr++;
                     90:                 } else if ((cptr = index(string,' ')) != NULL)
                     91:                         cptr++;
                     92: 
                     93: 
                     94: 
                     95:                 strcpy(lname, cptr);
                     96:                 eptr = index(lname, '\n');
                     97:                 *eptr = ',';
                     98: 
                     99: 
                    100: 
                    101:                 strcat(lname," ");
                    102:                 glength = (unsigned)(strlen(string) - strlen(cptr));
                    103:                 strncpy(gname, string, glength);
                    104: 
                    105: 
                    106: 
                    107:                 name = strncat(lname, gname, glength);
                    108:                 length = (unsigned)strlen(name);
                    109:                 array[count] = malloc(length + 1);
                    110: 
                    111: 
                    112: 
                    113:                 strcpy(array[count],name);
                    114:                 count++;
                    115:         }
                    116: 
                    117: 
                    118: 
                    119:         for (num = 0; num < count; num++)
                    120:                 printf("%s\n", array[num]);
                    121:         exit(0);
                    122: }
                    123: 
                    124: 
                    125: ***** See Also *****
                    126: 
                    127: strcpy(), string functions, string.h
                    128: 
                    129: 
                    130: COHERENT Lexicon                                           Page 2
                    131: 
                    132: 
                    133: 
                    134: 
                    135: strncpy()                String Function                strncpy()
                    136: 
                    137: 
                    138: 
                    139: 
                    140: ***** Notes *****
                    141: 
                    142: string1 must point to enough  space to n bytes; otherwise, a por-
                    143: tion of the program or operating system may be overwritten.
                    144: 
                    145: 
                    146: 
                    147: 
                    148: 
                    149: 
                    150: 
                    151: 
                    152: 
                    153: 
                    154: 
                    155: 
                    156: 
                    157: 
                    158: 
                    159: 
                    160: 
                    161: 
                    162: 
                    163: 
                    164: 
                    165: 
                    166: 
                    167: 
                    168: 
                    169: 
                    170: 
                    171: 
                    172: 
                    173: 
                    174: 
                    175: 
                    176: 
                    177: 
                    178: 
                    179: 
                    180: 
                    181: 
                    182: 
                    183: 
                    184: 
                    185: 
                    186: 
                    187: 
                    188: 
                    189: 
                    190: 
                    191: 
                    192: 
                    193: 
                    194: 
                    195: 
                    196: COHERENT Lexicon                                           Page 3
                    197: 
                    198: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.