Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Disk Usage v1.2 Bug Fix!

64 views
Skip to first unread message

ma...@xanth.uucp

unread,
Nov 20, 1987, 1:46:27 PM11/20/87
to
References:


Well for those folks who got my first VAX C program, well I had a minor
bug that just showed up. It appears that when you ran the disk usage
program the temp file was not wiped. Well this was traced to my
spawn routine. I can't believe I left it that way. O well, here
is the corrected version.

Please dont flame, Wesley Crusher is watching!

---cut here------
/* DU.C - By Mark D. Manes, System Programmer
Norfolk State University
November 18, 1987

The report generated will give percentages, byte usage, and
overall system disk usage.

If you have bigger drives than the RA81 or smaller, you need to
change the definition for RA81 to reflect the max space available
to your disk. */

/* Update #1 Nov 20, 1987 - A minor bug with the spawn routine
caused the temporary files not to be deleted. This fix modified
the spawn routine to allow it to either write to data file or not.
There are now 3 parameters to the spawn function, command,
output file, and the file switch. The file switch if set to 1 will
allow output to be redirected to a data file specified in param #2,
if it is set to zero (file_switch) then the 2nd parameter is ignored
and the command is processed with output directed at SYS$OUTPUT

A final note for this version the temporary file has been renamed
to t_disk.dt. */


#include <stdio.h>
#include <descrip.h>
#include <ssdef.h>
#include <libdef.h>
#include <string.h>
#include <math.h>

#define MAXLIN 90
#define RA81 891072 /* Max Free Blocks on a New RA81 */
#define BLOCK 512 /* Number of bytes in 1 block */

char cmd_string[MAXLIN] = "show dev d";
char out_file[10] = "t_disk.dt"; /* must use ALL 11 bytes */
char in_data[MAXLIN], f_bytes[MAXLIN];
char *disk_ptr, *dspace_ptr, *command, *form_bytes, *out_file_ptr;
char disk[5], dspace[7];
int a, d_space, used_blocks, percent_free, percent_used, free_bytes,
dummy, slen, width, blanks, file_switch;
float total_blocks_free, total_available, total_used;
float total_bytes_avail, total_bytes_used;
int count, count_space = 0;
float system_wide_usage;
FILE *in;
main()
{

/* Set up disk file for analysis, execute the SHOW DEV D command */
file_switch = 1;
a = spawn_it(cmd_string,out_file, file_switch);
if (a != SS$_NORMAL) {
printf("Command Failed %d\n",a);
exit(a);
}

in = fopen("t_disk.dt","r"); /* Open the disk file */
if (in == NULL) {
printf("File Open Failure %x\n",in);
exit(in);
}


printf(" DISK USAGE V1.2 \n");
printf(" (Individual Disk Usage)\n");
printf("\n");
printf("Disk Free Used Free Per Per Disk\n");
printf("Drive: Blocks Blocks Bytes Free Used Status\n");
printf("=============================================================================\n");

/* The 3 read lines below throw out the lines with no data */
fgets (in_data, MAXLIN, in);
fgets (in_data, MAXLIN, in);
fgets (in_data, MAXLIN, in);

/* Now do the real thing! */
while ( fgets (in_data,MAXLIN, in) != NULL) {
count++;
disk_ptr = mid(1,5,in_data); /* Get Disk Name */
strcpy (disk, disk_ptr);
dspace_ptr = mid(65,7,in_data); /* Get Free Blocks */
d_space = atoi(dspace_ptr);

/* Calculate detail line stuff */

used_blocks = RA81 - d_space;
free_bytes = d_space * BLOCK;
percent_free = (100 * d_space) / RA81;
percent_used = 100 - percent_free;

/* Get Totals! */

total_blocks_free = total_blocks_free + d_space;

/* String Formatting */
a = itoa (free_bytes, f_bytes);
form_bytes = insert_comma(f_bytes);
/* Print Detail Line */
printf("%s ",disk);
printf("%d ",d_space);
printf("%d ",used_blocks);
printf("%s ",form_bytes);
printf("%d%c ",percent_free,'%');
printf("%d%c ",percent_used,'%');
if (percent_free < 20)
printf(" -*ALERT*-\n");
else
if (percent_free < 40)
printf(" -WARNING-\n");
else
printf(" -OK-\n");
}
printf("\n - DISK USAGE SUMMARY -\n\n");

total_available = RA81 * count;
total_used = total_available - total_blocks_free;
total_bytes_avail = total_available * BLOCK;
total_bytes_used = total_used * BLOCK;
system_wide_usage = (total_used / total_available) * 100;

/* Print Final Statistics */

/* Total Blocks Available */

dummy = total_available;
itoa (dummy, f_bytes);
form_bytes = insert_comma(f_bytes);
printf("Total Blocks Available: ");
slen = strlen(form_bytes);
width = 13;
blanks = width - slen;
if (blanks > 0)
white_space (&blanks);
printf("%s",form_bytes);

/* Total Blocks Used */

dummy = total_used;
itoa (dummy, f_bytes);
form_bytes = insert_comma(f_bytes);
slen = strlen(form_bytes);
printf(" Used: ");
width = 13;
blanks = width - slen;
if (blanks > 0) {
white_space (&blanks);
}
printf("%s\n",form_bytes);

/* Total Bytes Available */

dummy = total_bytes_avail;
itoa (dummy, f_bytes);
form_bytes = insert_comma(f_bytes);
printf("Total Bytes Available: ");
slen = strlen(form_bytes);
width = 13;
blanks = width - slen;
if (blanks > 0) {
white_space (&blanks);
}
printf("%s",form_bytes);

/* Total Bytes Used */

dummy = total_bytes_used;
itoa (dummy, f_bytes);
form_bytes = insert_comma(f_bytes);
slen = strlen(form_bytes);
printf(" Used: ");
width = 13;
blanks = width - slen;
if (blanks > 0) {
white_space (&blanks);
}
printf("%s \n",form_bytes);

/* Percentages, and Good bye */

printf("\nSystem wide disk usage: %3.2f\n", system_wide_usage);
printf("\nEnd of Report\n");
printf("-------------\n");
printf("Disk Usage v1.2\nBy Mark Manes, System Programmer\n");
printf("Norfolk State University, November 13, 1987\n");
printf("This Software is Public Domain\n");

/* Clean up and say bye bye */

close(in);

/* Delete the temp file */
command = "delete t_disk.dt;*";
strcpy (cmd_string, command);
file_switch = 0; /* no out file */
a = spawn_it(cmd_string, out_file, file_switch);
if (a != SS$_NORMAL) {
printf("Command = %s\n",command);
printf("Command Failed %d\n",a);
exit(a);
}
exit(a);
};

/* End of Main Routine */



itoa (n, s) /* This routine is used to change a integer
into a string, page 60 - Kernighan / Ritchie */

char s[];
int n;
{
int i, sign;

if ((sign = n) < 0)
n = -n;
i = 0;
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}

reverse(s) /* reverse string s in place */
char s[];
{
int c, i, j;

for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}

insert_comma (string) /* This routine will take a string that is
passed to it and place comma's in every
third position, i.e. 1024 = 1,024.
Capice? */
#define COMMA ','

char string[];

{
int count, index, len, remainder = 0;
static char string_out_array[MAXLIN] = NULL;
char hold_string[MAXLIN] = NULL;
strcpy(hold_string, string);
len = strlen(string);
remainder = len % 3;
if (remainder != 0)
index = len + (len / 3);
else
index = len + (len / 3) - 1;
string_out_array[index + 1] = '\0';
count = 0;
while (len != -1) {
if (count > 3) {
string_out_array[index] = COMMA;
index--;
count = 1;
};
string_out_array [index] = hold_string[len];
len--;index--;count++;
}
return(string_out_array);
};

mid(beg,len,string) /* Extracts a string from within a string
based on the first bytes, and length */

int beg, len;
char string[];

{

char *search_line_ptr, *start;
int top = 0;
static char string_out_array[MAXLIN] = NULL;
search_line_ptr = string;
top = search_line_ptr + strlen(string);
start = (search_line_ptr + beg) -1;
if (start > top)
return(NULL);
strcpy(string_out_array, start);
string_out_array[len] = '\0';

return (string_out_array);
}

spawn_it(cmd,out,file)
/* Version 1.0 - By Mark Manes, System Programmer, Norfolk State */
/* Spawn Function calls LIB$SPAWN to execute the command passed to */
/* the 'cmd' variable, status is returned to the main program. */
/* The following include files are required: */
/* ssdef.h, libdef.h, string.h, descrip.h */

char *cmd;
char *out;
int file;

{
char command_text[MAXLIN];
$DESCRIPTOR (cmd_ptr, command_text);
char out_file[10]; /* total size of file name */
$DESCRIPTOR (out_file_ptr, out_file);
unsigned long int status = 0;

strcpy(command_text, cmd);
strcpy(out_file, out);
if (file != 0) {
status = LIB$SPAWN(&cmd_ptr,NULL,&out_file_ptr);
}
else
status = LIB$SPAWN(&cmd_ptr);


return (status);
}

white_space (x) /* This routine is used for padding */
int *x;

{
int count = 0;
char *blank = " ";
while (count++ != *x) {
printf(" ");
}

}

0 new messages