L-39 MCS 275 Mon 16 Apr 2001

Below are the listings for the programs we discussed in class.
/* L-39 MCS 275 Mon 16 Apr 2001 : personalized greetings */

/* The UNIX command "whoami" displays the effective username.
   The program below displays a personalized greetings,
   calling whoami.  It will not work on a Windows machine.

   Note that results of printf() statements is first put in 
   a buffer before it goes to screen.  Only if the buffer is
   full (or when followed by a scanf() statement) will the
   content be displayed.  Without flushing the buffer, the
   program below prints 
   <user name> <new line> Hello . */

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

int main()
{
   printf("Hello ");
   fflush(stdout);        /* flush buffered output */
   system("whoami");

   return 0;
}
The second topic is a more informative graceful fopen(), first a basic version :

/* L-39 MCS 275 Mon 16 Apr 2001 : dir after unsuccessful fopen */

/* The command "dir" on UNIX and Windows machines displays the
   content of the current directory. 
   A graceful open of a file for reading could, when unsuccessful,
   display the content of the current directory. */

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

int main()
{
   char filename[80];
   FILE *infile;

   printf("\nGraceful opening of a file for reading.\n");
   printf("Give file name : "); scanf("%s", filename);

   infile = fopen(filename,"r");

   if (infile != NULL)
      printf("\nFile successfully opened for reading.  Bye.\n\n");
   else
   {
      printf("\nFile \"%s\" not found.  ", filename);
      printf("Type one of the following names :\n \n");
      fflush(stdout);
      system("dir");
      printf("\n");
   }

   return 0;
}
and here a bit more advanced version, note the use of tmpnam to generate a temporary file name :
/* L-39 MCS 275 Mon 16 Apr 2001 : fopen with matching file names */

/* The command "dir" on UNIX and Windows machines displays the
   content of the current directory. 
   A graceful open of a file for reading could, when unsuccessful,
   look for matching file names in the current directory. */

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

#define MAX 80

int match ( char *s1, char *s2 );
/* returns the number of matching characters between s1 and s2 */

int match_files ( char *dirfile, char *s );
/* searches the file with name dirfile for strings that have at 
   least 3 characters in common (and at the same location) with s;
   each time a matching string is found it is printed on screen;
   the number of matching string is returned. */

int main()
{
   char filename[MAX], tmpfile[MAX], command[MAX];
   FILE *infile;
   int cnt;

   printf("\nGraceful opening of a file for reading.\n");
   printf("Give file name : "); scanf("%s", filename);

   infile = fopen(filename,"r");

   if (infile != NULL)
      printf("\nFile successfully opened for reading.  Bye.\n\n");
   else
   {
      printf("\nFile \"%s\" not found.  ", filename);
      printf("Looking for names that match...\n");

      tmpnam(tmpfile);                /* create temporary file name */

      sprintf(command,"dir > %s", tmpfile);      /* prepare command */
      system(command);                           /* execute command */
      cnt = match_files(tmpfile,filename);       /* look for matches */
      if (cnt == 0)
         printf("No matching files found.\n");
      remove(tmpfile);                           /* remove temp file */
   }

   return 0;
}

int match ( char *s1, char *s2 )
{
   int cnt = 0;
   int i;

   for (i = 0; s1[i] != '\0' && s2[i] != '\0'; i++)
      if (s1[i] == s2[i]) cnt++;

   return cnt;
}

int match_files ( char *dirfile, char *s )
{
   FILE *infile;
   char filename[80];
   int cnt = 0;

   infile = fopen(dirfile,"r");
   while (fscanf(infile,"%s",filename) == 1)
      if (match(filename,s) > 2)
      {
         cnt++;
         printf("  found \"%s\"\n",filename);
      }

   return cnt;
}
The last topic was on the use of environment variables, here is how you can get to the preferred type of editor :
/* L-39 MCS 275 Mon 16 Apr 2001 : getting type of editor */

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

int main()
{

   printf("Your editor is %s.\n", getenv("EDITOR"));

   return 0;
}