-
-
Save cygx/9b64a69fb9dc60d05bd8 to your computer and use it in GitHub Desktop.
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
if (0 == strcmp("r", fmode)) | |
flag = O_RDONLY; | |
else if (0 == strcmp("w", fmode)) | |
flag = O_CREAT| O_WRONLY | O_TRUNC; | |
else if (0 == strcmp("wa", fmode)) | |
flag = O_CREAT | O_WRONLY | O_APPEND; | |
else if !parse_open_mode(&flag, fmode) { | |
MVM_free(fname); | |
MVM_exception_throw_adhoc(tc, "Invalid open mode: %s", fmode); | |
} | |
static int parse_open_mode(int *flag, const char *cp) { | |
switch(*cp++) { | |
case 'r': | |
*flag = O_RDONLY; | |
break; | |
case '-': | |
*flag = O_WRONLY; | |
break; | |
case '+': | |
*flag = O_RDWR; | |
break; | |
default: | |
return 0; | |
} | |
for(;;) switch(*cp++) { | |
case 0: | |
return 1; | |
case 'a': | |
*flag |= O_APPEND; | |
break; | |
case 'c': | |
*flag |= O_CREAT; | |
break; | |
case 'x': | |
*flag |= O_EXCL; | |
break; | |
case 't': | |
*flag |= O_TRUNC; | |
break; | |
default: | |
return 0; | |
} | |
} |
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
my $m = given $mode { | |
when '<' { 'r' } | |
when '>' { '-ct' } | |
when '>>' { '-ca' } | |
when '+<' { '+' } | |
when '+>' { '+ct' } | |
when '+>>' { '+ca' } | |
when 'r' { 'r' } | |
when 'w' { '-ct' } | |
when 'wx' { '-cx' } | |
when 'a' { '-ca' } | |
when 'r+' { '+' } | |
when 'w+' { '+ct' } | |
when 'w+x' { '+cx' } | |
when 'a+' { '+ca' } | |
when 'rb' { $bin = True; 'r' } | |
when 'wb' { $bin = True; '-ct' } | |
when 'wbx' { $bin = True; '-cx' } | |
when 'ab' { $bin = True; '-ca' } | |
when 'r+b'|'rb+' { $bin = True; '+' } | |
when 'w+b'|'wb+' { $bin = True; '+ct' } | |
when 'w+bx'|'wb+x' { $bin = True; '+cx' } | |
when 'a+b '|'ab+' { $bin = True; '+ca' } | |
default { die } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment