Removed all code that uses OpenGL from Image.
[io/quag.git] / addons / User / source / IoUser.c
blob39b181b1e19b5a5a8e38949d124c8faa9d1e699b
1 /*#io
2 User ioDoc(
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")
7 docCategory("Server")
8 */
10 #include "IoUser.h"
11 #include "IoState.h"
12 #include "IoNumber.h"
13 #include "IoList.h"
14 #include "IoDirectory.h"
15 #include <stdlib.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);
22 return tag;
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},
36 {NULL, NULL},
38 IoObject_addMethodTable_(self, methodTable);
40 return self;
43 IoUser *IoUser_rawClone(IoUser *proto)
45 IoUser *self = IoObject_rawClonePrimitive(proto);
46 return self;
49 /* ----------------------------------------------------------- */
51 IoUser *IoUser_new(void *state)
53 IoObject *proto = IoState_protoWithInitFunction_(state, IoUser_proto);
54 return IOCLONE(proto);
57 /* ----------------------------------------------------------- */
59 #ifdef WIN32
61 #include <windows.h>
62 #include <shlobj.h>
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));
79 #else /* Unix */
81 #include <unistd.h>
82 #include <sys/types.h>
83 #include <pwd.h>
85 IoObject *IoUser_protoName(IoUser *self, IoObject *locals, IoMessage *m)
87 /*#io
88 docSlot("name", "Returns the current user's name.")
91 char *userName = (char *)getlogin();
93 if (userName == NULL)
95 userName = getenv("LOGNAME");
98 if (userName == NULL)
100 return IONIL(self);
103 return IOSYMBOL(userName);
106 #define IODIRECTORY(path) IoDirectory_newWithPath_(IOSTATE, IOSYMBOL(path));
108 IoObject *IoUser_homeDirectory(IoUser *self, IoObject *locals, IoMessage *m)
110 /*#io
111 docSlot("homeDirectory", "Returns the current user's home directory as a Directory object.")
114 char *login = (char *)getlogin();
116 IoSymbol *homePath;
119 if (login)
121 struct passwd *pw = getpwnam(login);
123 if (pw && pw->pw_dir)
125 return IODIRECTORY(pw->pw_dir);
130 char *path = getenv("HOME");
132 if (path)
134 return IODIRECTORY(path);
136 else
138 return IODIRECTORY("~");
143 #endif