Last active
December 15, 2015 17:09
-
-
Save davidsth/5294194 to your computer and use it in GitHub Desktop.
file permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* filepermission.c | |
* | |
* Date: 11/01/2012 | |
* | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
int pack_value(int,int,int); | |
int unpack_value(int,int,int); | |
void get_date(int holder,int *a,int *b,int *c); | |
void get_permissions(int,int,int *, int *, int *); | |
int main() { | |
int day,month,year; | |
printf("Date\n"); | |
printf("Enter a day: "); | |
scanf("%d",&day); | |
printf("Enter a month: "); | |
scanf("%d",&month); | |
printf("Enter a year: "); | |
scanf("%d",&year); | |
if (day >31) { | |
printf("attempt to store %d in 5 bits\n",day); | |
exit(1); | |
} | |
else if (month > 12) { | |
printf("attempt to store %d in 4 bits\n",month); | |
exit(1); | |
} | |
else if (year > pow(2,12)) { | |
printf("attempt to store %d in 32 bits\n",year); | |
exit(1); | |
} | |
int date = pack_value(day,5,0)|pack_value(month,4,5)|pack_value(year,12,20); | |
printf("The date is %d\n",date); | |
get_date(date,&day,&month,&year); | |
printf("day: %d, month: %d, year: %d\n\n",day,month,year); | |
char read,write,execute; | |
int count = 0; | |
char *users[] = {"owner","groups","others"}; | |
/*while (count < 3) { | |
printf("\nPermissions for %s:\n", users[count]); | |
printf("\tread: "); | |
scanf("%c",read); | |
printf("\twrite: "); | |
scanf("%c",write); | |
printf("\texecute: "); | |
scanf("%c",execute); | |
count++; | |
}*/ | |
printf("\nPermissions for owner:\n"); | |
printf("\tread: "); | |
scanf("%c",read); | |
printf("\twrite: "); | |
scanf("%c",write); | |
printf("\texecute: "); | |
scanf("%c",execute); | |
int owner = pack_value(read,2,1)|pack_value(write,2,4)|pack_value(execute,2,7); | |
} | |
int pack_value(int value, int size, int low_bit) { | |
int bitmask = pow(2,size)-1; | |
int newVal = value&0xfffff; | |
printf("newval: %d\n",newVal); | |
if (newVal != value) { | |
printf("value is too high. Exiting...\n"); | |
exit(1); | |
} | |
return newVal << low_bit; | |
} | |
int unpack_value(int holder, int size, int low_bit) { | |
int bitmask = pow(2,size)-1; | |
int newVal = holder&(bitmask<<low_bit); | |
return newVal >> low_bit; | |
} | |
void get_date(int holder, int *day, int *month, int *year) { | |
*day = unpack_value(holder,5,0); | |
*month = unpack_value(holder,4,5); | |
*year = unpack_value(holder,12,20); | |
} | |
void get_permissions(int permission_int, int class, int *read, int *write, int *execute) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment