24.01.2014 Views

Codice

Codice

Codice

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

310 volume VI os16<br />

Sorgenti della libreria generale 311<br />

«<br />

«<br />

«<br />

105.5.2 lib/grp/getgrnam.c<br />

Si veda la sezione 101.2.<br />

2340001 |#include <br />

2340002 |//----------------------------------------------------------------------<br />

2340003 |struct group *<br />

2340004 |getgrnam (const char *name)<br />

2340005 |{<br />

2340006 | return (getgrgid ((gid_t) 0));<br />

2340007 |}<br />

105.6 os16: «lib/libgen.h»<br />

Si veda la sezione 101.2.<br />

2350001 |#ifndef _LIBGEN_H<br />

2350002 |#define _LIBGEN_H 1<br />

2350003 |<br />

2350004 |//----------------------------------------------------------------------<br />

2350005 |char *basename (char *path);<br />

2350006 |char *dirname (char *path);<br />

2350007 |//----------------------------------------------------------------------<br />

2350008 |<br />

2350009 |#endif<br />

105.6.1 lib/libgen/basename.c<br />

Si veda la sezione 98.7.<br />

2360001 |#include <br />

2360002 |#include <br />

2360003 |#include <br />

2360004 |#include <br />

2360005 |//----------------------------------------------------------------------<br />

2360006 |char *<br />

2360007 |basename (char *path)<br />

2360008 |{<br />

2360009 | static char *point = "."; // When ‘path’ is NULL.<br />

2360010 | char *p; // Pointer inside ‘path’.<br />

2360011 | int i; // Scan index inside ‘path’.<br />

2360012 | //<br />

2360013 | // Empty path.<br />

2360014 | //<br />

2360015 | if (path == NULL || strlen (path) == 0)<br />

2360016 | {<br />

2360017 | return (point);<br />

2360018 | }<br />

2360019 | //<br />

2360020 | // Remove all final ‘/’ if it exists, excluded the first character:<br />

2360021 | // ‘i’ is kept greater than zero.<br />

2360022 | //<br />

2360023 | for (i = (strlen (path) - 1); i > 0 && path[i] == ’/’; i--)<br />

2360024 | {<br />

2360025 | path[i] = 0;<br />

2360026 | }<br />

2360027 | //<br />

2360028 | // After removal of extra final ‘/’, if there is only one ‘/’, this<br />

2360029 | // is to be returned.<br />

2360030 | //<br />

2360031 | if (strncmp (path, "/", PATH_MAX) == 0)<br />

2360032 | {<br />

2360033 | return (path);<br />

2360034 | }<br />

2360035 | //<br />

2360036 | // If there are no ‘/’.<br />

2360037 | //<br />

2360038 | if (strchr (path, ’/’) == NULL)<br />

2360039 | {<br />

2360040 | return (path);<br />

2360041 | }<br />

2360042 | //<br />

2360043 | // Find the last ‘/’ and calculate a pointer to the base name.<br />

2360044 | //<br />

2360045 | p = strrchr (path, (unsigned int) ’/’);<br />

2360046 | p++;<br />

2360047 | //<br />

2360048 | // Return the pointer to the base name.<br />

2360049 | //<br />

2360050 | return (p);<br />

2360051 |}<br />

2370028 | //<br />

2370029 | // Remove all final ‘/’ if it exists, excluded the first character:<br />

2370030 | // ‘i’ is kept greater than zero.<br />

2370031 | //<br />

2370032 | for (i = (strlen (path) - 1); i > 0 && path[i] == ’/’; i--)<br />

2370033 | {<br />

2370034 | path[i] = 0;<br />

2370035 | }<br />

2370036 | //<br />

2370037 | // After removal of extra final ‘/’, if there is only one ‘/’, this<br />

2370038 | // is to be returned.<br />

2370039 | //<br />

2370040 | if (strncmp (path, "/", PATH_MAX) == 0)<br />

2370041 | {<br />

2370042 | return (path);<br />

2370043 | }<br />

2370044 | //<br />

2370045 | // If there are no ‘/’<br />

2370046 | //<br />

2370047 | if (strchr (path, ’/’) == NULL)<br />

2370048 | {<br />

2370049 | return (point);<br />

2370050 | }<br />

2370051 | //<br />

2370052 | // If there is only a ‘/’ a the beginning.<br />

2370053 | //<br />

2370054 | if (path[0] == ’/’ &&<br />

2370055 | strchr (&path[1], (unsigned int) ’/’) == NULL)<br />

2370056 | {<br />

2370057 | path[1] = 0;<br />

2370058 | return (path);<br />

2370059 | }<br />

