How to compare POSIX ACLs? - c

Given two acl_t values, how do I compare them? That is, I need a way to find out if one acl_t has more or less entries than the other, or if the given acl_entry_t contains different set of permissions than the other one.
Linux does have non-standard acl_cmp function. What I need is basically a POSIX-compliant variant of this function.
The only idea I've come up with so far is comparing ACL's textual representations:
strcmp(acl_to_text(acl1), acl_to_text(acl2))
Any more performant solutions?

Since acl_t is an opaque type, the client code can't implement acl_cmp. Moreover, using strcmp(acl_to_text(acl1), acl_to_text(acl2)) also can't be relied on, because ACL's may be equivalent, but sorted in different order.
I came to conclusion that this function along with some others should've ended up in the standard, but unfortunate way of events resulted in its abandoned state.

Related

Tcl String function that does "shimmering" ruined my customized tcl type defined in c

I have defined a customized tcl type using tcl library in c/c++. I basically make the Tcl_Obj.internalRep.otherValuePtr point to my own data structure. The problem happens by calling [string length myVar] or other similar string functions that does so called shimmering behaviour which replace my internalRep with it's own string structure. So that after the string series tcl function, myVar cannot convert back! because it's a complicate data structure cannot be converted back from the Tcl_Obj.bytes representation plus the type is no longer my customized type. How can I avoid that.
The string length command converts the internal representation of the values it is given to the special string type, which records information to allow many string operations to be performed rapidly. Apart from most of the string command's various subcommands, the regexp and regsub commands are the main ones that do this (for their string-to-match-the-RE-against argument). If you have a precious internal representation of your own and do not wish to lose it, you should avoid those commands; there are some operations that avoid the trouble. (Tcl mostly assumes that internal representations are not fragility, and therefore that they can be regenerated on demand. Beware when using fragility!)
The key operations that are mostly safe (as in they generate the bytes/length rep through calling the updateStringProc if needed, but don't clear the internal rep) are:
substitution into a string; the substituted value won't have the internal rep, but it will still be in the original object.
comparison with the eq and ne expression operators. This is particularly relevant for checks to see if the value is the empty string.
Be aware that there are many other operations that spoil the internal representation in other ways, but most don't catch people out so much.
[EDIT — far too long for a comment]: There are a number of relatively well-known extensions that work this way (e.g., TCOM and Tcl/Java both do this). The only thing you can really do is “be careful” as the values really are fragile. For example, put them in an array and then pass the indexes into the array around instead, as those need not be fragile. Or keep things as elements in a list (probably in a global variable) and pass around the list indices; those are just plain old numbers.
The traditional, robust approach is to put a map (e.g., a Tcl_HashTable or std::map) in your C or C++ code and have the indices into that be short strings with not too much meaning (I like to use the name of the type of value followed by either a sequence number or a serialisation of the pointer, such as you might get with the %p conversion in sprintf(); the printed pointer reveals more of the implementation details, is a little more helpful if you're debugging, and generally doesn't actually make that much difference in practice). You then have the removal of things from the map be an explicit deletion operation, and it is also easy to provide operations like listing all the known current values. This is safe, but prone to “leaking” (though it's not formally a memory leak if you provide the listing operation). It can be accelerated by caching the lookup in a Tcl_Obj*'s internal representation (a cheap way to handle deletion is to use a sequence number that you increment when you delete something, and only bypass the map lookup if the sequence number that you cache in the intrep is equal to the main sequence number) but it's not usually a big deal; only go to that sort of thing if you've measured a bottleneck in the lookups.
But I'd probably just live with fragility in my own code, and would just take care to ensure that I never bust the assumptions. The problem is really that you're being incautious about how you use the values; the Tcl code should just pass them around and nothing else really. Also, I've experimented a fair bit with wrapping such things up inside a TclOO object; it's far too heavyweight (by the design of TclOO) for values that you're making a lot of, but if you've only got a few of them and you're wanting to treat them as objects with methods, this can work very well indeed (and gives many more options for automatic cleanup).

A function returning "ENOTDIR", "EBUSY", etc. as strings?

strerror() function returns a short error description, given error number as argument. For example, if the argument is ENOTDIR, it will return "Not a directory", if the argument is EBUSY, it will return "Device or resource busy", etc.
But, is there a function that returns "ENOTDIR" for argument equals ENOTDIR, "EBUSY" for argument EBUSY, etc.?
I just want to avoid writing a huge switch statement for this purpose.
No- there is no standard or commonly used nonstandard function that provides this functionality.
One approach would be to write a huge switch statement, but this might not be the best approach for you to take. Most values of errno are not specified by any standard, so their values may or may not be consistent across different operating systems or even different versions of the same operating system.
Plus it would be a pain in the rear end.
A more elegant approach, if some runtime overhead is acceptable, would be to write a function that looks up these errors codes when they occur, rather than hard-coding the values into a big table. GNU/Linux systems have a list of all possible errno values at:
/usr/include/asm-generic/errno-base.h
/usr/include/asm-generic/errno.h
These files provide a #define of each errno value along with their value and a short description in an adjacent comment. It'd be pretty trivial to search these files line-by-line and print out the matching error code. Even if not, these files would be the things to start with in your quest to write a huge switch statement.
Beware that the kernel might negate these values when they're passed to userspace.
As David points out above, the problem is there is no standard function that can provide the desired functionality. So thinking that this would be a neat problem to try to write a script for, I wrote a little something to automatically generate the switch function (if it should come to be necessary) and posted the code here. Seems to work alright on OS X, otherwise mileage may vary. A script such as this could be added to the build process to make sure that the values were defined correctly.

Why do C written libraries use so many structs?

I've looked to some open source Libraries in some places. And, I've realized which that Libraries are basically a great stack of structs. I've seen few methods.
Why does C written libraries uses so much structs? What's the basis behind this? This, for me, looked like a attempt to simulate object orientation, 'cause a fast searching told me that each struct is "instantiated" by the using program to make something, per example, in some Desktop enviroments for linux that I've seen that each window was a struct in the used GUI library.
Anyway, the question is that.
Structs are a great way to organize data. And data is fundamental, as Fred Brooks knew decades ago:
Show me your flowcharts and conceal your tables, and I shall continue
to be mystified. Show me your tables, and I won't usually need your
flowcharts; they'll be obvious.
Object-oriented programming doesn't have to be merely simulated in C, it can be realized. For example, did you know that inside your structs you can store function pointers which operate on those same structs, and then you are a little bit closer to C++'s classes?
Also consider extensibility: even a function taking many arguments may be improved by taking a single struct, because then its signature does not need to change when a new argument is added.
Finally, C does not have multiple return values from a single function call. But it can return a struct, which is about the same thing. C is a lot about building your own tools from the raw language, and being able to stash a bunch of related data and/or functions together in one place is a good building block.
With or without object orientation, structures are a useful way to group aggregate data into a single symbol. You can copy the structure wherever you like without having to write out all the members each time, and this makes the structure easier to change if you have to.
It also makes it easier to reference certain members using pointer arithmetic, if you're careful (see sockaddr).
Same argument as with arrays.
Simply put, there's no reason not to use structures.
Structures are useful while retrieving data using a pointer. Because single pointer is enough for complete bunch of data with in a structure.
One, it keeps the APIs clean. Instead of passing N separate arguments to a function, you pass a single argument containing N members.
Two, it allows the library to hide implementation details from the programmer. For example, the C FILE type abstracts away some details of stream I/O, details which vary from implementation to implementation. We don't need to know those details, so they're not exposed to us; we just use the FILE type to pass that information around.

Creating a good interface for functions which works with paths

I have functions which get file path as their input argument. This functions are cross platform. Functions support both unicode and regular file paths. What is the best interface for this functions, know I have 2 chooses:
make two version of each function FunctionW and FunctionA as in WinAPI.
make one version which will get char * as input argument, but this string must be in UTF8 format.
Which one is better?
Thanks in advance!
This really depends on the rest of your code and how you're going to use them. There is no correct answer here - try to approximate the time it will take you to write, to use and to maintain each one of the options, and try to take the one where it's easier.
You should also consider the difference between FunctionA and FunctionW. If the difference isn't big, then you can likely use a single inner helper function that both of them will call, and so the extra time for writing and maintaining a second function is minimal. If it is, consider how tough it would be (if at all) to convert strings to UTF8 for the 2nd option you presented.

How to represent functions in flowchart?

I define some function in my Ansi C Program (simple program). I don't known how to represent a function in flowchart. Anybody can help me?
In my opinion, a flowchart is more of a functional description of your algorithm and not where you would "define" a function in the sense of your program. Yes, a functional aspect represented in your flowchart may directly map to a single function in your C program, but it may be that multiple functions or multiple threads are used to accomplish it as well. The flowchart isn't where you would describe these.
In short, the flowchart is not where you should be "defining" functions for your C program. It should be a high-level representation of functional aspects of your program, not the implementation of it.
On a flowchart, a function can be anything: a state, an action that occurs while transitioning betwwen states, etc. It all depends on how you have your flowchart organized. I would recommend building your flowchart normally, then go back and add a function name to the description of anything that is implemented by a function.
There is no direct symbol.. you can create your function with the basic input /output/process symbols
Here is a tutorial on writing C functions using DRAKON charts (DRAKON charts are very similar to flowcharts):
http://drakon-editor.sourceforge.net/cpp/c.html
I had a similar question, Flowcharting a Get-ter, which was answered with:
NOTE: Please don't upvote this answer but instead the original within the above link.
Flowcharts represent flow of control, not flow of information.
Flowcharting formally captures steps and the linkages between them
that describe the transfer of the flow of control that are often based
on decisions: in particular, conditional branches and loops.
Flow of control is about what is done or happens next, and (sadly) not
about the required data to perform that step.
According to Wikipedia, there are some extensions for the flow of
data; however, they are basically limited to documents and files.
Generally speaking, state is poorly represented in flowcharting; there
is virtually no notion of data, variables, scopes, lifetimes, or
types. So, data (and metadata about that data, such as allowed or
expected types) is mostly documented informally with human language
description in the text within individual steps of the flowchart.
Input & Output in flowcharting is meant to indicate communication with
another independent and top-level process (even if it is just a later
running copy of one's self). As such this communication is about
reading/writing to disc or to a network.
A getter does not qualify as input or output, which is to say
communication with another independent process, so I think that is
out. I don't think they even had getter's when flowcharting was first
applied to software design (circa 1950).
You might look in to UML. – Erik Eidt Dec 12 '16 at 16:51

Resources