Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have no idea where to begin to even fix this... Could someone help me please?

ID: 3679184 • Letter: I

Question

I have no idea where to begin to even fix this...

Could someone help me please?

#include   <stdio.h>
    #include   <string.h>
   
    #define MAX_PATHNAME   64
    #define MAX_BUF_LINE   80
   
    char *sample(char *, const int, const int);
   
    int main(int argc, char *argv[])
    {
        char *buf;
        double up_time, idle_time;
        int report = 0;   // Report type
        int tot_mem, used_mem, avail_mem;
        int int_up_time, dd, hh, mm, ss;
        int incr_time, end_time;
        int disk_reads, disk_writes;
        int user_time, low_user_time, sys_time;
        int i;
   
    // Parse the command line
        if(argc == 1) report = 0;   // Default report
        if(argc == 2) { // Short form report
            if((argv[1][0] != '-') || (argv[1][1] != 's')) {
                printf("Usage: sample [-s][-l increment total] ");
                exit(0);
            }
            report = 1;
        }
        if(argc == 4) { // Long report
            if((argv[1][0] != '-') || (argv[1][1] != 'l')) {
                printf("Usage: sample [-s][-l increment total] ");
                exit(0);
            }
            sscanf(argv[2], "%d", &incr_time);
            sscanf(argv[3], "%d", &end_time);
            report = 2;
        }
   
    // Part B
    // CPU type and model
        buf = sample("cpuinfo", 4, 13);
        printf("CPU info: %s ", buf);
        free(buf);
    // Version
        buf = sample("sys/kernel/osrelease", 0, 0);
        printf("Version: Linux %s ", buf);
        free(buf);
    // dd:hh:mm:ss since last booted
        buf = sample("uptime", 0, 0);
        sscanf(buf, "%lf %lf", &up_time, &idle_time);
        free(buf);
        int_up_time = (int) up_time;   // Drop fractions of sections
        dd = int_up_time / (60*60*24);   // Number of days
        int_up_time = int_up_time % (60*60*24);
        hh = int_up_time / (60*60);       // Number of hours
        int_up_time = int_up_time % (60*60);
        mm = int_up_time / 60;       // Number of minutes
        ss = int_up_time % 60;       // Number of seconds
        printf("Time since system was last booted: %d:%d:%d:%d ", dd, hh, mm, ss);
        if(report <= 0) return 0;
   
    // Part C
    // User mode time, system mode time, idle time
        buf = sample("stat", 0, 4);
        sscanf(buf, "%d %d %d", &user_time, &low_user_time, &sys_time);
        free(buf);
        printf("Total user mode time since system was last booted: %d seconds ",
           user_time/100);
        printf("Total system mode time since system was last booted: %d seconds ",
           sys_time/100);
        printf("Total idle time since system was last booted: %d seconds ",
           (int) idle_time);
    // Disk read/write requests
        buf = sample("stat", 2, 8);
        sscanf(buf, "%d", &disk_reads);
        free(buf);
        printf("Disk read requests: %d ", disk_reads);
        buf = sample("stat", 3, 8);
        sscanf(buf, "%d", &disk_writes);
        free(buf);
        printf("Disk write requests: %d ", disk_writes);
    // Context switches
        buf = sample("stat", 9, 4);
        printf("Context switches: %s ", buf);
        free(buf);
    // The time at which the system was last booted
    /*****/
    // Processes created since system was last booted
    /*****/
        if(report <= 1) return 0;
   
    // Part D
    // Amount of memory
        buf = sample("meminfo", 1, 4);
        printf("Memmory line: %s ", buf);
        sscanf(buf, "%d%d%d", &tot_mem, &used_mem, &avail_mem);
        free(buf);
        printf("Total memory: %d ", tot_mem);
    // Amount of memory currently available
        printf("Unallocated memory: %d ", avail_mem);
    // Load averages
    /*****/
        for(i=0; i<end_time; i+=incr_time) {
            buf = sample("loadavg", 0, 0);
            printf("Load avg %s ", buf);
            free(buf);
            sleep(incr_time);
        }
   
        return(0);
    }
   
   
    char *sample(char *fileName, const int lineNo, const int charNo)
    {
        char ch;
        char pathName[MAX_PATHNAME] = "/proc/";
        char *buf;
        int i, j;
        FILE *fid;
   
        strcat(pathName, fileName);
        fid = fopen(pathName, "r");
        for(i = 0; i < lineNo; i++)
            for(j=0; ;j++) {
                ch = getc(fid);   // Span leading lines
                if(ch == ' ') break;
            }
        for(i = 0; i < charNo; i++) ch = getc(fid);   // Span leading chars
        i = 0;
        ch = '';
        buf = (char *) malloc(MAX_BUF_LINE);
        while(ch != ' ') {
            ch = getc(fid);
            buf[i++] = ch;
        }
        buf[i-1] = '';
        close(fid);
        return buf;
    }
    

