0
        //************************************
        //Includes


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


        //************************************
        //Globals
        int packed;


        //************************************
        //Function Prototypes


        int pack_value(int value, int size, int low_bit);
        int unpack_value(int holder, int size, int low_bit);
        void get_date(int holder, int day, int month, int year);
        void get_permissions(int permission, int clas, int *read , int *write , int *execute);
        int w_permission(int clas, int *read , int *write , int *execute);
        int take_permissions(int clas,int *r, int *w, int *e);




        //***********************************
        //Main function

        int main(int argc, char** argv) 
        {

            //Variables


            int d,m,y;          //date variables

            int r,w,e;          //permission variables

            int p1,p2,p3;       //packed class variables

            int i;
            int permissions;
            int day, month, year;
            char a;




            printf("DATE:\n");


            //getting day and packing it
            printf(" Day:"); 
            scanf("%d",&day);
            d = pack_value(day,31, 0);   //31 = 5 one's on the lower bits



            //getting month and packing it
            printf(" Month:");
            scanf("%d",&month);
            m=pack_value(month,15, 5);   //15 = 4 one's on the lower bits



            //getting Year and packing it
            printf(" Year:"); 
            scanf("%d",&year);
            y=pack_value(year,4095, 10); //4095 = 12 one's on the lower bits



            //Packing date in one variable
            packed = d|m|y;




            //Taking permissins
            printf("\n\nPermissions for Owner :\n");
            p1=take_permissions(1,&r,&w,&e);




            printf("\nPermissions for Group :");
            p2=take_permissions(2,&r,&w,&e);



            printf("\nPermissions for Others :");
            p3=take_permissions(3,&r,&w,&e);




            //packing permissions
            permissions=p1|p2|p3;


            //Unpacking date
            get_date( packed, &d, &m, &y);

            //Printing Date
            printf("\n\nDATE\n day:%d\n month:%d\n year:%d\n", d, m, y);


        //*****
        //Unpacking permissins and printing them


            for(i=1;i<=3;i++)
            {

                switch(i)
                {


                case 1:
                    printf("\nPermissions for Owner :");

                    get_permissions(permissions,i,&r,&w,&e);


                      if(r==1)

                        printf("read " );
                      if(w==1)
                        printf("Write " );
                      if(e==1)
                          printf("Execute " );

                      break;


                case 2:

                    printf("\nPermissions for Group :");

                    get_permissions(permissions,i,&r,&w,&e);


                      if(r==1)

                        printf("read " );
                      if(w==1)
                        printf("Write " );
                      if(e==1)
                          printf("Execute " );

                      break;



                case 3:

                    printf("\nPermissions for Others :");

                    get_permissions(permissions,i,&r,&w,&e);


                      if(r==1)


                          printf("read " );
                      if(w==1)

                          printf("Write " );
                      if(e==1)

                          printf("Execute " );

                      break;




                }

            }


        //****

        printf("\n");

            return (EXIT_SUCCESS);
        }


        //***********************************
        //Function to pack and shift value


        int pack_value(int value, int size, int low_bit)
        {

            int bit_check;
            bit_check= value & size;
            if (bit_check == value)
                {value= value << low_bit;
            return value;}
            else
            printf("Bit Error!");
            exit(1);

        }



        //***********************************
        //Function Unpack value

        int unpack_value(int holder, int size, int low_bit)
        {
            int unpack;
            unpack= (holder>>low_bit)&size;
            return unpack;

        }



        //***********************************
        //Function to get date

        void get_date(int holder, int *day, int *month, int *year)
        {
                  //int packed_value= day|month|year;

                *day= unpack_value(holder, 31, 0);

                *month= unpack_value(holder, 15, 5);

                *year= unpack_value(holder, 4095, 10);

                //printf("DATE\n day:%d\n month:%d\n year:%d", day, month, year);
        }




        //***********************************
        //Function unpack date

        void get_permissions(int permission, int clas, int *read , int *write , int *execute)
        {


            //(Class*3) will determine how much the shift is needed

            *read= unpack_value(permission, 1, (clas*3)-3);

            *write= unpack_value(permission, 1, ((clas*3)-2));

            *execute= unpack_value(permission, 1,((clas*3)-1));





        }



        //***********************************
        //Function to write permissions

        int w_permission(int clas, int *read , int *write , int *execute)
        {


            int p1,p2,p3,per=0;

            p1  = pack_value(*read, 1, (clas*3)-3);

            p2  = pack_value(*write, 1, ((clas*3)-2));

            p3  = pack_value(*execute, 1,((clas*3)-1));

            per =p1|p2|p3;

        return per;

        }


        //***********************************
        //Function to take permissions from user

        int take_permissions(int clas,int *r, int *w, int *e)
        {


            int p1;
            char a;

            printf("\nread  (y/n):"); 

            a=getch();

            if(a=='y')
            {

                *r=1;

            }else *r=0;



            printf("\nwrite  (y/n):"); 



            a=getch();


            if(a=='y')
            {

                *w=1;

            }else *w=0;

            printf("\nexecute  (y/n):"); 

            a=getch();
            if(a=='y')
            {

                *e=1;

            }else *e=0;



            p1=w_permission(clas,r,w,e);



         return p1;

        }
        prog.c: In function ‘main’:
        prog.c:106: warning: passing argument 2 of ‘get_date’ makes integer from pointer without a cast
        prog.c:106: warning: passing argument 3 of ‘get_date’ makes integer from pointer without a cast
        prog.c:106: warning: passing argument 4 of ‘get_date’ makes integer from pointer without a cast
        prog.c:48: warning: unused variable ‘a’
        prog.c:58: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
        prog.c:65: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
        prog.c:72: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
        prog.c: At top level:
        prog.c:231: error: conflicting types for ‘get_date’
        prog.c:22: error: previous declaration of ‘get_date’ was here
        prog.c: In function ‘take_permissions’:
        prog.c:304: warning: implicit declaration of function ‘getch’
2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by Adak
0

Your prototype for getdate is this:
void get_date(int holder, int day, int month, int year);

But the function itself, says:
void get_date(int holder, int *day, int *month, int *year);

Which has the compiler giving errors to you. Make your prototype the same as your function's first line, and you'll be seeing much fewer errors and warnings.

Note that getch() is in conio.h iirc. You could use getchar(), however. (Which returns an char, but it's an int really. Confusing, I know.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.