2370060 | //<br />

2370061 | // Replace the last ‘/’ with zero.<br />

2370062 | //<br />

2370063 | p = strrchr (path, (unsigned int) ’/’);<br />

2370064 | *p = 0;<br />

2370065 | //<br />

2370066 | // Now remove extra duplicated final ‘/’, except the very first<br />

2370067 | // character: ‘i’ is kept greater than zero.<br />

2370068 | //<br />

2370069 | for (i = (strlen (path) - 1); i > 0 && path[i] == ’/’; i--)<br />

2370070 | {<br />

2370071 | path[i] = 0;<br />

2370072 | }<br />

2370073 | //<br />

2370074 | // Now ‘path’ appears as a reduced string: the original path string<br />

2370075 | // is modified.<br />

2370076 | //<br />

2370077 | return (path);<br />

2370078 |}<br />

105.7 os16: «lib/pwd.h»<br />

Si veda la sezione 101.2.<br />

2380001 |#ifndef _PWD_H<br />

2380002 |#define _PWD_H 1<br />

2380003 |<br />

2380004 |#include <br />

2380005 |#include <br />

2380006 |#include // gid_t, uid_t<br />

2380007 |//----------------------------------------------------------------------<br />

2380008 |struct passwd {<br />

2380009 | char *pw_name;<br />

2380010 | char *pw_passwd;<br />

2380011 | uid_t pw_uid;<br />

2380012 | gid_t pw_gid;<br />

2380013 | char *pw_gecos;<br />

2380014 | char *pw_dir;<br />

2380015 | char *pw_shell;<br />

2380016 |};<br />

2380017 |//----------------------------------------------------------------------<br />

2380018 |struct passwd *getpwent (void);<br />

2380019 |void setpwent (void);<br />

2380020 |void endpwent (void);<br />

2380021 |struct passwd *getpwnam (const char *name);<br />

2380022 |struct passwd *getpwuid (uid_t uid);<br />

2380023 |//----------------------------------------------------------------------<br />

2380024 |<br />

2380025 |#endif<br />

«<br />

«<br />

105.6.2 lib/libgen/dirname.c<br />

Si veda la sezione 98.7.<br />

2370001 |#include <br />

2370002 |#include <br />

2370003 |#include <br />

2370004 |#include <br />

2370005 |//----------------------------------------------------------------------<br />

2370006 |char *<br />

2370007 |dirname (char *path)<br />

2370008 |{<br />

2370009 | static char *point = "."; // When ‘path’ is NULL.<br />

2370010 | char *p; // Pointer inside ‘path’.<br />

2370011 | int i; // Scan index inside ‘path’.<br />

2370012 | //<br />

2370013 | // Empty path.<br />

2370014 | //<br />

2370015 | if (path == NULL || strlen (path) == 0)<br />

2370016 | {<br />

2370017 | return (point);<br />

2370018 | }<br />

2370019 | //<br />

2370020 | // Simple cases.<br />

2370021 | //<br />

2370022 | if (strncmp (path, "/", PATH_MAX) == 0 ||<br />

2370023 | strncmp (path, ".", PATH_MAX) == 0 ||<br />

2370024 | strncmp (path, "..", PATH_MAX) == 0)<br />

2370025 | {<br />

2370026 | return (path);<br />

2370027 | }<br />

105.7.1 lib/pwd/pwent.c<br />

Si veda la sezione 98.53.<br />

2390001 |#include <br />

2390002 |#include <br />

2390003 |#include <br />

2390004 |#include <br />

2390005 |<br />

2390006 |//----------------------------------------------------------------------<br />

2390007 |static char buffer[BUFSIZ];<br />

2390008 |static struct passwd pw;<br />

2390009 |static FILE *fp = NULL;<br />

2390010 |//----------------------------------------------------------------------<br />

2390011 |struct passwd *<br />

2390012 |getpwent (void)<br />

2390013 |{<br />

2390014 | void *pstatus;<br />

2390015 | char *char_uid;<br />

2390016 | char *char_gid;<br />

2390017 | //<br />

2390018 | if (fp == NULL)<br />

2390019 | {<br />

2390020 | fp = fopen ("/etc/passwd", "r");<br />

2390021 | if (fp == NULL)<br />

2390022 | {<br />

2390023 | return NULL;<br />

2390024 | }<br />

2390025 | }<br />

2390026 | //<br />

2390027 | pstatus = fgets (buffer, BUFSIZ, fp);<br />

2390028 | if (pstatus == NULL)<br />

«

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!