3 docCopyright("Steve Dekorte", 2004)
4 docLicense("BSD revised")
5 docDescription("This object provides access to the local operating system's information about the current user.")
6 //docCredits("Windows code by Mike Austin")
14 #include "IoDirectory.h"
17 IoTag
*IoUser_newTag(void *state
)
19 IoTag
*tag
= IoTag_newWithName_("User");
20 IoTag_state_(tag
, state
);
21 IoTag_cloneFunc_(tag
, (IoTagCloneFunc
*)IoUser_rawClone
);
25 IoUser
*IoUser_proto(void *state
)
27 IoObject
*self
= IoObject_new(state
);
28 IoObject_tag_(self
, IoUser_newTag(state
));
30 IoState_registerProtoWithFunc_(state
, self
, IoUser_proto
);
33 IoMethodTable methodTable
[] = {
34 {"name", IoUser_protoName
},
35 {"homeDirectory", IoUser_homeDirectory
},
38 IoObject_addMethodTable_(self
, methodTable
);
43 IoUser
*IoUser_rawClone(IoUser
*proto
)
45 IoUser
*self
= IoObject_rawClonePrimitive(proto
);
49 /* ----------------------------------------------------------- */
51 IoUser
*IoUser_new(void *state
)
53 IoObject
*proto
= IoState_protoWithInitFunction_(state
, IoUser_proto
);
54 return IOCLONE(proto
);
57 /* ----------------------------------------------------------- */
64 IoObject
*IoUser_protoName(IoUser
*self
, IoObject
*locals
, IoMessage
*m
)
66 TCHAR userName
[256]; DWORD userSize
= 255;
67 /* Copies up to userSize, then sets userSize to actual size */
68 GetUserName(userName
, &userSize
);
69 return IOSYMBOL(userName
);
72 IoObject
*IoUser_homeDirectory(IoUser
*self
, IoObject
*locals
, IoMessage
*m
)
74 TCHAR homePath
[MAX_PATH
];
75 SHGetFolderPath( NULL
, CSIDL_APPDATA
, NULL
, 0, homePath
);
76 return IoDirectory_newWithPath_(IOSTATE
, IOSYMBOL(homePath
));
82 #include <sys/types.h>
85 IoObject
*IoUser_protoName(IoUser
*self
, IoObject
*locals
, IoMessage
*m
)
88 docSlot("name", "Returns the current user's name.")
91 char *userName
= (char *)getlogin();
95 userName
= getenv("LOGNAME");
103 return IOSYMBOL(userName
);
106 #define IODIRECTORY(path) IoDirectory_newWithPath_(IOSTATE, IOSYMBOL(path));
108 IoObject
*IoUser_homeDirectory(IoUser
*self
, IoObject
*locals
, IoMessage
*m
)
111 docSlot("homeDirectory", "Returns the current user's home directory as a Directory object.")
114 char *login
= (char *)getlogin();
121 struct passwd
*pw
= getpwnam(login
);
123 if (pw
&& pw
->pw_dir
)
125 return IODIRECTORY(pw
->pw_dir
);
130 char *path
= getenv("HOME");
134 return IODIRECTORY(path
);
138 return IODIRECTORY("~");