Explanation / Answer

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

#define MAX_PATHNAME 64
#define MAX_BUF_LINE 80

char *sample(char *, const int, const int);

int main(int argc, char *argv[])
{
char *buf;
double up_time, idle_time;
int report = 0; // Report type
int tot_mem, used_mem, avail_mem;
int int_up_time, dd, hh, mm, ss;
int incr_time, end_time;
int disk_reads, disk_writes;
int user_time, low_user_time, sys_time;
int i;

// Parse the command line
if(argc == 1) report = 0; // Default report
if(argc == 2) { // Short form report
if((argv[1][0] != '-') || (argv[1][1] != 's')) {
printf("Usage: sample [-s][-l increment total] ");
exit(0);
}
report = 1;
}
if(argc == 4) { // Long report
if((argv[1][0] != '-') || (argv[1][1] != 'l')) {
printf("Usage: sample [-s][-l increment total] ");
exit(0);
}
sscanf(argv[2], "%d", &incr_time);
sscanf(argv[3], "%d", &end_time);
report = 2;
}

// Part B
// CPU type and model
buf = sample("cpuinfo", 4, 13);
printf("CPU info: %s ", buf);
free(buf);
// Version
buf = sample("sys/kernel/osrelease", 0, 0);
printf("Version: Linux %s ", buf);
free(buf);
// dd:hh:mm:ss since last booted
buf = sample("uptime", 0, 0);
sscanf(buf, "%lf %lf", &up_time, &idle_time);
free(buf);
int_up_time = (int) up_time; // Drop fractions of sections
dd = int_up_time / (60*60*24); // Number of days
int_up_time = int_up_time % (60*60*24);
hh = int_up_time / (60*60); // Number of hours
int_up_time = int_up_time % (60*60);
mm = int_up_time / 60; // Number of minutes
ss = int_up_time % 60; // Number of seconds
printf("Time since system was last booted: %d:%d:%d:%d ", dd, hh, mm, ss);
if(report <= 0) return 0;

// Part C
// User mode time, system mode time, idle time
buf = sample("stat", 0, 4);
sscanf(buf, "%d %d %d", &user_time, &low_user_time, &sys_time);
free(buf);
printf("Total user mode time since system was last booted: %d seconds ",
user_time/100);
printf("Total system mode time since system was last booted: %d seconds ",
sys_time/100);
printf("Total idle time since system was last booted: %d seconds ",
(int) idle_time);
// Disk read/write requests
buf = sample("stat", 2, 8);
sscanf(buf, "%d", &disk_reads);
free(buf);
printf("Disk read requests: %d ", disk_reads);
buf = sample("stat", 3, 8);
sscanf(buf, "%d", &disk_writes);
free(buf);
printf("Disk write requests: %d ", disk_writes);
// Context switches
buf = sample("stat", 9, 4);
printf("Context switches: %s ", buf);
free(buf);
// The time at which the system was last booted
/*****/
// Processes created since system was last booted
/*****/
if(report <= 1) return 0;

// Part D
// Amount of memory
buf = sample("meminfo", 1, 4);
printf("Memmory line: %s ", buf);
sscanf(buf, "%d%d%d", &tot_mem, &used_mem, &avail_mem);
free(buf);
printf("Total memory: %d ", tot_mem);
// Amount of memory currently available
printf("Unallocated memory: %d ", avail_mem);
// Load averages
/*****/
for(i=0; i<end_time; i+=incr_time) {
buf = sample("loadavg", 0, 0);
printf("Load avg %s ", buf);
free(buf);
sleep(incr_time);
}

return(0);
}


char *sample(char *fileName, const int lineNo, const int charNo)
{
char ch;
char pathName[MAX_PATHNAME] = "/proc/";
char *buf;
int i, j;
FILE *fid;

strcat(pathName, fileName);
fid = fopen(pathName, "r");
for(i = 0; i < lineNo; i++)
for(j=0; ;j++) {
ch = getc(fid); // Span leading lines
if(ch == ' ') break;
}
for(i = 0; i < charNo; i++) ch = getc(fid); // Span leading chars
i = 0;
ch = '';
buf = (char *) malloc(MAX_BUF_LINE);
while(ch != ' ') {
ch = getc(fid);
buf[i++] = ch;
}
buf[i-1] = '';
close(fid);
return buf;
}

This is working fine..What's the isue u r facing and what is the one u need to implement??