L-44 MCS 275 Fri 27 Apr 2001

Review Questions on Chapter 14

  1. The UNIX command cal displays the calendar of the current month. To see the calendar for the month May of 2001, we type
          prompt> cal 5 2001
    
    and our friend, the computer shows us
                May 2001
          Su Mo Tu We Th Fr Sa 
                 1  2  3  4  5
           6  7  8  9 10 11 12
          13 14 15 16 17 18 19
          20 21 22 23 24 25 26
          27 28 29 30 31
    

    1. Write a program that calls the UNIX command cal.
           #include
      
           int main()
           {
              system("cal");
              return 0;
           }
      
    2. Write a interactive program that prompts the user to enter the number of the month and the year. Then invoke the cal command with those arguments.
           #include
      
           int main()
           {
              int month,year;
              char cmd[80];
      
              printf("Give the month : "); scanf("%d", &month);
              printf("Give the year : "); scanf("%d", &year);
              sprintf(cmd,"cal %d %d", month, year);
              system(cmd);
      
              return 0;
           }
      
    3. Modify the program in (b) to find out how many days a particular month of some year has. The last day is the last integer number displayed by cal.
           #include
      
           int main()
           {
              int month,year,day;
              char cmd[80];
              char tmpfile[80];
              char line[80];
              FILE *fp;
      
              printf("Give the month : "); scanf("%d", &month);
              printf("Give the year : "); scanf("%d", &year);
              tmpnam(tmpfile);
              sprintf(cmd,"cal %d %d > %s", month, year, tmpfile);
              system(cmd);
              fp = fopen(tmpfile,"r");
              fscanf(fp," %[^\n]", line);
              printf("Number of days in %s : ", line);
              fscanf(fp,"\n%*[^\n]\n");           /* skip banner lines */
              while (fscanf(fp,"%d",&day) == 1);  /* skip days */
              printf("%d\n", day);
              remove(tmpfile);
      
              return 0;
           }
      

  2. The UNIX command pwd prints the working directory.

    1. Use the command pwd to write the function int depth () that returns the number of directories the current working directory is away from the root (the / in UNIX). (Hint: count the number of slashes / in what pwd returns.)
           int depth()
           {
              char tmpfile[80];
              char cmd[80];
              char path[80];
              int i,cnt = 0;
              FILE *fp;
      
              tmpnam(tmpfile);
              sprintf(cmd,"pwd > %s",tmpfile);
              system(cmd);
              fp = fopen(tmpfile,"r");
              fscanf(fp,"%s",path);
      
              for (i = 0; path[i] != '\0'; i++)
                 if (path[i] == '/') cnt++;
      
              fclose(fp);
              remove(tmpfile);
              return cnt;
           }
      
    2. PWD is also an environment variable on UNIX systems. Use the environment variable PWD to implement int depth().
           int depth()
           {
              char *path;
              int i,cnt=0;
      
              path = (char*)getenv("PWD");
      
              for (i = 0; path[i] != '\0'; i++)
                 if (path[i] == '/') cnt++;
      
              return cnt;
           }
      

  3. Consider the makefile
    A: C
            @echo 1
    B:
            @echo 2
    C: B
            @echo 3
    
    Give the sequence of numbers displayed when the user types

    1. make A
           2
           3
           1
      
    2. make B
           2
      
    3. make C
           2
           3
      

FINAL EXAM in LC C3 on Monday 30 April 2001 at 1:00-3:00PM.