/** Binds a BSD socket to the specified address. * \ingroup adbus_Socket * * Supported address types are: * - TCP sockets of the form "tcp:host=<hostname>,port=<port number>" * - On unix: unix sockets of the form "unix:file=<filename>" * - On linux: abstract unix sockets of the form "unix:abstract=<filename>" * * For TCP: specifying a port of 0 will cause the underlying sockets API to * choose a random available port. The chosen port can be found by using * getsockname() on the returned socket. * * \return ADBUS_SOCK_INVALID on error * * For example: * \code * adbus_Socket s = adbus_sock_bind_s("tcp:host=localhost,port=12345", -1); * \endcode * * \sa adbus_connect_address() */ adbus_Socket adbus_sock_bind_s( const char* envstr, int size) { if (size < 0) size = strlen(envstr); char* str = adbusI_strndup(envstr, size); struct Fields f; memset(&f, 0, sizeof(struct Fields)); ParseFields(&f, str, size); adbus_Socket sfd = ADBUS_SOCK_INVALID; #ifdef _WIN32 if (f.proto && strcmp(f.proto, "tcp") == 0 && f.host && f.port) { sfd = BindTcp(&f); } #else if (f.proto && strcmp(f.proto, "tcp") == 0 && f.host && f.port) { sfd = BindTcp(&f); } else if (f.proto && strcmp(f.proto, "unix") == 0 && f.file) { sfd = BindUnix(&f); } else if (f.proto && strcmp(f.proto, "unix") == 0 && f.abstract) { sfd = BindAbstract(&f); } #endif free(str); return sfd; }
/*++ GetGroupList Creates a list of groups from the 'group' file located in 'etcPath'. Arguments: interp - Interpreter to use for error reporting. etcPath - Path to glFTPD's 'etc' directory. groupListPtr - Pointer to a receive a list of 'GlGroup' structures. Return Value: A standard Tcl result. Remarks: If the function fails, an error message is left in the interpreter's result. --*/ static int GetGroupList( Tcl_Interp *interp, const char *etcPath, GlGroup **groupListPtr ) { char *p; char line[512]; char groupFile[PATH_MAX]; int nameLength; long userId; FILE *stream; strncpy(groupFile, etcPath, ARRAYSIZE(groupFile)); strncat(groupFile, GLFTPD_GROUP, ARRAYSIZE(groupFile)); groupFile[ARRAYSIZE(groupFile)-1] = '\0'; stream = fopen(groupFile, "r"); if (stream == NULL) { Tcl_ResetResult(interp); Tcl_AppendResult(interp, "unable to open \"", groupFile, "\": ", Tcl_PosixError(interp), NULL); return TCL_ERROR; } while ((p = fgets(line, ARRAYSIZE(line), stream)) != NULL) { // Strip leading spaces and skip empty or commented lines. while (*p == ' ' || *p == '\t') { p++; } if (*p == '\0' || *p == '#') { continue; } // A 'passwd' entry has 3 delimiters for 4 fields. // Format: Group:Description:GID:Irrelevant if (ParseFields(p, 3, &nameLength, &userId) == TCL_OK) { GlGroup *groupPtr = (GlGroup *)ckalloc(sizeof(GlUser)); if (nameLength >= GL_GROUP_LENGTH) { nameLength = GL_GROUP_LENGTH; } else { nameLength++; } strncpy(groupPtr->name, p, nameLength); groupPtr->name[nameLength-1] = '\0'; groupPtr->id = userId; // Insert entry at the list head. groupPtr->next = *groupListPtr; *groupListPtr = groupPtr; } } fclose(stream); return TCL_OK; }
/*++ GetUserList Creates a list of users from the 'passwd' file located in 'etcPath'. Arguments: interp - Interpreter to use for error reporting. etcPath - Path to glFTPD's 'etc' directory. userListPtr - Pointer to a receive a list of 'GlUser' structures. Return Value: A standard Tcl result. Remarks: If the function fails, an error message is left in the interpreter's result. --*/ static int GetUserList( Tcl_Interp *interp, const char *etcPath, GlUser **userListPtr ) { char *p; char line[512]; char passwdFile[PATH_MAX]; int nameLength; long userId; FILE *stream; strncpy(passwdFile, etcPath, ARRAYSIZE(passwdFile)); strncat(passwdFile, GLFTPD_PASSWD, ARRAYSIZE(passwdFile)); passwdFile[ARRAYSIZE(passwdFile)-1] = '\0'; stream = fopen(passwdFile, "r"); if (stream == NULL) { Tcl_ResetResult(interp); Tcl_AppendResult(interp, "unable to open \"", passwdFile, "\": ", Tcl_PosixError(interp), NULL); return TCL_ERROR; } while ((p = fgets(line, ARRAYSIZE(line), stream)) != NULL) { // Strip leading spaces and skip empty or commented lines. while (*p == ' ' || *p == '\t') { p++; } if (*p == '\0' || *p == '#') { continue; } // A 'passwd' entry has 6 delimiters for 7 fields. // Format: User:Password:UID:GID:Date:HomeDir:Irrelevant if (ParseFields(p, 6, &nameLength, &userId) == TCL_OK) { GlUser *userPtr = (GlUser *)ckalloc(sizeof(GlUser)); if (nameLength >= GL_USER_LENGTH) { nameLength = GL_USER_LENGTH; } else { nameLength++; } strncpy(userPtr->name, p, nameLength); userPtr->name[nameLength-1] = '\0'; userPtr->id = userId; // Insert entry at the list head. userPtr->next = *userListPtr; *userListPtr = userPtr; } } fclose(stream); return TCL_OK; }
int main(int argc, char* argv[]) { ParseFields(testStringBuffer, pFields, NUM_FIELDS, ","); for(int i=0; i<NUM_FIELDS; i++) { printf("Result: %d - %s\n",i,pFields[i]); } return 0; }