COMS 3157 Advanced Programming

Index of 2024-1/code/13

Parent directory
cut-str.c
fcrop.c
fmt-io.c
ncat.c

cut-str.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char prompt[] = "Give me a line:";
    char str[6];
    char line[100];

    printf("%s\n", prompt);
    fgets(line, sizeof(line), stdin);
    printf("Original line read by fgets(): [%s]\n", line);

#ifdef CASE1 /* Using snprintf() */

    int n = snprintf(str, sizeof(str), "%s", line);
    printf("Truncated using snprintf(): [%s]\n", str);

    // snprintf() returns the number of chars it would have printed
    // (not including the null terminator) if there were no truncation
    //
    // Normally this return value isn't very useful, but if you don't use 
    // the return value, the compiler will warn about possible trancation.
    // Use -Wno-format-truncation compiler flag to disable this warning.
    //
    printf("Length of string if it hadn't been truncated: %d\n", n);

#endif
#ifdef CASE2 /* Using strncpy() INCORRECTLY */

    strncpy(str, line, sizeof(str));
    printf("Truncated using strncpy(): [%s]\n", str);

#endif
#ifdef CASE3 /* Using strncpy() correcly */

    strncpy(str, line, sizeof(str) - 1);
    str[sizeof(str) - 1] = '\0';
    printf("Truncated using strncpy(): [%s]\n", str);

#endif
}

fcrop.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int main(int argc, char **argv)
{
    if (argc != 5) {
        fprintf(stderr, "%s\n",
                "usage: fcrop <in_file> <bytes_skip> <bytes_write> <out_file>");
        exit(1);
    }

    FILE *f1 = fopen(argv[1], "rb");
    FILE *f2 = fopen(argv[4], "wb");
    assert(f1 && f2);

    int offset = atoi(argv[2]);
    int length = atoi(argv[3]);
    assert(offset >= 0 && length > 0);

    int res = fseek(f1, offset, SEEK_SET);
    assert(res == 0);

    char *buf = malloc(length);
    assert(buf);

    res = fread(buf, length, 1, f1);
    assert(res == 1);

    res = fwrite(buf, length, 1, f2);
    assert(res == 1);

    free(buf);
    fclose(f1);
    fclose(f2);
}

fmt-io.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *s = "  \t  0xffffffff  \n  ";

    unsigned int u;
    sscanf(s, "%x", &u);

    char buf[100];
    
    sprintf(buf, "%u\n", u);
    printf("%s", buf);

    snprintf(buf, 5, "%u\n", u);
    printf("%s", buf);
}

ncat.c

/*
 * ncat <file_name>
 *
 *   - reads a file line-by-line, printing them out with line numbers
 */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    if (argc != 2) {
        fprintf(stderr, "%s\n", "usage: ncat <file_name>");
        exit(1);
    }

    char *filename = argv[1];
    FILE *fp = fopen(filename, "r");
    if (fp == NULL) {
        perror(filename);
        exit(1);
    }

    char buf[100];
    int lineno = 1;
    while (fgets(buf, sizeof(buf), fp) != NULL) {
        printf("[%4d]", lineno++);
        if (fputs(buf, stdout) == EOF) {
            perror("can't write to stdout");
            exit(1);
        }
    }

    if (ferror(fp)) {
        perror(filename);
        exit(1);
    }

    fclose(fp);
    return 0;
}