Annotation of coherent/d/usr/src/examples/strncpy.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #define NNAMES 512
        !             3: #define MAXLEN 60
        !             4: 
        !             5: char *array[NNAMES];
        !             6: char gname[MAXLEN], lname[MAXLEN];
        !             7: extern int strncmp(), strcomp();
        !             8: extern char *strcpy(), *strncpy(), *strncat(), *index();
        !             9: 
        !            10: main(argc, argv)
        !            11: int argc; char *argv[];
        !            12: {
        !            13:        FILE *fp;
        !            14:        register int count, num;
        !            15:        register char  *name, string[60], *cptr, *eptr;
        !            16:        unsigned glength, length;
        !            17: 
        !            18:        if (argc != 2) {
        !            19:                fprintf (stderr, "Usage: strnmcpy filename\n");
        !            20:                exit(1);
        !            21:        }
        !            22: 
        !            23:        if ((fp = fopen(argv[1], "r")) == NULL) {
        !            24:                printf("Cannot open %s\n", argv[1]);
        !            25:        }
        !            26:        count = 0;
        !            27: 
        !            28:        while (fgets(string, 60, fp) != NULL) {
        !            29:                if ((cptr = index(string, '.')) != NULL) {
        !            30:                        cptr++; 
        !            31:                        cptr++;
        !            32:                } else 
        !            33:                        if ((cptr = index(string,' ')) != NULL)
        !            34:                                cptr++;
        !            35: 
        !            36:                strcpy(lname, cptr);
        !            37:                eptr = index(lname, '\n');
        !            38:                *eptr = ','; 
        !            39: 
        !            40:                strcat(lname," "); 
        !            41:                glength = (unsigned)(strlen(string) - strlen(cptr));
        !            42:                strncpy(gname, string, glength);
        !            43: 
        !            44:                name = strncat(lname, gname, glength);
        !            45:                length = (unsigned)strlen(name);
        !            46:                array[count] = malloc(length + 1);
        !            47: 
        !            48:                strcpy(array[count],name);
        !            49:                count++;
        !            50:        }
        !            51: 
        !            52:        for (num = 0; num < count; num++)
        !            53:                printf("%s\n", array[num]);
        !            54:        exit(0);
        !            55: }

unix.superglobalmegacorp.com

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